-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrowdsignals_ch7_classification.py
242 lines (192 loc) · 11.9 KB
/
crowdsignals_ch7_classification.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
##############################################################
# #
# Mark Hoogendoorn and Burkhardt Funk (2017) #
# Machine Learning for the Quantified Self #
# Springer #
# Chapter 7 #
# #
##############################################################
import os
import copy
import numpy as np
import pandas as pd
from pathlib import Path
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from Chapter7.PrepareDatasetForLearning import PrepareDatasetForLearning
from Chapter7.LearningAlgorithms import ClassificationAlgorithms
from Chapter7.LearningAlgorithms import RegressionAlgorithms
from Chapter7.Evaluation import ClassificationEvaluation
from Chapter7.Evaluation import RegressionEvaluation
from Chapter7.FeatureSelection import FeatureSelectionClassification
from Chapter7.FeatureSelection import FeatureSelectionRegression
from util import util
from util.VisualizeDataset import VisualizeDataset
# Read the result from the previous chapter, and make sure the index is of the type datetime.
DATA_PATH = Path('')
DATASET_FNAME = 'chapter5_result_alt.csv'
RESULT_FNAME = 'chapter7_classification_result.csv'
EXPORT_TREE_PATH = Path('./figures/crowdsignals_ch7_classification/')
# Next, we declare the parameters we'll use in the algorithms.
N_FORWARD_SELECTION = 10
try:
dataset = pd.read_csv(DATA_PATH / DATASET_FNAME, index_col=0,nrows=3000)
except IOError as e:
print('File not found, try to run previous crowdsignals scripts first!')
raise e
dataset.index = pd.to_datetime(dataset.index)
# Let us create our visualization class again.
DataViz = VisualizeDataset(__file__)
# Let us consider our first task, namely the prediction of the label. We consider this as a non-temporal task.
# We create a single column with the categorical attribute representing our class. Furthermore, we use 70% of our data
# for training and the remaining 30% as an independent test set. We select the sets based on stratified sampling. We remove
# cases where we do not know the label.
prepare = PrepareDatasetForLearning()
train_X, test_X, train_y, test_y = prepare.split_single_dataset_classification(dataset, ['label'], 'like', 0.7, filter=True, temporal=False)
print('Training set length is: ', len(train_X.index))
print('Test set length is: ', len(test_X.index))
# Select subsets of the features that we will consider:
basic_features = ['acc_phone_x','acc_phone_y','acc_phone_z','gyr_phone_x','gyr_phone_y','gyr_phone_z','light_phone_value','magnet_phone_x','magnet_phone_y','magnet_phone_z']
pca_features = ['pca_1','pca_2','pca_3','pca_4','pca_5','pca_6']
time_features = [name for name in dataset.columns if '_temp_' in name]
freq_features = [name for name in dataset.columns if (('_freq' in name) or ('_pse' in name))]
print('#basic features: ', len(basic_features))
print('#PCA features: ', len(pca_features))
print('#time features: ', len(time_features))
print('#frequency features: ', len(freq_features))
cluster_features = ['cluster']
print('#cluster features: ', len(cluster_features))
features_after_chapter_3 = list(set().union(basic_features, pca_features))
features_after_chapter_4 = list(set().union(basic_features, pca_features, time_features, freq_features))
features_after_chapter_5 = list(set().union(basic_features, pca_features, time_features, freq_features, cluster_features))
# First, let us consider the performance over a selection of features:
fs = FeatureSelectionClassification()
features, ordered_features, ordered_scores = fs.forward_selection(N_FORWARD_SELECTION,
train_X[features_after_chapter_5], train_y)
print(ordered_scores)
print(ordered_features)
# DataViz.plot_xy(x=[range(1, N_FORWARD_SELECTION+1)], y=[ordered_scores],
# xlabel='number of features', ylabel='accuracy')
# Based on the plot we select the top 10 features (note: slightly different compared to Python 2, we use
# those feartures here).
selected_features = ['acc_phone_z_temp_mean_ws_4', 'pca_2_temp_std_ws_4', 'pca_1_temp_mean_ws_4', 'pca_3_temp_std_ws_4', 'acc_phone_x_temp_mean_ws_4', 'pca_5_temp_mean_ws_4', 'gyr_phone_y_temp_mean_ws_4', 'pca_2_temp_mean_ws_4', 'acc_phone_y_pse', 'pca_2']
# Let us first study the impact of regularization and model complexity: does regularization prevent overfitting?
learner = ClassificationAlgorithms()
eval = ClassificationEvaluation()
reg_parameters = [0.0001, 0.001, 0.01, 0.1, 1, 10]
performance_training = []
performance_test = []
# We repeat the experiment a number of times to get a bit more robust data as the initialization of the NN is random.
N_REPEATS_NN = 5
for reg_param in reg_parameters:
performance_tr = 0
performance_te = 0
for i in range(0, N_REPEATS_NN):
class_train_y, class_test_y, class_train_prob_y, class_test_prob_y = learner.feedforward_neural_network(
train_X, train_y,
test_X, hidden_layer_sizes=(250, ), alpha=reg_param, max_iter=500,
gridsearch=False
)
performance_tr += eval.accuracy(train_y, class_train_y)
performance_te += eval.accuracy(test_y, class_test_y)
performance_training.append(performance_tr/N_REPEATS_NN)
performance_test.append(performance_te/N_REPEATS_NN)
DataViz.plot_xy(x=[reg_parameters, reg_parameters], y=[performance_training, performance_test], method='semilogx',
xlabel='regularization parameter value', ylabel='accuracy', ylim=[0.95, 1.01],
names=['training', 'test'], line_styles=['r-', 'b:'])
# Second, let us consider the influence of certain parameter settings for the tree model. (very related to the
# regularization) and study the impact on performance.
leaf_settings = [1,2,5,10]
performance_training = []
performance_test = []
for no_points_leaf in leaf_settings:
class_train_y, class_test_y, class_train_prob_y, class_test_prob_y = learner.decision_tree(
train_X[selected_features], train_y, test_X[selected_features], min_samples_leaf=no_points_leaf,
gridsearch=False, print_model_details=False)
performance_training.append(eval.accuracy(train_y, class_train_y))
performance_test.append(eval.accuracy(test_y, class_test_y))
DataViz.plot_xy(x=[leaf_settings, leaf_settings], y=[performance_training, performance_test],
xlabel='minimum number of points per leaf', ylabel='accuracy',
names=['training', 'test'], line_styles=['r-', 'b:'])
# So yes, it is important :) Therefore we perform grid searches over the most important parameters, and do so by means
# of cross validation upon the training set.
possible_feature_sets = [basic_features, features_after_chapter_3, features_after_chapter_4, features_after_chapter_5, selected_features]
feature_names = ['initial set', 'Chapter 3', 'Chapter 4', 'Chapter 5', 'Selected features']
N_KCV_REPEATS = 5
scores_over_all_algs = []
for i in range(0, len(possible_feature_sets)):
selected_train_X = train_X[possible_feature_sets[i]]
selected_test_X = test_X[possible_feature_sets[i]]
# First we run our non deterministic classifiers a number of times to average their score.
print("test1")
performance_tr_nn = 0
performance_tr_rf = 0
performance_tr_svm = 0
performance_te_nn = 0
performance_te_rf = 0
performance_te_svm = 0
for repeat in range(0, N_KCV_REPEATS):
print("test2")
class_train_y, class_test_y, class_train_prob_y, class_test_prob_y = learner.feedforward_neural_network(
selected_train_X, train_y, selected_test_X, gridsearch=True
)
performance_tr_nn += eval.accuracy(train_y, class_train_y)
performance_te_nn += eval.accuracy(test_y, class_test_y)
print("test3")
class_train_y, class_test_y, class_train_prob_y, class_test_prob_y = learner.random_forest(
selected_train_X, train_y, selected_test_X, gridsearch=True
)
performance_tr_rf += eval.accuracy(train_y, class_train_y)
performance_te_rf += eval.accuracy(test_y, class_test_y)
print("test4")
# class_train_y, class_test_y, class_train_prob_y, class_test_prob_y = learner.support_vector_machine_with_kernel(
# selected_train_X, train_y, selected_test_X, gridsearch=True
# )
# performance_tr_svm += eval.accuracy(train_y, class_train_y)
# performance_te_svm += eval.accuracy(test_y, class_test_y)
# print("test5")
overall_performance_tr_nn = performance_tr_nn/N_KCV_REPEATS
overall_performance_te_nn = performance_te_nn/N_KCV_REPEATS
overall_performance_tr_rf = performance_tr_rf/N_KCV_REPEATS
overall_performance_te_rf = performance_te_rf/N_KCV_REPEATS
overall_performance_tr_svm = performance_tr_svm/N_KCV_REPEATS
overall_performance_te_svm = performance_te_svm/N_KCV_REPEATS
# And we run our deterministic classifiers:
print("test6")
class_train_y, class_test_y, class_train_prob_y, class_test_prob_y = learner.k_nearest_neighbor(
selected_train_X, train_y, selected_test_X, gridsearch=True
)
performance_tr_knn = eval.accuracy(train_y, class_train_y)
performance_te_knn = eval.accuracy(test_y, class_test_y)
print("test7")
class_train_y, class_test_y, class_train_prob_y, class_test_prob_y = learner.decision_tree(
selected_train_X, train_y, selected_test_X, gridsearch=True
)
performance_tr_dt = eval.accuracy(train_y, class_train_y)
performance_te_dt = eval.accuracy(test_y, class_test_y)
print("test8")
class_train_y, class_test_y, class_train_prob_y, class_test_prob_y = learner.naive_bayes(
selected_train_X, train_y, selected_test_X
)
performance_tr_nb = eval.accuracy(train_y, class_train_y)
performance_te_nb = eval.accuracy(test_y, class_test_y)
scores_with_sd = util.print_table_row_performances(feature_names[i], len(selected_train_X.index), len(selected_test_X.index), [
(overall_performance_tr_nn, overall_performance_te_nn),
(overall_performance_tr_rf, overall_performance_te_rf),
(overall_performance_tr_svm, overall_performance_te_svm),
(performance_tr_knn, performance_te_knn),
(performance_tr_dt, performance_te_dt),
(performance_tr_nb, performance_te_nb)])
scores_over_all_algs.append(scores_with_sd)
print("test9")
DataViz.plot_performances_classification(['NN', 'RF', 'SVM', 'KNN', 'DT', 'NB'], feature_names, scores_over_all_algs)
# And we study two promising ones in more detail. First, let us consider the decision tree, which works best with the
# selected features.
class_train_y, class_test_y, class_train_prob_y, class_test_prob_y = learner.decision_tree(train_X[selected_features], train_y, test_X[selected_features],
gridsearch=True,
print_model_details=True, export_tree_path=EXPORT_TREE_PATH)
class_train_y, class_test_y, class_train_prob_y, class_test_prob_y = learner.random_forest(
train_X[selected_features], train_y, test_X[selected_features],
gridsearch=True, print_model_details=True)
test_cm = eval.confusion_matrix(test_y, class_test_y, class_train_prob_y.columns)
DataViz.plot_confusion_matrix(test_cm, class_train_prob_y.columns, normalize=False)