forked from Snishikant/CreditRating-FeatureSelection-GAW
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfitness_function.py
55 lines (40 loc) · 1.63 KB
/
fitness_function.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
from sklearn.model_selection import StratifiedKFold
import numpy as np
from sklearn.metrics import f1_score, accuracy_score
class FitenessFunction:
def __init__(self,n_splits = 5,*args,**kwargs):
"""
Parameters
-----------
n_splits :int,
Number of splits for cv
verbose: 0 or 1
"""
self.n_splits = n_splits
def calculate_fitness(self,model,x,y):
# x = x.values.
cv_set = np.repeat(-1.,x.shape[0])
skf = StratifiedKFold(n_splits = self.n_splits)
for train_index,test_index in skf.split(x,y):
# print(train_index, test_index)
x_train,x_test = x.iloc[train_index],x.iloc[test_index]
y_train,y_test = y[train_index],y[test_index]
if x_train.shape[0] != y_train.shape[0]:
raise Exception()
model.fit(x_train, y_train)
predicted_y = model.predict(x_test)
cv_set[test_index] = predicted_y
return accuracy_score(y,cv_set)
def give_predicted(self,model,x,y):
cv_set = np.repeat(-1.,x.shape[0])
skf = StratifiedKFold(n_splits = self.n_splits)
for train_index,test_index in skf.split(x,y):
# print(train_index, test_index)
x_train,x_test = x.iloc[train_index],x.iloc[test_index]
y_train,y_test = y[train_index],y[test_index]
if x_train.shape[0] != y_train.shape[0]:
raise Exception()
model.fit(x_train, y_train)
predicted_y = model.predict(x_test)
cv_set[test_index] = predicted_y
return cv_set