-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
139 lines (117 loc) · 5.07 KB
/
test.py
File metadata and controls
139 lines (117 loc) · 5.07 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
#https://github.com/etas/SynCAN/blob/master/train_2.zip
import numpy as np
import pandas as pd
from sklearn import svm
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from genetic_selection import GeneticSelectionCV
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from sklearn.metrics import f1_score
from sklearn.metrics import confusion_matrix
import SwarmPackagePy
from SwarmPackagePy import testFunctions as tf
def nearest_spider(spider, spiders):
spudis = list(spiders)
try:
pos = spudis.index(spider)
spudis.pop(pos)
except ValueError:
pass
dists = np.array([np.linalg.norm(spider - s) for s in spudis])
m = dists.argmin()
d = dists[m]
return d, m
def main():
train = pd.read_csv('dataset/CAN.csv',nrows=14000)
train.fillna(0,inplace=True)
print(train)
print(train.shape)
le = LabelEncoder()
train['ID'] = pd.Series(le.fit_transform(train['ID']))
print(train)
X = train.values[:, 1:7]
Y = train.values[:, 0]
print(Y)
#X = tf.easom_function(X[0].astype(int))
#print(X)
alh = SwarmPackagePy.ssa(10, tf.easom_function, -10, 6, 2, 20,0.4)
print(nearest_spider(0, X[0]))
print(nearest_spider(1, X[1]))
print(nearest_spider(2, X[2]))
print(nearest_spider(3, X[3]))
'''
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size = 0.2, random_state = 0)
estimator = svm.SVC(C=2.0,gamma='scale',kernel = 'rbf', random_state = 0)
estimator.fit(X_train, y_train)
y_pred = estimator.predict(X_test)
hr = accuracy_score(y_test,y_pred)*100
mr = precision_score(y_test, y_pred,average='macro') * 100
fr = recall_score(y_test, y_pred,average='macro') * 100
cr = f1_score(y_test, y_pred,average='macro') * 100
print(str(hr)+" "+str(mr)+" "+str(fr)+" "+str(cr))
tn, fp, fn, tp = confusion_matrix(y_test, y_pred).ravel()
print(str(fp)+" "+str(fn))
X = train.values[:, 3:7]
Y = train.values[:, 0]
print(Y)
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size = 0.2, random_state = 0)
estimator = KNeighborsClassifier()
estimator.fit(X_train, y_train)
y_pred = estimator.predict(X_test)
hr = accuracy_score(y_test,y_pred)*100
mr = precision_score(y_test, y_pred,average='macro') * 100
fr = recall_score(y_test, y_pred,average='macro') * 100
cr = f1_score(y_test, y_pred,average='macro') * 100
print(str(hr)+" "+str(mr)+" "+str(fr)+" "+str(cr))
tn, fp, fn, tp = confusion_matrix(y_test, y_pred).ravel()
print(str(fp)+" "+str(fn))
X = train.values[:, 4:7]
Y = train.values[:, 0]
print(Y)
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size = 0.2, random_state = 0)
estimator = DecisionTreeClassifier(max_features=2)
estimator.fit(X_train, y_train)
y_pred = estimator.predict(X_test)
hr = accuracy_score(y_test,y_pred)*100
mr = precision_score(y_test, y_pred,average='macro') * 100
fr = recall_score(y_test, y_pred,average='macro') * 100
cr = f1_score(y_test, y_pred,average='macro') * 100
print(str(hr)+" "+str(mr)+" "+str(fr)+" "+str(cr))
tn, fp, fn, tp = confusion_matrix(y_test, y_pred).ravel()
print(str(fp)+" "+str(fn))
X = train.values[:, 1:7]
Y = train.values[:, 0]
print(Y)
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size = 0.2, random_state = 0)
estimator = svm.SVC(C=2.0,gamma='scale',kernel = 'rbf', random_state = 0)
selector = GeneticSelectionCV(estimator,
cv=5,
verbose=1,
scoring="accuracy",
max_features=6,
n_population=5,
crossover_proba=0.5,
mutation_proba=0.2,
n_generations=5,
crossover_independent_proba=0.5,
mutation_independent_proba=0.05,
tournament_size=3,
n_gen_no_change=2,
caching=True,
n_jobs=-1)
selector = selector.fit(X_train, y_train)
y_pred = selector.predict(X_test)
hr = accuracy_score(y_pred,y_pred)*100
mr = precision_score(y_pred, y_pred,average='macro') * 100
fr = recall_score(y_pred, y_pred,average='macro') * 100
cr = f1_score(y_pred, y_pred,average='macro') * 100
print(str(hr)+" "+str(mr)+" "+str(fr)+" "+str(cr))
tn, fp, fn, tp = confusion_matrix(y_pred, y_pred).ravel()
print(str(fp)+" "+str(fn))
'''
if __name__ == "__main__":
main()