-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamlTask3.py
More file actions
executable file
·189 lines (123 loc) · 5.45 KB
/
Copy pathamlTask3.py
File metadata and controls
executable file
·189 lines (123 loc) · 5.45 KB
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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import biosppy.signals.ecg as ecg
from sklearn.preprocessing import StandardScaler, Normalizer
from sklearn.metrics import f1_score, make_scorer
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.feature_selection import SelectKBest, f_classif
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
import auxilary
FEATURE_SELECTION = False
REMOVE_OUTLIER = True
PLOT_FEATURE_SCORES = False
X_train = np.load('preprocessed_X_train_20.npy')
X_test = np.load('preprocessed_X_test_20.npy')
print(X_train.shape)
print(X_test.shape)
y_train = pd.read_csv(r'y_train.csv')
y_train = y_train.drop(columns= 'id', axis=1)
y_train = y_train.values.ravel()
print("train shape: ", X_train.shape)
print("test shape: ", X_test.shape)
# Remove Outliers
if REMOVE_OUTLIER:
X_train, y_train = auxilary.OutlierDetectionIsolationForest(X_train, y_train, percentageOutlier='auto')
# Plot scores of features
if PLOT_FEATURE_SCORES:
auxilary.plotSFeatureScores(X_train, y_train, f_classif)
# Feature Selection !
if FEATURE_SELECTION:
inputDim = 120
featureSelection = SelectKBest(f_classif, k = inputDim)
X_train = featureSelection.fit_transform(X_train, y_train)
scores = featureSelection.scores_
print("Shape after feature selection: ", X_train.shape)
normalizer = Normalizer()
scaler = StandardScaler()
#X_train = scaler.fit_transform(X_train)
X_train = scaler.fit_transform(X_train)
# First train a binary classifier on labels [0,1,2] vs 3
y_train_BinaryClassifier = np.copy(y_train)
indicesLabel1 = np.argwhere(y_train_BinaryClassifier == 1)
indicesLabel2 = np.argwhere(y_train_BinaryClassifier == 2)
y_train_BinaryClassifier[indicesLabel1] = 0
y_train_BinaryClassifier[indicesLabel2] = 0
indicesLabel3 = np.argwhere(y_train_BinaryClassifier == 3)
y_train_BinaryClassifier[indicesLabel3] = 1
print('Label 1: ', indicesLabel1.shape)
print('Label 2: ', indicesLabel2.shape)
print('Label 3: ', indicesLabel3.shape)
parameters_BinaryClassifier = { 'n_estimators': [10, 100, 250, 500, 1000],
'criterion': ['entropy'],
'class_weight': ['balanced']
}
rfc_bc = RandomForestClassifier(random_state=0)
parameters_BinaryClassifier = {
'kernel': ['rbf', 'linear'],
'C': [0.001, 0.01,0.1,1,10]
}
svc = SVC(gamma='scale', class_weight='balanced', random_state=37, decision_function_shape='ovo')
scoreFunction = make_scorer(f1_score, average='micro', greater_is_better=True)
clf_bc = GridSearchCV(estimator=svc, param_grid=parameters_BinaryClassifier, cv=5, verbose=2, scoring=scoreFunction, n_jobs=3)
clf_bc.fit(X_train, y_train_BinaryClassifier)
print("Best score of best on validation set: ", clf_bc.best_score_) #rbf, 1e-5
print("Best Parameters: ", clf_bc.best_params_) # 0.968448
#X_test = scaler.transform(X_test)
X_test = normalizer.transform(X_test)
y_pred_bc = clf_bc.predict(X_test)
numberOfOthers = np.count_nonzero(y_pred_bc == 0)
numberOfLabel3 = np.count_nonzero(y_pred_bc == 1)
print('prediction others:', numberOfOthers)
print('prediction label3:', numberOfLabel3)
print('Number of 0:', np.count_nonzero(y_train == 0))
print('Number of 1:', np.count_nonzero(y_train == 1))
print('Number of 2:', np.count_nonzero(y_train == 2))
print('Number of 3:', np.count_nonzero(y_train == 3))
# Now remove the class 3 from data and train a second classifier
X_train = np.delete(X_train, indicesLabel3, axis=0)
y_train = np.delete(y_train, indicesLabel3, axis=0)
print('Number of 0:', np.count_nonzero(y_train == 0))
print('Number of 1:', np.count_nonzero(y_train == 1))
print('Number of 2:', np.count_nonzero(y_train == 2))
print('Number of 3:', np.count_nonzero(y_train == 3))
parameters_rfc = { 'n_estimators': [10, 100, 250, 500, 1000],
'criterion': ['entropy'],
'class_weight': ['balanced']
}
rfc = RandomForestClassifier(random_state=0)
clf = GridSearchCV(estimator=rfc, param_grid= parameters_rfc, cv=5, verbose=2, scoring= scoreFunction, n_jobs=3)
clf.fit(X_train, y_train)
print("Best score of best on validation set: ", clf.best_score_) #0.6670
print("Best Parameters: ", clf.best_params_) #rbf, 10
# First predict the first 3 classes
X_test = scaler.transform(X_test)
y_pred_test = clf.predict(X_test)
y_pred_test_bc = clf_bc.predict(X_test)
indicesOfLabel3Prediction = np.argwhere(y_pred_test_bc == 1)
y_pred_test[indicesOfLabel3Prediction] = 3
auxilary.createSubmissionFiles(y_pred_test)
######## Plot some samples #########
# for i in range(10):
# randomIndex = np.random.randint(0, 5117)
# sample = X_train[randomIndex,:]
# sample = pd.Series(sample)
# sample.plot()
# label = y_train[randomIndex]
# plt.title('y index: %i' % label )
# plt.show(block=False)
# plt.pause(1)
# plt.close()
# indicesOfLabel3 = np.where(y_train == 3)[0]
# print(indicesOfLabel3.shape)
# for i in range(10):
# randomIndex = np.random.randint(0, indicesOfLabel3.shape[0])
# sample = X_train[indicesOfLabel3[randomIndex],:]
# sample = pd.Series(sample)
# sample.plot()
# label = y_train[indicesOfLabel3[randomIndex]]
# plt.title('y index: %i' % label )
# plt.show(block=False)
# plt.pause(1)
# plt.close()