-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBaye_op.py
57 lines (41 loc) · 1.73 KB
/
Baye_op.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
# -*- coding: utf-8 -*-
"""
Created on Sun Sep 6 16:26:28 2020
A Python program to perform bayesian optimization in bagging-based PU learning with bayesian optimization
This code is released from the paper of Zhiqiang Zhang in computers and geosciences
authors: Zhiqiang Zhang, Gongwen Wang, Chong Liu, Lizhen Cheng, Deming Sha
email: zq_zhang_geo@126.com, gwwang@cugb.edu.cn
@author: Zhiqiang Zhang
"""
from __future__ import division, print_function
import numpy as np
from sklearn.model_selection import cross_val_score
from sklearn.ensemble import RandomForestClassifier as RFC
from hyperopt import fmin, tpe, Trials
def bestObj(space, n_iter_hopt, kFoldSplits, data_bootstrap, train_label,
seed = 42, metric = 'accuracy'):
def objective(space):
best_score=1.0
metric = 'f1'
model = RFC(**space)
score = 1-cross_val_score(model, data_bootstrap,train_label
, cv=5
, scoring=metric
, verbose=False).mean()
if (score < best_score):
best_score=score
return score
trials = Trials()
best = fmin(objective, space = space
, algo = tpe.suggest
, max_evals = n_iter_hopt
, trials = trials
, rstate = np.random.RandomState(seed)
)
else:
n_estimatorsObj = best['n_estimators']
else:
max_depthObj = best['max_depth']
else:
min_samples_leafObj=best['min_samples_leaf']
return n_estimatorsObj, max_depthObj, min_samples_leafObj