-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodeling.py
186 lines (148 loc) · 7.84 KB
/
modeling.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
import numpy as np
import pandas as pd
import prepare as pr
import matplotlib.pyplot as plt
import seaborn as sns
import explore as ex
import sklearn.preprocessing
from sklearn.metrics import mean_squared_error
from sklearn.linear_model import LinearRegression
def model_results_table(df):
'''
this function will calculate baseline RMSE for train/validate and generate the linear regression models/preds/RMSE and show the difference
'''
train, val, test= ex.tts(df)
target=['english_1', 'english_2', 'algebra','biology', 'history']
subject=['English 1', 'English 2', 'Algebra', 'Biology', 'U.S. History']
rmse_base_t=[]
rmse_base_v=[]
rmse_train=[]
rmse_val=[]
rmse_test=[]
X_train=train.drop(columns=['biology', 'english_1', 'english_2', 'algebra', 'history', 'school_id'])
X_val=val.drop(columns=['biology', 'english_1', 'english_2', 'algebra', 'history', 'school_id'])
X_test=test.drop(columns=['biology', 'english_1', 'english_2', 'algebra', 'history', 'school_id'])
lm=LinearRegression()
for t in target:
y_train=pd.DataFrame(train[t])
y_val=pd.DataFrame(val[t])
y_test=pd.DataFrame(test[t])
lm.fit(X_train, y_train[t])
pred_t= lm.predict(X_train)
pred_v= lm.predict(X_val)
pred_test= lm.predict(X_test)
train['Baseline Mean'] = train[t].mean()
rmse_base_t.append(mean_squared_error(train[t], train['Baseline Mean'], squared=False))
val['Baseline Mean'] = val[t].mean()
rmse_base_v.append(mean_squared_error(val[t], val['Baseline Mean'], squared=False))
rmse_train.append(mean_squared_error(y_train[t], pred_t, squared=False))
rmse_val.append(mean_squared_error(y_val[t], pred_v, squared=False))
rmse_test.append(mean_squared_error(y_test[t], pred_test, squared=False))
results=pd.DataFrame(index=subject, data= {
'Train Baseline RMSE': rmse_base_t,
'Validate Baseline RMSE': rmse_base_v,
'Train Model RMSE': rmse_train,
'Validate Model RMSE': rmse_val,
'Test RMSE': rmse_test
})
results['Model Difference']=results['Train Model RMSE']- results['Validate Model RMSE']
diff = results['Validate Baseline RMSE'] - results['Validate Model RMSE']
reduction = diff / results['Validate Baseline RMSE']
pct_improvement = round(reduction*100 ,2)
pct_improvement
results['Validate Improvement'] = pct_improvement
results['Validate Improvement'] = results['Validate Improvement'].astype(str) + '%'
results.insert(6, 'Test RMSE', results.pop('Test RMSE'))
results=results.style.set_properties(**{'background-color': 'yellow'}, subset=['Test RMSE'])
return results
################################################################################################
def model_results_plot(df):
'''
this function will calculate baseline RMSE for train/validate and generate the linear regression models/preds/RMSE and show the difference
'''
train, val, test= ex.tts(df)
target=['english_1', 'english_2', 'algebra','biology', 'history']
subject=['English 1', 'English 2', 'Algebra', 'Biology', 'U.S. History']
rmse_base_t=[]
rmse_base_v=[]
rmse_train=[]
rmse_val=[]
rmse_test=[]
X_train=train.drop(columns=['biology', 'english_1', 'english_2', 'algebra', 'history', 'school_id'])
X_val=val.drop(columns=['biology', 'english_1', 'english_2', 'algebra', 'history', 'school_id'])
X_test=test.drop(columns=['biology', 'english_1', 'english_2', 'algebra', 'history', 'school_id'])
lm=LinearRegression()
for t in target:
y_train=pd.DataFrame(train[t])
y_val=pd.DataFrame(val[t])
y_test=pd.DataFrame(test[t])
lm.fit(X_train, y_train[t])
pred_t= lm.predict(X_train)
pred_v= lm.predict(X_val)
pred_test= lm.predict(X_test)
train['Baseline Mean'] = train[t].mean()
rmse_base_t.append(round(mean_squared_error(train[t], train['Baseline Mean'], squared=False),2))
val['Baseline Mean'] = val[t].mean()
rmse_base_v.append(mean_squared_error(val[t], val['Baseline Mean'], squared=False))
rmse_train.append(mean_squared_error(y_train[t], pred_t, squared=False))
rmse_val.append(mean_squared_error(y_val[t], pred_v, squared=False))
rmse_test.append(round(mean_squared_error(y_test[t], pred_test, squared=False),2))
results=pd.DataFrame(index=subject, data= {
'Train Baseline RMSE': rmse_base_t,
'Validate Baseline RMSE': rmse_base_v,
'Train Model RMSE': rmse_train,
'Validate Model RMSE': rmse_val,
'Test RMSE': rmse_test
})
results['Model Difference']=results['Train Model RMSE']- results['Validate Model RMSE']
diff = results['Validate Baseline RMSE'] - results['Validate Model RMSE']
reduction = diff / results['Validate Baseline RMSE']
pct_improvement = round(reduction*100 ,2)
pct_improvement
results['Validate Improvement'] = pct_improvement
results['Validate Improvement'] = results['Validate Improvement'].astype(str) + '%'
results.insert(6, 'Test RMSE', results.pop('Test RMSE'))
return results
################################################################################################
def modeling_visual(df):
ma=model_results_plot(df)
plt.figure(figsize=(10,5))
X = ['English 1', 'English 2', 'Algebra', 'Biology', 'U.S. History']
X_axis = np.arange(len(X))
fig, ax = plt.subplots(layout='constrained')
ax.bar(X_axis[0] - 0.3, ma['Train Baseline RMSE'][0], 0.2,
label = 'Baseline RMSE', color=['blue'], ec='black')
ax.bar(X_axis[0] - 0.1, ma['Train Model RMSE'][0], 0.2,
label = 'Train RMSE', color=['orange'], ec='black')
ax.bar(X_axis[0] + 0.1, ma['Validate Model RMSE'][0], 0.2,
label = 'Validate RMSE', color=['rebeccapurple'], ec='black')
ax.bar(X_axis[0] + 0.3, ma['Test RMSE'][0], 0.2,
label = 'Test RMSE', color=['green'], ec='black')
ax.bar(X_axis[1] - 0.3, ma['Train Baseline RMSE'][1], 0.2, color=['blue'], ec='black')
ax.bar(X_axis[1] - 0.1, ma['Train Model RMSE'][1], 0.2, color=['orange'], ec='black')
ax.bar(X_axis[1] + 0.1, ma['Validate Model RMSE'][1], 0.2, color=['rebeccapurple'], ec='black')
ax.bar(X_axis[1] + 0.3, ma['Test RMSE'][1], 0.2, color=['green'], ec='black')
ax.bar(X_axis[2] - 0.3, ma['Train Baseline RMSE'][2], 0.2, color=['blue'], ec='black')
ax.bar(X_axis[2] - 0.1, ma['Train Model RMSE'][2], 0.2, color=['orange'], ec='black')
ax.bar(X_axis[2] + 0.1, ma['Validate Model RMSE'][2], 0.2, color=['rebeccapurple'], ec='black')
ax.bar(X_axis[2] + 0.3, ma['Test RMSE'][2], 0.2, color=['green'], ec='black')
ax.bar(X_axis[3] - 0.3, ma['Train Baseline RMSE'][3], 0.2, color=['blue'], ec='black')
ax.bar(X_axis[3] - 0.1, ma['Train Model RMSE'][3], 0.2, color=['orange'], ec='black')
ax.bar(X_axis[3] + 0.1, ma['Validate Model RMSE'][3], 0.2, color=['rebeccapurple'], ec='black')
ax.bar(X_axis[3] + 0.3, ma['Test RMSE'][3], 0.2, color=['green'], ec='black')
ax.bar(X_axis[4] - 0.3, ma['Train Baseline RMSE'][4], 0.2, color=['blue'], ec='black')
ax.bar(X_axis[4] - 0.1, ma['Train Model RMSE'][4], 0.2, color=['orange'], ec='black')
ax.bar(X_axis[4] + 0.1, ma['Validate Model RMSE'][4], 0.2, color=['rebeccapurple'], ec='black')
ax.bar(X_axis[4] + 0.3, ma['Test RMSE'][4], 0.2, color=['green'], ec='black')
container = [0,3,4,7,8,11,12,15,16,19]
for c in container:
ax.bar_label(ax.containers[c])
plt.xticks(X_axis, X)
plt.xlabel("Subject")
plt.ylabel("RMSE")
plt.title("Linear Regression Model Results")
plt.ylim(0, 25)
plt.grid(True, alpha=0.3, linestyle='--', axis='y')
leg = plt.legend(title="RMSE")
leg._legend_box.align = "left"
plt.show()