Repository navigation
Expand file tree
/
Copy pathclassifiers.py
More file actions
185 lines (155 loc) · 4.86 KB
/
Copy pathclassifiers.py
File metadata and controls
185 lines (155 loc) · 4.86 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
import numpy as np
from sklearn import svm
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split, GridSearchCV, StratifiedKFold
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
FEATURE_NAMES = [
"height",
"root_density",
"area",
"shape_index",
"linearity",
"sphericity",
"slenderness",
"length_height_ratio",
"circularity",
"footprint_density",
]
def data_loading(data_file="data.txt"):
data = np.loadtxt(data_file, dtype=np.float32, delimiter=",", comments="#")
ID = data[:, 0].astype(np.int32)
y = data[:, 1].astype(np.int32)
X = data[:, 2:].astype(np.float32)
return ID, X, y
def subset_features(X, feature_indices=None):
if feature_indices is None:
return X
return X[:, feature_indices]
def print_results(title, y_test, y_pred, best_params=None, cv_score=None):
acc = accuracy_score(y_test, y_pred)
print(f"\n===== {title} =====")
if best_params is not None:
print("Best parameters:", best_params)
if cv_score is not None:
print("Best cross-val accuracy: %.3f" % cv_score)
print("Test accuracy: %.3f" % acc)
print("Confusion matrix:")
print(confusion_matrix(y_test, y_pred))
print("Classification report:")
print(classification_report(y_test, y_pred, digits=3))
return acc
def get_split(X, y, test_size=0.4, random_state=42):
return train_test_split(
X,
y,
test_size=test_size,
random_state=random_state,
stratify=y
)
def tune_svm(X, y, feature_indices=None, test_size=0.4, random_state=42):
X = subset_features(X, feature_indices)
X_train, X_test, y_train, y_test = get_split(
X, y, test_size=test_size, random_state=random_state
)
pipeline = Pipeline([
("scaler", StandardScaler()),
("svm", svm.SVC())
])
param_grid = [
{
"svm__kernel": ["linear"],
"svm__C": [0.01, 0.1, 1, 10, 30, 50, 100]
},
{
"svm__kernel": ["rbf"],
"svm__C": [0.1, 1, 10, 30, 50, 100],
"svm__gamma": ["scale", 0.001, 0.01, 0.03, 0.05, 0.1, 1]
},
{
"svm__kernel": ["poly"],
"svm__C": [0.1, 1, 10, 30],
"svm__degree": [2, 3, 4],
"svm__gamma": ["scale", 0.01, 0.1]
},
{
"svm__kernel": ["sigmoid"],
"svm__C": [0.1, 1, 10],
"svm__gamma": ["scale", 0.01, 0.1]
}
]
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=random_state)
grid = GridSearchCV(
estimator=pipeline,
param_grid=param_grid,
scoring="accuracy",
cv=cv,
n_jobs=-1,
refit=True
)
grid.fit(X_train, y_train)
best_model = grid.best_estimator_
y_pred = best_model.predict(X_test)
acc = print_results(
title="SVM RESULTS",
y_test=y_test,
y_pred=y_pred,
best_params=grid.best_params_,
cv_score=grid.best_score_
)
return {
"model": best_model,
"accuracy": acc,
"best_params": grid.best_params_,
"cv_score": grid.best_score_,
"y_test": y_test,
"y_pred": y_pred
}
def tune_rf(X, y, feature_indices=None, test_size=0.4, random_state=42):
X = subset_features(X, feature_indices)
X_train, X_test, y_train, y_test = get_split(
X, y, test_size=test_size, random_state=random_state
)
rf = RandomForestClassifier(random_state=random_state)
param_grid = {
"n_estimators": [100, 200, 300, 500],
"max_depth": [None, 10, 20, 30],
"max_features": ["sqrt", "log2", None],
"min_samples_split": [2, 5, 10],
"min_samples_leaf": [1, 2, 4]
}
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=random_state)
grid = GridSearchCV(
estimator=rf,
param_grid=param_grid,
scoring="accuracy",
cv=cv,
n_jobs=-1,
refit=True
)
grid.fit(X_train, y_train)
best_model = grid.best_estimator_
y_pred = best_model.predict(X_test)
acc = print_results(
title="RF RESULTS",
y_test=y_test,
y_pred=y_pred,
best_params=grid.best_params_,
cv_score=grid.best_score_
)
importances = best_model.feature_importances_
ranking = np.argsort(importances)[::-1]
print("\nTop RF feature importances:")
for i in ranking[:10]:
print(f"{FEATURE_NAMES[i]:25s} {importances[i]:.4f}")
return {
"model": best_model,
"accuracy": acc,
"best_params": grid.best_params_,
"cv_score": grid.best_score_,
"importances": importances,
"ranking": ranking,
"y_test": y_test,
"y_pred": y_pred
}