-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSkeleton.py
More file actions
392 lines (310 loc) · 15 KB
/
Copy pathSkeleton.py
File metadata and controls
392 lines (310 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 28 14:54:26 2019
@author: annag
Linear Regression Model v2
"""
import matplotlib.pyplot as plt
import numpy as np
path1 = r"C:\Users\annag\Documents\2018-2019\Spring_2019\BigDataProjects\flu-data-linear-regression\initial_flu.csv"
path2 = "/home/mandub/Desktop/6th semester/courses/Data Science Projects/data flu/flu-data-linear-regression/initial_flu.csv"
path3 = r"C:\Users\jakeo\OneDrive\Documents\M467\flu-data-linear-regression\initial_flu.csv"
path4 = r"C:\Users\Bill Griffin\flu-data-linear-regression\initial_flu.csv"
pathlist = [path1, path2, path3, path4]
names = ["Anna", "Mandub", "Jake", "Bill"]
for paths in range(len(pathlist)):
try:
with open(pathlist[paths]) as f:#, encoding = "utf-8"
print ("This is", names[paths])
path = pathlist[paths]
break
except:
print("This is not", names[paths])
from collections import defaultdict
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import linregress
import csv
#%%
#################################################
# read from the file and create Data dictionaries
#################################################
#============================================================ Mandub code
CountyDict = defaultdict(list)
yeardata=defaultdict(list)
CountyRateDict = defaultdict(list) # Rate
CountyCDict = defaultdict(list) # C
CountyPopDict = defaultdict(list) #population
actualNames=[] # hold the actual county Names for presentation purpose
countyLicenceNumber=[] # hold the actual county Licence Number for presentation purpose
counties=[] # hold county Shortcut names
countyIndexes=defaultdict(list) # hold 3 name and indexes for each county
# first will be for the rate
# the second will be for C
# the third will be for population
counter = 1 # number of line in the file
with open(path) as f:
for line in f:
if counter == 1: # read the first line in the file
actualNames = line.split(",")
actualNames = actualNames[3:] # remove the first 3 elements in the list
actualNames[-1] = actualNames[-1].rstrip('\n') # remove newline char from last element
counter +=1
# need to clean redundant data
elif counter == 2: #read the second line in the file
countyLicenceNumber= line.split(",")
countyLicenceNumber = countyLicenceNumber[3:] # remove the first 3 elements in the list
countyLicenceNumber[-1] = countyLicenceNumber[-1].rstrip('\n') # remove newline char from last element
counter +=1
# need to clean redundant data and convert to int
elif counter == 3: #read the third line in the file
counter +=1
counties = line.split(",")
counties[-1] = counties[-1].rstrip('\n') # remove newline char from last element
temp = counties # hold the line structure to use the indexes
counties = counties[3:]
counties2 = [] # to remove redundant data
for county in counties:
if county not in counties2:
counties2.append(county)
counties = counties2
for county in counties: # add counties indexes to countyIndexes
indexes= [] #hold indexes for only one county
# =============================================================================
CountyRateDict[county]= [] # ...we fill CountyRateDict by counties with empty list for the years
# ....where index zreo will be year 1
CountyCDict[county]=[] # ...we fill CountyCDict by counties with empty list for the years
CountyPopDict[county]= [] # ...we fill CountyPopDict by counties with empty list for the years
# =============================================================================
for index,value in enumerate (temp): # if the same county append the index
if county == value:
indexes.append(index)
countyIndexes[county]=indexes
elif counter == 4: #read the third line in the file
counter+=1 # we do not do any thing because this line is headers line
else: # read the others lines in the file for Data
data =line.split(",")
year = int (data[1]) -1 # to use year as index for Dicts
weak = int (data[2]) -1 # to use weak as index ofr Dicts
if weak == 0 : #newyear start
for county in CountyRateDict:
CountyRateDict[county].append([]) # add new list for the weaks
CountyCDict[county].append([])
CountyPopDict[county].append([])
rateIndex= countyIndexes[county][0] #take the index of rate
rate = float (data[rateIndex])
CIndex= countyIndexes[county][1] #take the index of C
C = int (data[CIndex])
popIndex= countyIndexes[county][2] #take the index of population
pop = int (data[popIndex])
CountyRateDict[county][year].append(rate)
CountyCDict[county][year].append(C)
CountyPopDict[county][year].append(pop)
#repat for othre dict
else:
for county in CountyRateDict:
rateIndex= countyIndexes[county][0] #take the index of rate
rate = float (data[rateIndex])
CIndex= countyIndexes[county][1] #take the index of C
C = int (data[CIndex])
popIndex= countyIndexes[county][2] #take the index of population
pop = int (data[popIndex])
CountyRateDict[county][year].append(rate)
CountyCDict[county][year].append(C)
CountyPopDict[county][year].append(pop)
#%%
###################################
# Apply line regulation and create
# production dictionary
###################################
productionDict = defaultdict(list)
for county in counties: # fill productionDict with empty list for ecah county
productionDict[county] =[]
from sklearn import datasets, linear_model
from sklearn.metrics import mean_squared_error, r2_score
def sklearn_linear_Regression(weeksRateslist):
Y_hat=np.array(weeksRateslist[-1])
del weeksRateslist[-1]
X_hat=np.array([weeksRateslist[-1]])
x_hat= weeksRateslist[-1]
del weeksRateslist[-1]
X = np.array(weeksRateslist)
del weeksRateslist[0]
weeksRateslist.append(x_hat)
Y= np.array(weeksRateslist)
# Create linear regression object
regr = linear_model.LinearRegression()
regr.fit(X.reshape(-1, 1), Y)
y_pred = regr.predict(X_hat.reshape(1, -1))
return y_pred[0]
def linearRegression2(weeksRateslist):
if len (weeksRateslist) ==1:
return weeksRateslist[0]
elif len (weeksRateslist) == 2:
return weeksRateslist[1]
elif len (weeksRateslist) == 3:
return weeksRateslist[2]
else:
y_pred = sklearn_linear_Regression(weeksRateslist)
return y_pred
def productionFun2(CountyRate):
alist2D = []
for yearIndex,year in enumerate (CountyRate):
weeks_up_today=[]
alist2D.append([])
for weekIndex,week in enumerate(year):
alist2D[yearIndex].append([]) #make space for a week
weeks_up_today.append(week)
aWeekPrediction = linearRegression2(weeks_up_today)
alist2D[yearIndex][weekIndex]=aWeekPrediction
return alist2D
#productionDict["SB"]= productionFun(CountyRateDict["SB"])
for county in counties:
productionDict[county]= productionFun2(CountyRateDict[county])
"""
for indexyear, year in enumerate (productionDict["SB"]):
print len(year)
print ("-------------------------------------------")
for indexweek, week in enumerate (year):
print (week,CountyRateDict["SB"][indexyear][indexweek])
"""
#%%
###################################
# plot line regulation
###################################
def plot1(County,Year):
Year= Year -1
y = range(len (CountyRateDict[County][Year]))
x = CountyRateDict[County][Year]
z = productionDict[County][Year]
actualNames[counties.index(County)]
Title= actualNames[counties.index(County)] +" Year " +str (Year +1)
plt.title(Title)
plt.plot(y, x, color = "black", linewidth = 2.0, label = "Observed Rates")
plt.plot(y, z, color = "blue", linewidth = 2.0, label = "Prediction Rates")
plt.legend(loc = "upper right")
plt.show()
plot1("SB",1)
def Plot_ObsVsPred(County,Year,nplots):
#Initialize a matrix to store predictions
predictions = np.zeros((52,10))
#Get observed rates
y = CountyRateDict[County][Year]
#Create a list of weeks
x = [i for i in range(1,len(y)+1)]
#use an input of 1 as nplots to get a single plot with all ten lines, use any other input to get ten
#separate plots.
if nplots == 1:
for i in range(0,10):
#Populates the prediction matrix with test values, change the assignment to extract predictions
#predictions[:,i] = PredictionDictionary[County][year] For example
predictions[:,i] = np.repeat(i/20000,len(y))
plt.plot(predictions[:,i], label = str(i+1)+" weeks")
plt.plot(x, y, color = "black", linewidth = 2.0, label = "Observed Rates")
plt.legend(loc = "upper right")
plt.show()
else:
for i in range(0,10):
#Populates the prediction matrix with test values, change the assignment to extract predictions
#predictions[:,i] = PredictionDictionary[County][year] For example
predictions[:,i] = np.repeat(i/20000,len(y))
plt.plot(x, predictions[:,i], label = str(i+1)+" weeks")
plt.plot(x, y, color = "black", linewidth = 2.0, label = "Observed Rates")
plt.legend(loc = "upper right")
plt.show()
#Plot_ObsVsPred("SB",1,1)
# def function(County , number of weeks , index of staring weak , list of nibers)
# return list of preductions
def makeCountyDict(counties,CountyRateDict,CountyCDict,CountyPopDict):
# inputs counties as list and CountyRateDict for rate , CountyCDict for count ,CountyPopDict for pop
# output countyDict for use it in kmean and winter functions
# Example countyDict['BE'][1]['rate'] = [ from county BE return list of weeks for rate in year 1 ]
countyDict = defaultdict(list)
for county in counties:
countyDict[county]={1:{},2:{},3:{},4:{},5:{},6:{},7:{},8:{},9:{}}
for county in counties:
for index in range(9):
countyDict[county][index+1]= {'rate' :CountyRateDict[county][index],
'count':CountyCDict[county][index],
'pop' :CountyPopDict[county][index]
}
for i in countyDict['BE'][3]['pop']:
print (i)
return countyDict
# to use countyDict
# countyDict[county][year][ 'rate' OR count' OR 'pop'] return list of weeks
countyDict = makeCountyDict(counties,CountyRateDict,CountyCDict,CountyPopDict)
#%%
"""
#Interactive Defining of variables
County = input("Which County would you like to predict?\n")
if County.upper() in CountyRateDict.keys():
print("Predicting for ", County.upper())
else:
print("Invalid entry. Please try again")
County = input("Which County would you like to predict?\n")
County = County.upper()
PredRates = int(input("How many weeks would you like to use as predictors? (integer between 3 and 10):\n"))
if 3<= PredRates <= 10:
print("Number of pridictor rates =", PredRates)
else:
print("Invalid entry, please try again")
PredRates = int(input("How many weeks would you like to use as predictors? (integer between 3 and 10):\n"))
N = int(input("Enter the week you want to predict (integer between number of predictors and 441):\n"))
if PredRates <= N <= 441:
print("N=", N)
else:
print("Invalid. Please try again")
N = int(input("Enter the week you want to predict (integer between number of predictors and 441):\n"))
n = int(input("How many obsetvations would you like to use? (integer less than or equal to N-number of predictors):\n"))
if n <= N-PredRates:
print("n=", n)
else:
print("Invalid entry, please try again")
n = int(input("How many obsetvations would you like to use? (integer less than or equal to N-number of predictors):\n"))
"""
###################################
# apply another algorithm
###################################
#
##path = r"C:\Users\Bill Griffin\flu-data-linear-regression"
####################################
## write the production to files
####################################
#pathprint = path.strip("initial_flue.csv") + "\County Flu Forecasts Weeks " + str(N) + " to 441.csv"
#
# # change this path as applicable
#
####################### Iterate through counties, weeks, build .csv file ... output to console for S&G's
#
#
#with open(pathprint, mode = 'w') as output_file:
#
# output_writer = csv.writer(output_file, dialect = 'excel')
#
# for County in counties:
#
# N = weekRequest # start each county at the week requested
# n = N - PredRates
#
# while N < 442: # we want the last yhat to be week 441, which is last year we
# # have a y
#
# rates=CountyRateDict1[County] # load in the rate column for interated county
#
# # Execute functions -- Solve lineq
# yhat,yObserved, delta, betahat = MatrixSolve(N,n,q,PredRates,rates,County,CountyRateDict1)
#
# output_writer.writerow([County, N, float(yhat), yObserved, delta])
#
# # send to console as well
#
# print("Writing to file")
# print( '\r' + County," Week = ", N, "yhat=", yhat,"y=", yObserved, 'delta = ', delta, end='')
#
# # increment N, n
# N += 1
# n += 1
#
#
#%%