1010import scipy as sp
1111import matplotlib .pyplot as plt
1212
13+ sp .random .seed (3 ) # to reproduce the data later on
1314
1415data = sp .genfromtxt (os .path .join (DATA_DIR , "web_traffic.tsv" ), delimiter = "\t " )
1516print (data [:10 ])
17+ print (data .shape )
1618
1719# all examples will have three classes in this file
1820colors = ['g' , 'k' , 'b' , 'm' , 'r' ]
2527y = y [~ sp .isnan (y )]
2628
2729# plot input data
28-
29-
3030def plot_models (x , y , models , fname , mx = None , ymax = None , xmin = None ):
31+
32+ plt .figure (num = None , figsize = (8 , 6 ))
3133 plt .clf ()
3234 plt .scatter (x , y , s = 10 )
3335 plt .title ("Web traffic over the last month" )
@@ -59,11 +61,15 @@ def plot_models(x, y, models, fname, mx=None, ymax=None, xmin=None):
5961plot_models (x , y , None , os .path .join (CHART_DIR , "1400_01_01.png" ))
6062
6163# create and plot models
62- fp1 , res , rank , sv , rcond = sp .polyfit (x , y , 1 , full = True )
63- print ("Model parameters: %s" % fp1 )
64- print ("Error of the model:" , res )
64+ fp1 , res1 , rank1 , sv1 , rcond1 = sp .polyfit (x , y , 1 , full = True )
65+ print ("Model parameters of fp1 : %s" % fp1 )
66+ print ("Error of the model of fp1 :" , res1 )
6567f1 = sp .poly1d (fp1 )
66- f2 = sp .poly1d (sp .polyfit (x , y , 2 ))
68+
69+ fp2 , res2 , rank2 , sv2 , rcond2 = sp .polyfit (x , y , 2 , full = True )
70+ print ("Model parameters of fp2: %s" % fp2 )
71+ print ("Error of the model of fp2:" , res2 )
72+ f2 = sp .poly1d (fp2 )
6773f3 = sp .poly1d (sp .polyfit (x , y , 3 ))
6874f10 = sp .poly1d (sp .polyfit (x , y , 10 ))
6975f100 = sp .poly1d (sp .polyfit (x , y , 100 ))
@@ -102,7 +108,8 @@ def error(f, x, y):
102108
103109# extrapolating into the future
104110plot_models (
105- x , y , [f1 , f2 , f3 , f10 , f100 ], os .path .join (CHART_DIR , "1400_01_06.png" ),
111+ x , y , [f1 , f2 , f3 , f10 , f100 ],
112+ os .path .join (CHART_DIR , "1400_01_06.png" ),
106113 mx = sp .linspace (0 * 7 * 24 , 6 * 7 * 24 , 100 ),
107114 ymax = 10000 , xmin = 0 * 7 * 24 )
108115
@@ -118,8 +125,8 @@ def error(f, x, y):
118125 print ("Error d=%i: %f" % (f .order , error (f , xb , yb )))
119126
120127plot_models (
121- x , y , [fb1 , fb2 , fb3 , fb10 , fb100 ], os . path . join (
122- CHART_DIR , "1400_01_07.png" ),
128+ x , y , [fb1 , fb2 , fb3 , fb10 , fb100 ],
129+ os . path . join ( CHART_DIR , "1400_01_07.png" ),
123130 mx = sp .linspace (0 * 7 * 24 , 6 * 7 * 24 , 100 ),
124131 ymax = 10000 , xmin = 0 * 7 * 24 )
125132
@@ -131,6 +138,8 @@ def error(f, x, y):
131138train = sorted (shuffled [split_idx :])
132139fbt1 = sp .poly1d (sp .polyfit (xb [train ], yb [train ], 1 ))
133140fbt2 = sp .poly1d (sp .polyfit (xb [train ], yb [train ], 2 ))
141+ print ("fbt2(x)= \n %s" % fbt2 )
142+ print ("fbt2(x)-100,000= \n %s" % (fbt2 - 100000 ))
134143fbt3 = sp .poly1d (sp .polyfit (xb [train ], yb [train ], 3 ))
135144fbt10 = sp .poly1d (sp .polyfit (xb [train ], yb [train ], 10 ))
136145fbt100 = sp .poly1d (sp .polyfit (xb [train ], yb [train ], 100 ))
@@ -140,13 +149,13 @@ def error(f, x, y):
140149 print ("Error d=%i: %f" % (f .order , error (f , xb [test ], yb [test ])))
141150
142151plot_models (
143- x , y , [fbt1 , fbt2 , fbt3 , fbt10 , fbt100 ], os . path . join ( CHART_DIR ,
144- "1400_01_08.png" ),
152+ x , y , [fbt1 , fbt2 , fbt3 , fbt10 , fbt100 ],
153+ os . path . join ( CHART_DIR , "1400_01_08.png" ),
145154 mx = sp .linspace (0 * 7 * 24 , 6 * 7 * 24 , 100 ),
146155 ymax = 10000 , xmin = 0 * 7 * 24 )
147156
148157from scipy .optimize import fsolve
149158print (fbt2 )
150159print (fbt2 - 100000 )
151- reached_max = fsolve (fbt2 - 100000 , 800 ) / (7 * 24 )
160+ reached_max = fsolve (fbt2 - 100000 , x0 = 800 ) / (7 * 24 )
152161print ("100,000 hits/hour expected at week %f" % reached_max [0 ])
0 commit comments