-
Notifications
You must be signed in to change notification settings - Fork 0
/
ml_analysis.py
317 lines (269 loc) · 12.8 KB
/
ml_analysis.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
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
import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder, normalize
from sklearn.model_selection import train_test_split
from sklearn import dummy, linear_model, svm
from sklearn.linear_model import LinearRegression
from sklearn.svm import SVR
from sklearn.metrics import mean_squared_error
from scipy import stats
from sklearn.preprocessing import PolynomialFeatures, StandardScaler
from sklearn.feature_selection import SelectKBest, f_regression, mutual_info_regression
from sklearn.neural_network import MLPRegressor
all_features = ['rural_urban', 'urban_influence', 'high_school_degree_percent',
'politics', 'percent_pov_child', 'percent_pov_all', 'employment_rate',
'med_household_income_2017', 'white_county', 'white_school', 'grade_span',
'male_percent', 'num_free_reduced_meals', 'total_percent_absences',
'low_income_absence_percent', 'religious_exempt',
'personal_exempt', 'medical_exempt', 'school_district']
multi_regression_features_shortened = ['urban_influence',
'high_school_degree_percent',
'politics',
'percent_pov_child',
'employment_rate',
'med_household_income_2017',
'white_county',
'white_school',
'grade_span',
'num_free_reduced_meals',
'total_percent_absences',
'low_income_absence_percent',
'religious_exempt',
'personal_exempt',
'medical_exempt',
'school_district']
labels = ['immunization_rate']
def preprocess_features():
df = pd.read_csv('./cleaned_data_files/complete_with_school_cleaned.csv')
df['med_household_income_2017'] = df['med_household_income_2017'].str.replace('$','')
string_columns = ['high_school', 'clinton', 'trump', 'med_household_income_2017',
'employed_2015', 'labor_total_2015', 'labor_total_2016',
'employed_2016']
for column in string_columns:
df[column] = pd.to_numeric(df[column].str.replace(',',''))
percent_columns = ['total_percent_absences', 'low_income_absence_percent']
for column in percent_columns:
df[column] = pd.to_numeric(df[column].str.replace('%',''))
df['high_school_degree_percent'] = (1 - (df['high_school'] / df['totalPop'])).multiply(100).round(1)
df['politics'] = (df['clinton'] / (df['clinton'] + df['trump'])).round(2)
# corresponding year
df['employment_rate'] = (df['employed_2015'] / df['labor_total_2015']).multiply(100).round(1)
df.loc[df['school_year'] == 2016, 'employment_rate'] = (df['employed_2016'] /
df['labor_total_2016']).multiply(100).round(1)
df.loc[df['school_year'] == 2017, 'employment_rate'] = (df['employed_2017'] /
df['labor_total_2017']).multiply(100).round(1)
df['white_county'] = df['white']
df['white_school'] = (df['school_white'] / df['total_enrollment']).multiply(100).round(1)
df['grade_span'] = df['end_grade'] - df['start_grade']
df['male_percent'] = (df['male'] / df['total_enrollment']).multiply(100).round(1)
label_encoder = LabelEncoder()
df['school_district'] = np.array(label_encoder.fit_transform(df['school_district'].values))
df['immunization_rate'] = (df['all_immunizations'] / df['k12_enrollment']).multiply(100).round(1)
return df[all_features + labels]
def feature_selection(df, method, k):
X = df[all_features].values
y = np.ravel(df[labels].values)
selector = SelectKBest(method, k=k)
X_selected = selector.fit_transform(X, y)
feature_indices = selector.get_support(indices=True)
selected_features = []
for index in feature_indices:
selected_features.append(all_features[index])
print(selected_features)
return X_selected
def singleLinearRegression(df):
for feature in all_features:
single_r_square = 0
single_train_mse = 0
single_test_mse = 0
for i in range(5):
X = df[feature].values
X = np.reshape(X, (-1,1))
y = df[labels].values
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.1,
shuffle=True
)
lm = LinearRegression().fit(X_train, y_train)
train_r_squared = lm.score(X_train, y_train)
train_mse = mean_squared_error(y_train, lm.predict(X_train))
test_mse = mean_squared_error(y_test, lm.predict(X_test))
single_r_square += train_r_squared
single_train_mse += train_mse
single_test_mse += test_mse
print(feature + " results:")
print('Training R-Squared: %f' % (single_r_square / 5))
print('Training MSE: %f' % (single_train_mse / 5))
print('Testing MSE: %f' % (single_test_mse / 5))
def getMultipleLinearRegressionP(df):
X = df[all_features].values
y = df[labels].values
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.1,
shuffle=True
)
lm = LinearRegression().fit(X_train, y_train)
params = np.append(lm.intercept_,lm.coef_)
predictions = lm.predict(X_train)
newX = np.append(np.ones((len(X_train),1)), X_train, axis=1)
MSE = (sum((y_train-predictions)**2))/(len(newX)-len(newX[0]))
var_b = MSE*(np.linalg.inv(np.dot(newX.T,newX)).diagonal())
sd_b = np.sqrt(var_b)
ts_b = params/ sd_b
p_values =[2*(1-stats.t.cdf(np.abs(i),(len(newX)-1))) for i in ts_b]
myDF3 = pd.DataFrame()
myDF3["Coefficients"],myDF3["Standard Errors"],myDF3["t values"],myDF3["Probabilites"] = [params,sd_b,ts_b,p_values]
print(myDF3)
def multipleLinearRegression(X, y):
multi_r_sqaure = 0
multi_train_mse = 0
multi_test_mse = 0
for i in range(20):
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.1,
shuffle=True
)
lm = LinearRegression().fit(X_train, y_train)
train_r_squared = lm.score(X_train, y_train)
train_mse = mean_squared_error(y_train, lm.predict(X_train))
test_mse = mean_squared_error(y_test, lm.predict(X_test))
multi_r_sqaure += train_r_squared
multi_train_mse += train_mse
multi_test_mse += test_mse
print('Training R-Squared: %f' % (multi_r_sqaure / 20))
print('Training MSE: %f' % (multi_train_mse / 20))
print('Testing MSE: %f' % (multi_test_mse / 20))
def svm(X, y, kernel):
if kernel == 'poly':
# print('normalizing data')
X = np.array([(x - min(x)) / (max(x) - min(x)) for x in X])
y = y / 100
svm_r_sqaure = 0
svm_train_mse = 0
svm_test_mse = 0
for i in range(3):
X_train, X_test, y_train, y_test = train_test_split(
X,
y.ravel(),
test_size=0.1,
shuffle=True
)
clf = SVR(kernel=kernel,
degree=3,
gamma='auto').fit(X_train, y_train)
train_r_squared = clf.score(X_train, y_train)
train_mse = mean_squared_error(y_train, clf.predict(X_train))
test_mse = mean_squared_error(y_test, clf.predict(X_test))
svm_r_sqaure += train_r_squared
svm_train_mse += train_mse
svm_test_mse += test_mse
print('Training R-Squared: %f' % (svm_r_sqaure / 3))
print('Training MSE: %f' % (svm_train_mse / 3))
print('Testing MSE: %f' % (svm_test_mse / 3))
def neural_network(X, y):
nn_r_sqaure = 0
nn_train_mse = 0
nn_test_mse = 0
for i in range(5):
X_train, X_test, y_train, y_test = train_test_split(
X,
y.ravel(),
test_size=0.1,
shuffle=True
)
clf = MLPRegressor(hidden_layer_sizes=(8,),
activation="tanh",
alpha=0.001,
learning_rate_init=0.001,
max_iter=2000).fit(X_train, y_train)
train_r_squared = clf.score(X_train, y_train)
train_mse = mean_squared_error(y_train, clf.predict(X_train))
test_mse = mean_squared_error(y_test, clf.predict(X_test))
nn_r_sqaure += train_r_squared
nn_train_mse += train_mse
nn_test_mse += test_mse
print('Training R-Squared: %f' % (nn_r_sqaure / 5))
print('Training MSE: %f' % (nn_train_mse / 5))
print('Testing MSE: %f' % (nn_test_mse / 5))
def baseLine(df):
dummy_train_r2 = 0
dummy_train_mse = 0
dummy_test_mse = 0
for i in range(5):
base_line_predictor = dummy.DummyRegressor(strategy="mean")
X = df[all_features].values
y = df[labels].values
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.1,
shuffle=True
)
base_line_predictor.fit(X_train, y_train)
train_r_squared = base_line_predictor.score(X_train, y_train)
train_mse = mean_squared_error(y_train, base_line_predictor.predict(X_train))
test_mse = mean_squared_error(y_test, base_line_predictor.predict(X_test))
dummy_train_r2 += train_r_squared
dummy_train_mse += train_mse
dummy_test_mse += test_mse
print('Training R-Squared: %f' % (dummy_train_r2 / 5))
print('Training MSE: %f' % (dummy_train_mse / 5))
print('Testing MSE: %f' % (dummy_test_mse / 5))
def main():
df = preprocess_features()
# baseLine(df)
# print('Top 1 Features selected by F-regression:')
# feature_selection(df, f_regression, 1)
# print('Top 3 Features selected by F-regression:')
# feature_selection(df, f_regression, 3)
# print('Top 5 Features selected by F-regression:')
f_regression_X = feature_selection(df, f_regression, 5)
# print('Top 1 Features selected by mutual info regression:')
# feature_selection(df, mutual_info_regression, 1)
# print('Top 3 Features selected by mutual info regression:')
# feature_selection(df, mutual_info_regression, 3)
# print('Top 5 Features selected by mutual info regression:')
mutual_info_X = feature_selection(df, mutual_info_regression, 5)
# singleLinearRegression(df)
# getMultipleLinearRegressionP(df)
# print('Multi-linear regression on all features:')
# multipleLinearRegression(df[all_features].values, df[labels].values)
# print('Multi-linear regression on features selected by p-values:')
# multipleLinearRegression(df[multi_regression_features_shortened].values, df[labels].values)
# print('Multi-linear regression on features selected by sklearn (f score):')
# multipleLinearRegression(f_regression_X, df[labels].values)
# print('Multi-linear regression on features selected by sklearn (mutual info):')
# multipleLinearRegression(mutual_info_X, df[labels].values)
# print('SVM (rbf) regression on all features:')
# svm(df[all_features].values, df[labels].values, 'rbf')
# print('SVM (rbf) regression on features selected by p-values:')
# svm(df[multi_regression_features_shortened].values, df[labels].values, 'rbf')
# print('SVM (rbf) regression on features selected by sklearn (f score):')
# svm(f_regression_X, df[labels].values, 'rbf')
# print('SVM (rbf) regression on features selected by sklearn (mutual info):')
# svm(mutual_info_X, df[labels].values, 'rbf')
# print('Neural Network regression on all features:')
# neural_network(df[all_features].values, df[labels].values)
# print('Neural Network regression on features selected by p-values:')
# neural_network(df[multi_regression_features_shortened].values, df[labels].values)
# print('Neural Network regression on features selected by sklearn (f score):')
# neural_network(f_regression_X, df[labels].values)
# print('Neural Network regression on features selected by sklearn (mutual info):')
# neural_network(mutual_info_X, df[labels].values)
#
# print('SVM (poly) regression on all features:')
# svm(df[all_features].values, df[labels].values, 'poly')
# print('SVM (poly) regression on features selected by p-values:')
# svm(df[multi_regression_features_shortened].values, df[labels].values, 'poly')
# print('SVM (poly) regression on features selected by sklearn (f score):')
# svm(f_regression_X, df[labels].values, 'poly')
# print('SVM (poly) regression on features selected by sklearn (mutual info):')
# svm(mutual_info_X, df[labels].values, 'poly')
if __name__ == "__main__":
main()