-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel-implementation.py
83 lines (61 loc) · 2.24 KB
/
Model-implementation.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
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
import random
import scipy
from sklearn.preprocessing import MinMaxScaler
from sklearn.svm import SVC
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.model_selection import RandomizedSearchCV
for i in range(1, 5):
img_arr = X_train[random.choice(X_train.index)]
img = img_arr.reshape(224, 224, 3)
plt.subplot(2,2,i)
plt.imshow(img)
plt.axis("off")
full_data = create_dataset(directory, dst, label_dict, xls=False)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
full_data['arrays'], full_data['labels'], test_size=0.2, random_state=42)
#combining
arr_X_train = np.vstack(X_train)
arr_X_test = np.vstack(X_test)
arr_y_train = np.vstack(y_train).ravel()
arr_y_test = np.vstack(y_test).ravel()
#scaling data
scaler = MinMaxScaler()
scaled_X_train = scaler.fit_transform(arr_X_train)
scaled_X_test = scaler.fit_transform(arr_X_test)
#tuning hyper-parameters
param_grid = {'C': scipy.stats.expon(scale=100),
'gamma': scipy.stats.expon(scale=.1),
'kernel': ['rbf']}
grid = RandomizedSearchCV(SVC(),param_grid,refit=True,verbose=2)
grid.fit(scaled_X_train, arr_y_train)
#training model
svc_clf = SVC(kernel='rbf',
class_weight='balanced',
verbose=True,
decision_function_shape='ovr')
svc_clf.fit(scaled_X_train, arr_y_train)
y_pred = svc_clf.predict(scaled_X_test)
#evaluation
print(confusion_matrix(y_test,y_pred))
print(classification_report(arr_y_test,y_pred))
"""
[[ 1 0 9 0 1]
[ 0 90 8 0 8]
[ 0 3 73 0 16]
[ 0 1 2 0 0]
[ 0 4 29 0 67]]
precision recall f1-score support
1 1.00 0.09 0.17 11
2 0.92 0.85 0.88 106
3 0.60 0.79 0.69 92
4 0.00 0.00 0.00 3
5 0.73 0.67 0.70 100
accuracy 0.74 312
macro avg 0.65 0.48 0.49 312
weighted avg 0.76 0.74 0.73 312
"""