-
Notifications
You must be signed in to change notification settings - Fork 11
/
fitnessFUNs.py
38 lines (28 loc) · 1.45 KB
/
fitnessFUNs.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
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import Binarizer
from sklearn import metrics
#____________________________________________________________________________________
def FN1(I,trainInput,trainOutput,dim):
data_train_internal, data_test_internal, target_train_internal, target_test_internal = train_test_split(trainInput, trainOutput, test_size=0.34, random_state=1)
reducedfeatures=[]
for index in range(0,dim):
if (I[index]==1):
reducedfeatures.append(index)
reduced_data_train_internal=data_train_internal[:,reducedfeatures]
reduced_data_test_internal=data_test_internal[:,reducedfeatures]
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(reduced_data_train_internal, target_train_internal)
target_pred_internal = knn.predict(reduced_data_test_internal)
acc_train = float(accuracy_score(target_test_internal, target_pred_internal))
fitness=0.99*(1-acc_train)+0.01*sum(I)/(dim)
return fitness
#_____________________________________________________________________
def getFunctionDetails(a):
# [name, lb, ub, dim]
param = { 0:["FN1",-1,1]
}
return param.get(a, "nothing")