-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassification_model_comparison.py
172 lines (143 loc) · 5.75 KB
/
classification_model_comparison.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
import matplotlib.pyplot as plt
from utilfunction import *
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.naive_bayes import GaussianNB, BernoulliNB
import numpy
numpy.random.seed(7)
from sklearn.svm import SVC
def show_metric(title, ylabel, labels, data, markers=('s','^'), colors=('b','r')):
users = np.arange(1, 49)
userslabel = df.index.get_level_values(0).drop_duplicates()
plt.close()
for d,m,c in zip(data,markers,colors):
plt.scatter(users, d, marker=m, c=c)
plt.title(title)
plt.ylabel(ylabel)
plt.xlabel('User')
plt.legend(labels,
loc='best')
plt.xticks(users, userslabel, rotation='vertical')
plt.ylim(0.55,1)
plt.grid(True)
plt.show()
show_metric('',
'F1-Score',
['Impersonal', 'Personal'],
[pd.read_pickle('f1_imp_nn_withoutsleep'), pd.read_pickle('f1_p_nn_withoutsleep')])
'''
df = pd.read_pickle('sedentarismdata.pkl')
df = delete_user(df,52)
df = METcalculation(df)
df = delete_sleep_hours(df)
df = makeSedentaryClasses(df)
df = makeDummies(df)
df = shift_hours(df,1, 'classification')
df.drop(['hourofday'],
axis=1, inplace=True)
'''
estimator = KerasClassifier(build_fn=baseline_model, epochs=30, batch_size=512, verbose=2)
modelnn = create_model(estimator)
estimator2 = KerasClassifier(build_fn=baseline_model, epochs=30, batch_size=32, verbose=2)
modelnn2 = create_model(estimator2)
clf = LogisticRegression(solver='liblinear', max_iter=400, class_weight='balanced')
model = create_model(clf)
'''
#f1_p_nn = per_user_classification(df, modelnn, True)
f1_p_logreg = per_user_classification(df, model, True)
#pd.to_pickle(f1_p_logreg,'f1_p_logreg_withsleep')
a = pd.read_pickle('f1_p_logreg_withsleep')
b = pd.read_pickle('f1_p_logreg_withoutsleep')
f1_imp_nn = live_one_out_classification(df, modelnn, True)
f1_imp_nn1 = pd.read_pickle('f1_imp_nn_withsleep')
f1_imp_nn2 = pd.read_pickle('f1_imp_nn_withoutsleep')
pd.to_pickle(f1_imp_nn1,'f1_imp_nn_withoutsleep')
pd.to_pickle(f1_imp_nn2,'f1_imp_nn_withsleep')
f1_imp_logreg = live_one_out_classification(df, model, True)
b = df.groupby(df.index.get_level_values(0))['sclass'].apply(lambda x : np.sum(x==1)).values
c = df.groupby(df.index.get_level_values(0))['sclass'].count().values
b = b/c
"""
show_metric('Model F1-score for impersonal models ',
'F1-score',
['nn', 'logreg'],
[f1_imp_nn, f1_imp_logreg])
"""
show_metric('',
'F1-Score',
['Impersonal', 'Personal'],
[pd.read_pickle('f1_imp_nn_withsleep'), pd.read_pickle('f1_p_logreg_withsleep')])
show_metric('',
'F1-Score',
['Impersonal', 'Personal'],
[pd.read_pickle('f1_imp_nn_withoutsleep'), pd.read_pickle('f1_p_logreg_withoutsleep')])
'''
print('f1_imp_nn_withsleep')
print(np.mean(pd.read_pickle('f1_imp_nn_withsleep')))
print(np.std(pd.read_pickle('f1_imp_nn_withsleep')))
print('f1_imp_nn_withoutsleep')
print(np.mean(pd.read_pickle('f1_imp_nn_withoutsleep')))
print(np.std(pd.read_pickle('f1_imp_nn_withoutsleep')))
print('f1_imp_logreg_withsleep')
print(np.mean(pd.read_pickle('f1_imp_logreg_withsleep')))
print(np.std(pd.read_pickle('f1_imp_logreg_withsleep')))
print('f1_imp_logreg_withoutsleep')
print(np.mean(pd.read_pickle('f1_imp_logreg_withoutsleep')))
print(np.std(pd.read_pickle('f1_imp_logreg_withoutsleep')))
print('f1_p_logreg_withsleep')
print(np.mean(pd.read_pickle('f1_p_logreg_withsleep')))
print(np.std(pd.read_pickle('f1_p_logreg_withsleep')))
print('f1_p_logreg_withoutsleep')
print(np.mean(pd.read_pickle('f1_p_logreg_withoutsleep')))
print(np.std(pd.read_pickle('f1_p_logreg_withoutsleep')))
print('f1_p_nn_withsleep')
print(np.mean(pd.read_pickle('f1_p_nn_withsleep')))
print(np.std(pd.read_pickle('f1_p_nn_withsleep')))
print('f1_p_nn_withoutsleep')
print(np.mean(pd.read_pickle('f1_p_nn_withoutsleep')))
print(np.std(pd.read_pickle('f1_p_nn_withoutsleep')))
df = pd.read_pickle('sedentarismdata.pkl')
df = delete_user(df,52)
df = METcalculation(df)
df = makeSedentaryClasses(df)
df = makeDummies(df)
df = shift_hours(df,1, 'classification')
df.drop(['hourofday'],
axis=1, inplace=True)
#pd.to_pickle(live_one_out_classification(df, modelnn),'f1_imp_nn_withsleep')
pd.to_pickle(live_one_out_classification(df, model),'f1_imp_logreg_withsleep')
#pd.to_pickle(per_user_classification(df, model),'f1_p_logreg_withsleep')
#data = df.loc[ (df.index.get_level_values(0) >= 50) & (df.index.get_level_values(0)<60) ]
#pd.to_pickle(per_user_classification(data, modelnn2),'f1_p_nn_withsleep_6')
df = pd.read_pickle('sedentarismdata.pkl')
df = delete_user(df,52)
df = METcalculation(df)
df = delete_sleep_hours(df)
df = makeSedentaryClasses(df)
df = makeDummies(df)
df = shift_hours(df,1, 'classification')
df.drop(['hourofday'],
axis=1, inplace=True)
#pd.to_pickle(live_one_out_classification(df, modelnn),'f1_imp_nn_withoutsleep')
#pd.to_pickle(live_one_out_classification(df, model),'f1_imp_logreg_withoutsleep')
#pd.to_pickle(per_user_classification(df, model),'f1_p_logreg_withoutsleep')
data = df.loc[ (df.index.get_level_values(0) >= 50) & (df.index.get_level_values(0)<60) ]
pd.to_pickle(per_user_classification(data, modelnn2),'f1_p_nn_withoutsleep_5')
def join_pkls(name):
f1 = []
for i in np.arange(1,6):
f1.extend(pd.read_pickle(name + '_' + str(i)))
pd.to_pickle(f1,name)
join_pkls('f1_p_nn_withsleep')
join_pkls('f1_p_nn_withoutsleep')
pkl = pd.read_pickle('f1_p_nn_withoutsleep')
np.mean(pkl)
a = pd.read_pickle('f1_imp_nn_withoutsleep')
b = pd.read_pickle('f1_p_nn_withoutsleep')
plt.boxplot(a)
plt.show()
c = df.groupby(df.index.get_level_values(0))['noiseLevel'].count().values
data = pd.DataFrame(data={'f1':a,
'count':c})
plt.close()
sns.lmplot(x='count',y='f1', data=data)
plt.show()