|
9 | 9 | from bayes_opt import BayesianOptimization |
10 | 10 |
|
11 | 11 | # Load data set and target values |
12 | | -data, target = make_classification(n_samples=1000, |
13 | | - n_features=45, |
14 | | - n_informative=12, |
15 | | - n_redundant=7) |
| 12 | +data, target = make_classification( |
| 13 | + n_samples=1000, |
| 14 | + n_features=45, |
| 15 | + n_informative=12, |
| 16 | + n_redundant=7 |
| 17 | +) |
16 | 18 |
|
17 | 19 | def svccv(C, gamma): |
18 | | - return cross_val_score(SVC(C=C, gamma=gamma, random_state=2), |
19 | | - data, target, 'f1', cv=2).mean() |
| 20 | + val = cross_val_score( |
| 21 | + SVC(C=C, gamma=gamma, random_state=2), |
| 22 | + data, target, 'f1', cv=2 |
| 23 | + ).mean() |
| 24 | + |
| 25 | + return val |
20 | 26 |
|
21 | 27 | def rfccv(n_estimators, min_samples_split, max_features): |
22 | | - return cross_val_score(RFC(n_estimators=int(n_estimators), |
23 | | - min_samples_split=int(min_samples_split), |
24 | | - max_features=min(max_features, 0.999), |
25 | | - random_state=2), |
26 | | - data, target, 'f1', cv=2).mean() |
| 28 | + val = cross_val_score( |
| 29 | + RFC(n_estimators=int(n_estimators), |
| 30 | + min_samples_split=int(min_samples_split), |
| 31 | + max_features=min(max_features, 0.999), |
| 32 | + random_state=2 |
| 33 | + ), |
| 34 | + data, target, 'f1', cv=2 |
| 35 | + ).mean() |
| 36 | + return val |
27 | 37 |
|
28 | 38 | if __name__ == "__main__": |
29 | | - gp_params = {"alpha": 1e5} |
| 39 | + gp_params = {"alpha": 1e-5} |
30 | 40 |
|
31 | | - svcBO = BayesianOptimization(svccv, {'C': (0.001, 100), 'gamma': (0.0001, 0.1)}) |
| 41 | + svcBO = BayesianOptimization(svccv, |
| 42 | + {'C': (0.001, 100), 'gamma': (0.0001, 0.1)}) |
32 | 43 | svcBO.explore({'C': [0.001, 0.01, 0.1], 'gamma': [0.001, 0.01, 0.1]}) |
33 | 44 |
|
34 | | - rfcBO = BayesianOptimization(rfccv, {'n_estimators': (10, 250), |
35 | | - 'min_samples_split': (2, 25), |
36 | | - 'max_features': (0.1, 0.999)}) |
| 45 | + rfcBO = BayesianOptimization( |
| 46 | + rfccv, |
| 47 | + {'n_estimators': (10, 250), |
| 48 | + 'min_samples_split': (2, 25), |
| 49 | + 'max_features': (0.1, 0.999)} |
| 50 | + ) |
37 | 51 |
|
38 | 52 | svcBO.maximize(n_iter=10, **gp_params) |
39 | | - print('-'*53) |
| 53 | + print('-' * 53) |
40 | 54 | rfcBO.maximize(n_iter=10, **gp_params) |
41 | 55 |
|
42 | | - print('-'*53) |
| 56 | + print('-' * 53) |
43 | 57 | print('Final Results') |
44 | 58 | print('SVC: %f' % svcBO.res['max']['max_val']) |
45 | 59 | print('RFC: %f' % rfcBO.res['max']['max_val']) |
0 commit comments