-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcal_model_param.py
220 lines (184 loc) · 9.06 KB
/
cal_model_param.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
import time
import plotly.graph_objects as go
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn import metrics
import plotly.express as px
import joblib
plt.style.use('seaborn-v0_8-whitegrid')
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
plt.rcParams['figure.figsize'] = (8.0, 6.0)
plt.rcParams['figure.dpi'] = 300
def fit_recall_halflife(raw):
print('fit_recall_halflife_dhp')
raw['log_hinc'] = raw['halflife_increase'].map(lambda x: np.log(x - 1))
raw['log_h'] = raw['last_halflife'].map(lambda x: np.log(x))
raw['fi'] = raw['last_p_recall'].map(lambda x: 1 - x)
raw['log_fi'] = raw['last_p_recall'].map(lambda x: np.log(1 - x))
raw['log_d'] = raw['d'].map(lambda x: np.log(x))
raw['log_delta_h'] = np.log(raw['halflife'] - raw['last_halflife'])
corr = raw.corr()
print(corr)
X = raw[['log_d', 'log_h', 'log_fi']]
Y = raw[['log_hinc']]
lr = LinearRegression()
lr.fit(X, Y, sample_weight=raw['group_cnt'])
print('Intercept: ', lr.intercept_)
print('Slope: ', lr.coef_)
y_pred = (np.exp(lr.predict(X)) + 1) * raw[['last_halflife']].values
Y = (np.exp(Y) + 1) * raw[['last_halflife']].values
print("Explained Variance Score: ",
metrics.explained_variance_score(Y, y_pred, sample_weight=raw['group_cnt']))
print('Mean Absolute Error:',
metrics.mean_absolute_error(Y, y_pred, sample_weight=raw['group_cnt']))
print('Mean Squared Error:',
metrics.mean_squared_error(Y, y_pred, sample_weight=raw['group_cnt']))
print('Root Mean Squared Error:',
np.sqrt(metrics.mean_squared_error(Y, y_pred, sample_weight=raw['group_cnt'])))
print('Mean Absolute Percentage Error:',
np.sqrt(metrics.mean_absolute_percentage_error(Y, y_pred, sample_weight=raw['group_cnt'])))
raw['predict_halflife_dhp'] = y_pred
print('fit_recall_halflife_hlr')
lr = joblib.load('fit_result/clf.pkl')
X = raw[['right', 'wrong', 'd']]
Y = raw[['log_halflife']]
print('Intercept: ', lr.intercept_)
print('Slope: ', lr.coef_)
y_pred = np.exp(lr.predict(X))
Y = np.exp(Y)
print("Explained Variance Score: ",
metrics.explained_variance_score(Y, y_pred, sample_weight=raw['group_cnt']))
print('Mean Absolute Error:',
metrics.mean_absolute_error(Y, y_pred, sample_weight=raw['group_cnt']))
print('Mean Squared Error:',
metrics.mean_squared_error(Y, y_pred, sample_weight=raw['group_cnt']))
print('Root Mean Squared Error:',
np.sqrt(metrics.mean_squared_error(Y, y_pred, sample_weight=raw['group_cnt'])))
print('Mean Absolute Percentage Error:',
np.sqrt(metrics.mean_absolute_percentage_error(Y, y_pred, sample_weight=raw['group_cnt'])))
raw['predict_halflife_hlr'] = y_pred
fig = go.Figure()
fig.add_trace(
go.Scatter(x=raw['halflife'], y=raw['predict_halflife_dhp'], marker_size=np.log(raw['group_cnt']),
mode='markers',
name='DHP'))
fig.add_trace(
go.Scatter(x=raw['halflife'], y=raw['predict_halflife_hlr'], marker_size=np.log(raw['group_cnt']),
mode='markers',
name='HLR', opacity=0.7))
fig.update_xaxes(title_text='observed half-life after recall', title_font=dict(size=18), tickfont=dict(size=14))
fig.update_yaxes(title_text='predicted half-life after recall', title_font=dict(size=18), tickfont=dict(size=14))
fig.update_layout(legend_font_size=14, margin_t=10)
fig.write_image(f"plot/fit_recall_halflife.pdf")
time.sleep(3)
fig.write_image(f"plot/fit_recall_halflife.pdf")
# fig.show()
def fit_forget_halflife(raw):
print('fit_forget_halflife_dhp')
raw['log_h'] = raw['last_halflife'].map(lambda x: np.log(x))
raw['fi'] = raw['last_p_recall'].map(lambda x: 1 - x)
raw['log_fi'] = raw['last_p_recall'].map(lambda x: np.log(1 - x))
raw['log_d'] = raw['d'].map(lambda x: np.log(x))
print(raw.corr())
X = raw[['log_d', 'log_h', 'log_fi']]
Y = raw[['log_halflife']]
lr = LinearRegression()
lr.fit(X, Y, sample_weight=raw['group_cnt'])
print('Intercept: ', lr.intercept_)
print('Slope: ', lr.coef_)
y_pred = np.exp(lr.predict(X))
Y = np.exp(Y)
print("Explained Variance Score: ",
metrics.explained_variance_score(Y, y_pred, sample_weight=raw['group_cnt']))
print('Mean Absolute Error:',
metrics.mean_absolute_error(Y, y_pred, sample_weight=raw['group_cnt']))
print('Mean Squared Error:',
metrics.mean_squared_error(Y, y_pred, sample_weight=raw['group_cnt']))
print('Root Mean Squared Error:',
np.sqrt(metrics.mean_squared_error(Y, y_pred, sample_weight=raw['group_cnt'])))
print('Mean Absolute Percentage Error:',
np.sqrt(metrics.mean_absolute_percentage_error(Y, y_pred, sample_weight=raw['group_cnt'])))
raw['predict_halflife_dhp'] = y_pred
print('fit_forget_halflife_hlr')
lr = joblib.load('fit_result/clf.pkl')
X = raw[['right', 'wrong', 'd']]
Y = raw[['log_halflife']]
print('Intercept: ', lr.intercept_)
print('Slope: ', lr.coef_)
y_pred = np.exp(lr.predict(X))
Y = np.exp(Y)
print("Explained Variance Score: ",
metrics.explained_variance_score(Y, y_pred, sample_weight=raw['group_cnt']))
print('Mean Absolute Error:',
metrics.mean_absolute_error(Y, y_pred, sample_weight=raw['group_cnt']))
print('Mean Squared Error:',
metrics.mean_squared_error(Y, y_pred, sample_weight=raw['group_cnt']))
print('Root Mean Squared Error:',
np.sqrt(metrics.mean_squared_error(Y, y_pred, sample_weight=raw['group_cnt'])))
print('Mean Absolute Percentage Error:',
np.sqrt(metrics.mean_absolute_percentage_error(Y, y_pred, sample_weight=raw['group_cnt'])))
raw['predict_halflife_hlr'] = y_pred
fig = go.Figure()
fig.add_trace(
go.Scatter(x=raw['halflife'], y=raw['predict_halflife_dhp'], marker_size=np.log(raw['group_cnt']),
mode='markers',
name='DHP'))
fig.add_trace(
go.Scatter(x=raw['halflife'], y=raw['predict_halflife_hlr'], marker_size=np.log(raw['group_cnt']),
mode='markers',
name='HLR', opacity=0.7))
fig.update_xaxes(title_text='observed half-life after forget', title_font=dict(size=18), tickfont=dict(size=14))
fig.update_yaxes(title_text='predicted half-life after forget', title_font=dict(size=18), tickfont=dict(size=14))
fig.update_layout(legend_font_size=14, margin_t=10)
fig.write_image(f"plot/fit_forget_halflife.pdf")
time.sleep(3)
fig.write_image(f"plot/fit_forget_halflife.pdf")
# fig.show()
def fit_hlr_model(raw):
print('hlr_model_fitting')
corr = raw.select_dtypes(exclude=['object']).corr()
print(corr)
X = raw[['right', 'wrong', 'd']]
Y = raw[['log_halflife']]
lr = LinearRegression()
lr.fit(X, Y, sample_weight=raw['group_cnt'])
print('Intercept: ', lr.intercept_)
print('Slope: ', lr.coef_)
joblib.dump(lr, 'fit_result/clf.pkl')
y_pred = np.exp(lr.predict(X))
Y = np.exp(Y)
print("Explained Variance Score: ",
metrics.explained_variance_score(Y, y_pred, sample_weight=raw['group_cnt']))
print('Mean Absolute Error:',
metrics.mean_absolute_error(Y, y_pred, sample_weight=raw['group_cnt']))
print('Mean Squared Error:',
metrics.mean_squared_error(Y, y_pred, sample_weight=raw['group_cnt']))
print('Root Mean Squared Error:',
np.sqrt(metrics.mean_squared_error(Y, y_pred, sample_weight=raw['group_cnt'])))
print('Mean Absolute Percentage Error:',
np.sqrt(metrics.mean_absolute_percentage_error(Y, y_pred, sample_weight=raw['group_cnt'])))
raw['predict_halflife'] = y_pred
fig = px.scatter(raw, x='halflife', y='predict_halflife', size='group_cnt')
fig.update_xaxes(title_font=dict(size=18), tickfont=dict(size=14))
fig.update_yaxes(title_font=dict(size=18), tickfont=dict(size=14))
fig.write_image(f"plot/fit_hlr_model.pdf", width=600, height=360)
time.sleep(3)
fig.write_image(f"plot/fit_hlr_model.pdf", width=600, height=360)
# fig.show()
if __name__ == "__main__":
raw = pd.read_csv('./data/halflife_for_visual.tsv', sep='\t')
raw['right'] = raw['r_history'].str.count('1')
raw['wrong'] = raw['r_history'].str.count('0')
raw['right'] = raw['right'].map(lambda x: np.sqrt(x + 1))
raw['wrong'] = raw['wrong'].map(lambda x: np.sqrt(x + 1))
raw['log_halflife'] = raw['halflife'].map(lambda x: np.log(x))
raw.drop_duplicates(inplace=True)
raw = raw[raw['group_cnt'] > 1000]
fit_hlr_model(raw[(raw['last_recall'] == 0) | (raw['halflife_increase'] > 1) & (raw['last_recall'] == 1) & (
raw['r_history'].str.count('0') == 1)].copy())
fit_recall_halflife(
raw[(raw['halflife_increase'] > 1) & (raw['last_recall'] == 1) & (raw['r_history'].str.count('0') == 1)].copy())
fit_forget_halflife(raw[raw['last_recall'] == 0].copy())