-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
291 lines (173 loc) · 6.37 KB
/
models.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
#!/usr/bin/env python
# coding: utf-8
# In[2]:
import numpy as np
import pandas as pd
from sklearn.linear_model import Lasso, LassoCV, Ridge, RidgeCV
from sklearn.model_selection import train_test_split, GridSearchCV, RandomizedSearchCV
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier
from sklearn import preprocessing, metrics
from sklearn.svm import SVR
from sklearn.feature_selection import SelectFromModel
import matplotlib.pyplot as plt
import seaborn as sns
from RegscorePy import *
from statsmodels.compat import lzip
import matplotlib.gridspec as gridspec
# In[3]:
# load data
df = pd.read_csv('AUT_data.csv', encoding='utf-8')
df = df.dropna()
df.rename(columns={'rareness':'1/freq'}, inplace=True)
# In[4]:
X = df.iloc[:, 7:-1]
y = df.iloc[:, -1]
X = preprocessing.scale(X)
X_train, X_test , y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state=1)
# In[5]:
# cross-validation to find alpha
lassocv = LassoCV(alphas = None, cv = 10, max_iter = 100000, normalize = True)
lassocv.fit(X_train, y_train)
#lasso regression
lasso = Lasso(max_iter = 10000, normalize=True)
lasso.set_params(alpha=lassocv.alpha_)
#print("Alpha =", lassocv.alpha_)
lasso_model = lasso.fit(X_train, y_train)
print("R2 = ", r2_score(y_test, lasso.predict(X_test)))
#np.corrcoef(lasso.predict(X_test), df.iloc[y_test.index, -1])
# In[5]:
r2_score(y_train, lasso.predict(X_train))
# In[19]:
features = df.columns[7:-1]
coef = lasso.coef_
top10 = sorted(zip(features, abs(coef)), key=lambda t: t[1], reverse=True)[:10]
plt.bar(range(len(top10)), [val[1] for val in top10], align='center')
plt.xticks(range(len(top10)), [val[0] for val in top10])
plt.xticks(rotation=70)
plt.title("Absolute Coefficient of LASSO regression")
plt.savefig('lasso.pdf', bbox_inches='tight')
# In[6]:
x, y = pd.Series(lasso.predict(X_test), name="fitted value"), pd.Series(y_test-lasso.predict(X_test), name="residual")
ax = sns.regplot(x,y)
plt.title("Residuals of LASSO Regression")
plt.savefig('lasso_residuals.pdf')
# In[7]:
x, y = pd.Series(lasso.predict(X_test), name="Predictions"), pd.Series(y_test, name="Mean Expert Ratings")
ax = sns.regplot(x,y)
plt.title("LASSO Regression Performance")
plt.savefig('lasso.png')
# In[8]:
alphas = 10**np.linspace(6,-2,50)*0.5
ridgecv = RidgeCV(alphas=alphas, normalize=True)
ridgecv.fit(X_train, y_train)
#print("Alpha=", ridgecv.alpha_)
ridge = Ridge(alpha=ridgecv.alpha_, normalize=True)
ridge.fit(X_train, y_train)
#print("mse = ",mean_squared_error(y_test, ridge.predict(X_test)))
print("R2 = ", r2_score(y_test, ridge.predict(X_test)))
np.corrcoef(ridge.predict(X_test), df.iloc[y_test.index, -1])
# In[9]:
r2_score(y_train, ridge.predict(X_train))
# In[18]:
features = df.columns[7:-1]
coef = ridge.coef_
top10 = sorted(zip(features, abs(coef)), key=lambda t: t[1], reverse=True)[:10]
plt.bar(range(len(top10)), [val[1] for val in top10], align='center')
plt.xticks(range(len(top10)), [val[0] for val in top10])
plt.xticks(rotation=70)
plt.title("Absolute Coefficients of Ridge Regression")
plt.savefig('ridge.pdf', bbox_inches='tight')
# In[10]:
x, y = pd.Series(ridge.predict(X_test), name="fitted value"), pd.Series(y_test-ridge.predict(X_test), name="residual")
ax = sns.regplot(x,y)
plt.title("Residuals of Ridge Regression")
plt.savefig('ridge_residuals.pdf')
# In[12]:
x, y = pd.Series(ridge.predict(X_test), name="Predictions"), pd.Series(y_test, name="Mean Expert Ratings")
ax = sns.regplot(x,y)
plt.title("Ridge Regression Performance")
plt.savefig('ridge.png')
# In[13]:
# Create the parameter grid based on the results of random search
param_grid = {
'max_depth': [5, 7, 10],
'n_estimators': [800, 1000],
'max_features': ['sqrt', 'log2'],
# 'min_samples_leaf': [2, 3],
# 'min_samples_split': [2, 5],
}
# Create a based model
rf = RandomForestRegressor()
# Instantiate the grid search model
grid_search = GridSearchCV(estimator = rf, param_grid = param_grid,
cv=5, n_jobs=-1, verbose=2)
# In[14]:
# Fit the grid search to the data
grid_search.fit(X_train, y_train)
best_grid = grid_search.best_estimator_
# In[15]:
best_grid
# In[16]:
best_grid.score(X_train, y_train)
# In[17]:
best_grid.score(X_test, y_test)
# In[9]:
features = df.columns[7:-1]
coef = best_grid.feature_importances_
top10 = sorted(zip(features, abs(coef)), key=lambda t: t[1], reverse=True)[:10]
plt.bar(range(len(top10)), [val[1] for val in top10], align='center')
plt.xticks(range(len(top10)), [val[0] for val in top10])
plt.xticks(rotation=70)
plt.title("Feature Importance of Random Forest")
plt.savefig('rf.pdf', bbox_inches='tight')
# In[56]:
df0 = df.sample(n=10, random_state=19)
df0 = df0[['cleaned_response', 'vec_248', 'vec_281', 'mean']]
p1=sns.regplot(data=df0, x="vec_248", y="vec_281", fit_reg=False, marker="o", color="skyblue", scatter_kws={'s':400})
# add annotations one by one with a loop
for i in range(0,df0.shape[0]):
row = df0.iloc[i]
p1.text(
row.vec_248+0.01, row.vec_281+0.02, row.cleaned_response,
horizontalalignment='right', size='small', color='black')
fig = p1.get_figure()
fig.savefig("vectors.png")
# In[19]:
x, y = pd.Series(best_grid.predict(X_test), name="Predictions"), pd.Series(y_test, name="Mean Expert Ratings")
ax = sns.regplot(x,y)
plt.title("Performance Random Forest Regressor")
plt.savefig('random forest.png')
# In[54]:
cols = [x[0] for x in top10]
cols.append('mean')
df1 = df[cols]
sns.heatmap(df1.corr(), annot=True)
# In[283]:
df.head()
# In[ ]:
df1 = df.drop(columns=['wup_similarity', 'path_similarity'])
# In[317]:
X1 = df1.iloc[:, 7:-1]
y1 = df1.iloc[:, -1]
X1 = preprocessing.scale(X1)
#y[y>=0] = np.log(y[y>=0])
X1_train, X1_test , y1_train, y1_test = train_test_split(X1, y1, test_size = 0.3, random_state=1)
# Fit the grid search to the data
grid_search.fit(X1_train, y1_train)
best_grid = grid_search.best_estimator_
# In[318]:
best_grid.score(X1_test, y1_test)
# In[319]:
df2 = df.drop(columns=['freq', '1/freq'])
X2 = df2.iloc[:, 7:-1]
y2 = df2.iloc[:, -1]
X2 = preprocessing.scale(X2)
#y[y>=0] = np.log(y[y>=0])
X2_train, X2_test , y2_train, y2_test = train_test_split(X2, y2, test_size = 0.3, random_state=1)
# Fit the grid search to the data
grid_search.fit(X2_train, y2_train)
best_grid = grid_search.best_estimator_
best_grid.score(X2_test, y2_test)
# In[322]:
df2.head()