-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.py
78 lines (67 loc) · 2.57 KB
/
test.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
from scipy.io import loadmat
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import LinearSVC
from sklearn.metrics import classification_report,accuracy_score
def sq_norm(mat):
normM = mat - mat.min()
normM = np.sqrt(normM)
normM = normM / normM.max()
mat_sq_norm = normM
return mat_sq_norm
# x = loadmat('../matlab-outputs/knn/with-others/trainFeaturesData.mat')
# y = loadmat('../matlab-outputs/knn/with-others/k-3/testFeaturesData.mat')
# y = np.load('../pred.npy')
# z = np.load('./pred (1).npy')
# train_features = x['trainingFeaturesNorm']
# train_features_norm_mat = x['trainingFeaturesNorm']
# train_features = np.load('../test_features.npy')
# test_features = y['testFeaturesNorm']
# print y
# print('Matlab')
# print(train_features_mat)
# print('Python')
# print(train_features)
# print('Matlab - Norm')
# print(train_features_norm_mat)
# print('Python - Norm')
# print(sq_norm(train_features))
# # x = np.load('/model/pred.npy')
# # print sq_norm(y)[0]
# # print sq_norm(z)[0]
# # print x['trainingFeaturesNorm'][0]
# # x = np.array([[1,4,2],[4,9,6]])
# # print sq_norm(x)
train_features = sq_norm(np.load('/model/train_features.npy'))
train_labels = np.load('/model/train_labels.npy')
test_features = sq_norm(np.load('/model/test_features.npy'))
test_labels = np.load('/model/test_labels.npy')
target_names = ['blade','gun','others','shuriken']
# train_features = train_features.
print(train_features.shape)
print(test_features.shape)
print(train_labels.shape)
print(test_labels.shape)
try:
svm = LinearSVC(random_state=0)
svm.fit(train_features, train_labels)
pred = svm.predict(test_features)
np.save('/output/pred_svm',pred)
# pred = np.load('/model2/pred.npy')
print('SVM')
print(accuracy_score(test_labels, pred))
print(classification_report(test_labels, pred,target_names=target_names))
for i in range(1,30,2):
print("k=%d" %(i))
knn = KNeighborsClassifier(n_neighbors=i)
# print('train',train_features.shape,train_labels.shape)
# print('test',test_features.shape,test_labels.shape)
# train_features = train_features.reshape(train_features.shape[0],-1)
knn.fit(train_features, train_labels)
# test_features = test_features.reshape(test_features.shape[0],-1)
pred = knn.predict(test_features)
np.save('/output/pred_%d' %(i),pred)
# pred = np.load('/model2/pred.npy')
print(accuracy_score(test_labels, pred))
print(classification_report(test_labels, pred,target_names=target_names))
except Exception as e: print(e)