99import scipy as sp
1010import matplotlib .pyplot as plt
1111
12- data_dir = os . path . join (
13- os . path . dirname ( os . path . realpath ( __file__ )), ".." , "data" )
14- data = sp .genfromtxt (os .path .join (data_dir , "web_traffic.tsv" ), delimiter = "\t " )
12+ from utils import DATA_DIR , CHART_DIR
13+
14+ data = sp .genfromtxt (os .path .join (DATA_DIR , "web_traffic.tsv" ), delimiter = "\t " )
1515print (data [:10 ])
1616
1717# all examples will have three classes in this file
@@ -56,7 +56,7 @@ def plot_models(x, y, models, fname, mx=None, ymax=None, xmin=None):
5656 plt .savefig (fname )
5757
5858# first look at the data
59- plot_models (x , y , None , os .path .join (".." , "1400_01_01.png" ))
59+ plot_models (x , y , None , os .path .join (CHART_DIR , "1400_01_01.png" ))
6060
6161# create and plot models
6262fp1 , res , rank , sv , rcond = sp .polyfit (x , y , 1 , full = True )
@@ -68,10 +68,10 @@ def plot_models(x, y, models, fname, mx=None, ymax=None, xmin=None):
6868f10 = sp .poly1d (sp .polyfit (x , y , 10 ))
6969f100 = sp .poly1d (sp .polyfit (x , y , 100 ))
7070
71- plot_models (x , y , [f1 ], os .path .join (".." , "1400_01_02.png" ))
72- plot_models (x , y , [f1 , f2 ], os .path .join (".." , "1400_01_03.png" ))
71+ plot_models (x , y , [f1 ], os .path .join (CHART_DIR , "1400_01_02.png" ))
72+ plot_models (x , y , [f1 , f2 ], os .path .join (CHART_DIR , "1400_01_03.png" ))
7373plot_models (
74- x , y , [f1 , f2 , f3 , f10 , f100 ], os .path .join (".." , "1400_01_04.png" ))
74+ x , y , [f1 , f2 , f3 , f10 , f100 ], os .path .join (CHART_DIR , "1400_01_04.png" ))
7575
7676# fit and plot a model using the knowledge about inflection point
7777inflection = 3.5 * 7 * 24
@@ -83,7 +83,7 @@ def plot_models(x, y, models, fname, mx=None, ymax=None, xmin=None):
8383fa = sp .poly1d (sp .polyfit (xa , ya , 1 ))
8484fb = sp .poly1d (sp .polyfit (xb , yb , 1 ))
8585
86- plot_models (x , y , [fa , fb ], os .path .join (".." , "1400_01_05.png" ))
86+ plot_models (x , y , [fa , fb ], os .path .join (CHART_DIR , "1400_01_05.png" ))
8787
8888
8989def error (f , x , y ):
@@ -102,7 +102,7 @@ def error(f, x, y):
102102
103103# extrapolating into the future
104104plot_models (
105- x , y , [f1 , f2 , f3 , f10 , f100 ], os .path .join (".." , "1400_01_06.png" ),
105+ x , y , [f1 , f2 , f3 , f10 , f100 ], os .path .join (CHART_DIR , "1400_01_06.png" ),
106106 mx = sp .linspace (0 * 7 * 24 , 6 * 7 * 24 , 100 ),
107107 ymax = 10000 , xmin = 0 * 7 * 24 )
108108
@@ -118,7 +118,7 @@ def error(f, x, y):
118118 print ("Error d=%i: %f" % (f .order , error (f , xb , yb )))
119119
120120plot_models (
121- x , y , [fb1 , fb2 , fb3 , fb10 , fb100 ], os .path .join (".." , "1400_01_07.png" ),
121+ x , y , [fb1 , fb2 , fb3 , fb10 , fb100 ], os .path .join (CHART_DIR , "1400_01_07.png" ),
122122 mx = sp .linspace (0 * 7 * 24 , 6 * 7 * 24 , 100 ),
123123 ymax = 10000 , xmin = 0 * 7 * 24 )
124124
@@ -139,7 +139,7 @@ def error(f, x, y):
139139 print ("Error d=%i: %f" % (f .order , error (f , xb [test ], yb [test ])))
140140
141141plot_models (
142- x , y , [fbt1 , fbt2 , fbt3 , fbt10 , fbt100 ], os .path .join (".." ,
142+ x , y , [fbt1 , fbt2 , fbt3 , fbt10 , fbt100 ], os .path .join (CHART_DIR ,
143143 "1400_01_08.png" ),
144144 mx = sp .linspace (0 * 7 * 24 , 6 * 7 * 24 , 100 ),
145145 ymax = 10000 , xmin = 0 * 7 * 24 )
0 commit comments