Question: import matplotlib.pyplot as pltimport numpy as npfrom numpy.polynomial import Polynomialdef plot _ web _ traffic ( x , y , models = None ) :

import matplotlib.pyplot as pltimport numpy as npfrom numpy.polynomial import Polynomialdef plot_web_traffic(x, y, models=None): ''' Plot the web traffic (y) over time (x). If models is given, it is expected to be a list of fitted models, which will be plotted as well (used later in this chapter).''' plt.figure(figsize=(12,6)) # width and height of the plot in inches plt.scatter(x, y, s=10) plt.title("Web traffic over the last month") plt.xlabel("Time") plt.ylabel("Hits/hour") plt.xticks([w*7*24 for w in range(5)],['week %i'%(w+1) for w in range(5)]) if models: colors = plt.cm.viridis(np.linspace(0,1, len(models))) linestyles =['-','-.','--',':','-'] mx = np.linspace(0, x[-1],1000) for model, color in zip(models, colors): plt.plot(mx, model(mx), linestyle='-', linewidth=2, c=color) # Create legend based on model orders plt.legend(["d =%i"% m.degree() for m in models], loc="upper left") plt.autoscale(tight=True) plt.show()def calculate_total_error(x, y, model): ''' Calculate the total error for a given model. ''' return np.sum((y - model(x))**2)def predict_time_to_reach_hits(model, target_hits): ''' Predict the time to reach a certain number of hits/hour using the model. ''' # Create a range of time values to predict time_values = np.linspace(0,4*7*24,10000) predicted_hits = model(time_values) # Find the time where predicted hits reach the target try: time_to_reach = time_values[predicted_hits >= target_hits][0] except IndexError: time_to_reach = np.nan # If the target is never reached, return NaN return time_to_reach# Generate some example datanp.random.seed(0)x = np.arange(0,4*7*24,1) # every hour for four weeksy =2.5* np.sin(x /24*2* np.pi)+ np.random.normal(0,0.5, len(x))# Create 40 polynomial modelsmodels =[Polynomial.fit(x, y, deg) for deg in range(1,41)]# Calculate total errors for each modeltotal_errors =[calculate_total_error(x, y, model) for model in models]# Predict the time to reach 100,000 hits/hour for each modeltarget_hits =100000predicted_times =[predict_time_to_reach_hits(model, target_hits) for model in models]# Plot the total error against polynomial degreeplt.figure(figsize=(12,6))plt.plot(range(1,41), total_errors, marker='o')plt.title("Total Error vs. Polynomial Degree")plt.xlabel("Polynomial Degree")plt.ylabel("Total Error")plt.grid(True)plt.show()# Plot the predicted time to reach 100,000 hits/hour against polynomial degreeplt.figure(figsize=(12,6))plt.plot(range(1,41), predicted_times, marker='o')plt.title("Predicted Time to Reach 100,000 Hits/Hour vs. Polynomial Degree")plt.xlabel("Polynomial Degree")plt.ylabel("Predicted Time (hours)")plt.grid(True)plt.show()# Plot the web traffic with the polynomial modelsplot_web_traffic(x, y, models=models)
My last graph isn't showing any data. Can you please tell me what im doing wrong?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!