Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Instant Reference

Authors: Gruber, Martin Gruber

2nd Edition

0782125395, 9780782125399

More Books

Students also viewed these Databases questions

Question

How can companies establish creative work environments?

Answered: 1 week ago

Question

List the advantages and disadvantages of the pay programs. page 536

Answered: 1 week ago