forked from shubhampachori12110095/DFLsklearn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.py
More file actions
190 lines (158 loc) · 7.06 KB
/
examples.py
File metadata and controls
190 lines (158 loc) · 7.06 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 18 10:46:41 2017
@authors: V. Latorre, F. Benvenuto
"""
import numpy
import sklearn
import DFLsklearn
def demo_classification():
#------------------------------------------------------------------------------
#Load the dateset and split it in training and test sets
#------------------------------------------------------------------------------
print('Loading the iris dataset from sklearn')
dataset=sklearn.datasets.load_iris()
X=dataset.data
Y=dataset.target
seed=1
test_size = int( 0.2 * len( Y ) )
numpy.random.seed( seed )
indices = numpy.random.permutation(len(X))
X_train = X[ indices[:-test_size]]
Y_train = Y[ indices[:-test_size]]
X_test = X[ indices[-test_size:]]
Y_test = Y[ indices[-test_size:]]
#------------------------------------------------------------------------------
#Create the DFLsklearn object and train
#------------------------------------------------------------------------------
print('Initializing and training the MLPClassifier')
estimator_path='sklearn.neural_network.MLPClassifier'
model=DFLsklearn.DFL_estimator(estimator_path=estimator_path,
estimator_param={'random_state':0},
metric='accuracy_score',
minimization=False,
iprint=0)
model.fit(X_train,Y_train)
print('Optimization complete, accuracy score on the test set: %f'
% sklearn.metrics.accuracy_score(model.predict(X_test),Y_test))
def demo_regression():
#------------------------------------------------------------------------------
#Load the dateset and split it in training and test sets
#------------------------------------------------------------------------------
print('Loading the diabetes dataset from sklearn')
dataset=sklearn.datasets.load_diabetes()
X=dataset.data
Y=dataset.target
seed=1
test_size = int( 0.2 * len( Y ) )
numpy.random.seed( seed )
indices = numpy.random.permutation(len(X))
X_train = X[ indices[:-test_size]]
Y_train = Y[ indices[:-test_size]]
X_test = X[ indices[-test_size:]]
Y_test = Y[ indices[-test_size:]]
#------------------------------------------------------------------------------
#Create the DFLsklearn object and train
#------------------------------------------------------------------------------
print('Initializing and training the MLPClassifier')
estimator_path='sklearn.neural_network.MLPRegressor'
model=DFLsklearn.DFL_estimator(estimator_path=estimator_path,
estimator_param={'random_state':0},
metric='r2_score',
minimization=False,
iprint=0)
model.fit(X_train,Y_train)
print('Optimization complete, coef. of determination on the test set: %f'
% sklearn.metrics.r2_score(model.predict(X_test),Y_test))
def demo_custom():
'''
Custom hyperparameters optimization, by setting the formal paramters for a
support vector machine
'''
#------------------------------------------------------------------------------
#Load the dateset and split it in training and test sets
#------------------------------------------------------------------------------
print('Loading the diabetes dataset from sklearn')
dataset=sklearn.datasets.load_diabetes()
X=dataset.data
Y=dataset.target
seed=1
test_size = int( 0.2 * len( Y ) )
numpy.random.seed( seed )
indices = numpy.random.permutation(len(X))
X_train = X[ indices[:-test_size]]
Y_train = Y[ indices[:-test_size]]
X_test = X[ indices[-test_size:]]
Y_test = Y[ indices[-test_size:]]
#------------------------------------------------------------------------------
#Create the DFLsklearn object and train
#------------------------------------------------------------------------------
print('Initializing and training the MLPClassifier')
estimator_path='sklearn.neural_network.MLPRegressor'
model=DFLsklearn.DFL_estimator(estimator_path=estimator_path,
preset_config=0,
base=10.0,
hp_list=['hidden_layer_sizes','alpha'],
hp_init=[(10,10,),-8],
lb=[10,10,-8],
ub=[100,100,8],
step=[1,1,1],
init_int_step=[10,10,1],
is_integer=[1,1,0],
on_a_mesh=[0,0,1],
var_is_int=[0,0,0],
estimator_param={'random_state':1},
iprint=0)
model.fit(X_train,Y_train)
print('Optimization complete, coef. of determination on the test set: %f'
% sklearn.metrics.r2_score(model.predict(X_test),Y_test))
def demo_custom_preset():
'''
Custom hyperparameters optimization, by setting the formal paramters for a
support vector machine
'''
#------------------------------------------------------------------------------
#Load the dateset and split it in training and test sets
#------------------------------------------------------------------------------
print('Loading the diabetes dataset from sklearn')
dataset=sklearn.datasets.load_diabetes()
X=dataset.data
Y=dataset.target
seed=1
test_size = int( 0.2 * len( Y ) )
numpy.random.seed( seed )
indices = numpy.random.permutation(len(X))
X_train = X[ indices[:-test_size]]
Y_train = Y[ indices[:-test_size]]
X_test = X[ indices[-test_size:]]
Y_test = Y[ indices[-test_size:]]
#------------------------------------------------------------------------------
#Create the DFLsklearn object and train
#------------------------------------------------------------------------------
print('Initializing and training the MLPClassifier')
estimator_path='sklearn.neural_network.MLPRegressor'
model=DFLsklearn.DFL_estimator(estimator_path=estimator_path,
preset_function=newpreset,
estimator_param={'random_state':1})
model.fit(X_train,Y_train)
print('Optimization complete, coef. of determination on the test set: %f'
% sklearn.metrics.r2_score(model.predict(X_test),Y_test))
def newpreset(model_f):
n=3
base=10.0
hp_list=['hidden_layer_sizes','alpha']
hp_init=[(10,10,),-8]
lb=[10,10,-8]
ub=[100,100,8]
step=[1,1,1]
init_int_step=[10,10,1]
is_integer=[1,1,0]
on_a_mesh=[0,0,1]
var_is_int=[0,0,0]
return n,hp_init,lb,ub,step,init_int_step,is_integer,base,hp_list,on_a_mesh,var_is_int
if __name__ == '__main__':
demo_classification()
demo_regression()
demo_custom()
demo_custom_preset()