-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoptimzer.py
99 lines (83 loc) · 2.81 KB
/
optimzer.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
84
85
86
87
88
89
90
91
92
93
94
95
96
import csv
import numpy as np
from pyFIMTDD import FIMTDD as FIMTGD
from FIMTDD_LS import FIMTDD as FIMTLS
import matplotlib.pyplot as plt
import itertools
import time
from multiprocessing import Pool
import progressbar as pb
from sklearn.model_selection import GridSearchCV
from sklearn.base import BaseEstimator
def abalone_test(paramlist,learner):
cumLoss=[0]
with open( "abalone.data", 'rt') as abalonefile:
for row in abalonefile:
row=row.rstrip().split(',')
target=float(row[-1])
if row[0]=="M":
numgender=1.
if row[0]=="I":
numgender=0.5
if row[0]=="F":
numgender=0.
input=[numgender]
for item in row[1:-1]:
input.append(float(item))
cumLoss.append(cumLoss[-1] + np.fabs(target - learner.eval_and_learn(np.array(input), target)))
return cumLoss[-1]
class Abalone_Optimizer(BaseEstimator):
def __init__(self,fimtdd):
self.fimtdd = fimtdd
self.cumLoss = 0
def fit(self,X,y,sample_weight=None):
self.cumLoss = 0
for i in range(len(X)):
self.cumLoss += np.fabs(y-self.fimtdd.eval_and_learn(X[i],y[i]))
return self
def predict(self,X):
y = list()
for x in X:
y.append(self.fimtdd.eval(x))
return y
def score(self, X,y,sample_weight=None):
self.fit(X,y,sample_weight)
return self.cumLoss/float(len(X))
def get_params(self, deep=True):
pass
def set_params(self,**params):
for a in params:
if a == "gamma":
gamma = params[a]
if a == "learn":
learn = params[a]
if a == "threshold":
threshold = params[a]
if a == "n_min":
n_min = params[a]
if a == "alpha":
alpha = params[a]
if type(self.fimtdd) == FIMTGD:
self.fimtdd = FIMTGD(gamma=gamma,learn=learn,n_min=n_min,threshold=threshold,alpha=alpha)
else:
self.fimtdd = FIMTLS(gamma=gamma,learn=learn,n_min=n_min,threshold=threshold,alpha=alpha)
def get_data():
input = list()
target = list()
with open( "abalone.data", 'rt') as abalonefile:
for row in abalonefile:
row=row.rstrip().split(',')
target.append(float(row[-1]))
if row[0]=="M":
numgender=1.
if row[0]=="I":
numgender=0.5
if row[0]=="F":
numgender=0.
inp=[numgender]
for item in row[1:-1]:
inp.append(float(item))
input.append(inp)
return np.array(input),np.array(target)
X,y = get_data()
#GridSearchCV(Abalone_Optimizer(FIMTGD()),param_grid=param_grid,scoring=)