-
Notifications
You must be signed in to change notification settings - Fork 1
/
ml_predictFlowMetrics.py
241 lines (186 loc) · 6.54 KB
/
ml_predictFlowMetrics.py
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
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.tree import DecisionTreeRegressor
import math
import random
from scipy import stats
import matplotlib.pyplot as plt
import time
removeSparse = True
copyNo = 0
df = pd.read_csv("universallyAligned_powers.csv")
df.index = df[df.columns[0]]
df = df.drop(df.columns[0], axis=1)
df = df.transpose()
df["grdc_no"] = df.index
print(df)
#quit()
cdf = pd.read_csv("alldata.csv")
# keep track of which columns have a lot of NaN values
tooSparseColumns = []
for column in cdf:
num = np.sum(cdf[column].isna())
if num > 500:
tooSparseColumns.append(column)
#print(str(column) + " " + str(num))
for column in cdf:
column1 = column.replace("_","")
if type(cdf[column][0]) == type("string") and column1.isalpha():
codes, uniques = pd.factorize(cdf[column])
cdf[column] = codes
print(cdf[column])
#quit()
xcats = []
for cat in cdf["grdc_no"]:
try:
xcats.append("X" + str(int(cat)))
except:
xcats.append(None)
cdf["grdc_no"] = xcats
df = df.merge(cdf, on="grdc_no")
print(df)
for i in range(len(df.columns)):
print(str(i) + " " + str(df.columns[i]))
# normalize each column
#predictors = list(df.columns[1299:1413])
predictors = list(df.columns[0:1101]) # just frequency decomposition columns
def splitTestTrain(data, seed1, seed2):
random.seed(seed1 + seed2 + copyNo) # ensure different selections between periods, but consistent between runs
nSamples, nFeatures = data.shape
eightyPercent = int(0.8 * nSamples)
indices = list(range(nSamples))
random.shuffle(indices)
train = indices[:eightyPercent]
test = indices[eightyPercent:]
return data[test], data[train]
def splitIntoXY(data):
y = data[:,0]
x = data[:,1:]
return x, y
def convertNA(df):
#print("*****************before")
#print(df)
for column in df.columns:
newCol = []
for item in df[column]:
try:
item = float(item)
if math.isnan(item) or (item == float('inf')) or (item == float('-inf')):
newCol.append(np.nan)
else:
newCol.append(float(item))
except:
newCol.append(None)
df[column] = newCol
#print("after")
#print(df)
return df
shortDict = {
"flow_metric":[],
"period":[],
"importance":[],
"trial_no":[],
"model_type":[]
}
wideDict = {"flow_metric":[],"model_type":[]}
rSquaredDict = {
"trialNo":[],
"flow_metric":[],
"n_total":[],
"n_validation":[],
"n_test":[],
"r_squared":[],
"p_value":[],
"std_err":[],
"slope":[],
"intercept":[],
"model_type":[]
}
secondsTotal = time.time()
#seconds1 = time.time()
#seconds2 = time.time()
df = df - df.mean()
df = df / df.std()
for trialNo in range(0,60):
for i in range(1109,1298):
seconds1 = time.time()
try:
# drop everything except the X and Y values
flowMetric = df.columns[i]
keepers = [flowMetric] + predictors
df1 = df.filter(keepers)
df1 = convertNA(df1)
df1 = df1.dropna()
print("trial no: " + str(trialNo) + ". col: " + str(df.columns[i]))
dataMatrix = df1.to_numpy()
# split into train, test
test, train = splitTestTrain(dataMatrix, i, trialNo)
# split into X and Y
testX, testY = splitIntoXY(test)
trainX, trainY = splitIntoXY(train)
if trialNo % 2 == 0:
model = GradientBoostingRegressor(n_estimators = 100)
modelType = "GBR"
elif trialNo % 2 == 1:
model = RandomForestRegressor(n_estimators = 100)
modelType = "RFR"
model.fit(trainX, trainY)
importances = model.feature_importances_
wideDict["flow_metric"].append(flowMetric)
wideDict["model_type"].append(modelType)
for j in range(len(importances)):
importance = importances[j]
feature = predictors[j]
if feature not in wideDict.keys():
wideDict[feature] = []
wideDict[feature].append(importance)
for j in range(len(importances)):
importance = importances[j]
feature = predictors[j]
shortDict["flow_metric"].append(flowMetric)
shortDict["period"].append(feature)
shortDict["importance"].append(importance)
shortDict["trial_no"].append(trialNo)
shortDict["model_type"].append(modelType)
yHat = model.predict(testX)
slope, intercept, rValue, pValue, stdErr = stats.linregress(yHat,testY)
rSquared = rValue * rValue
rSquaredDict["trialNo"].append(trialNo)
rSquaredDict["flow_metric"].append(flowMetric)
rSquaredDict["r_squared"].append(rSquared)
rSquaredDict["p_value"].append(pValue)
rSquaredDict["std_err"].append(stdErr)
rSquaredDict["slope"].append(slope)
rSquaredDict["intercept"].append(intercept)
rSquaredDict["model_type"].append(modelType)
rSquaredDict["n_total"].append(df1.shape[0])
rSquaredDict["n_validation"].append(test.shape[0])
rSquaredDict["n_test"].append(train.shape[0])
print("r_squared: " + str(rSquared))
except:
print("error")
seconds2 = time.time()
print("time")
print(seconds2 - seconds1)
shortDf = pd.DataFrame.from_dict(shortDict)
wideDf = pd.DataFrame.from_dict(wideDict)
rSquaredDf = pd.DataFrame.from_dict(rSquaredDict)
shortDf.to_csv("ml_predict_flow_metrics_feature_importances_short" + str(copyNo) + ".csv")
wideDf.to_csv("ml_predict_flow_metrics_feature_importances_wide" + str(copyNo) + ".csv")
rSquaredDf.to_csv("ml_predict_flow_metrics_r_squared" + str(copyNo) + ".csv")
print(secondsTotal)
#1299 - 1492
#for i in range(len(df.columns)):
# print(str(i) + " " + str(df.columns[i]))
#quit()
# for each frequency
# grab all the predictors
# split into test/train
# train
# test
# grab importances
print(df)
print(cdf)