编辑: 我正在使用以下代码。我使用平均精确度作为指标。在下面的代码中,在打印语句中,我得到了准确的结果。但是,当我使用其他数据集时,它可以正常工作。我试图将数据作为数组传递,即使这样我也得到了nan值。谁能解释我发生了什么事。
from sklearn.model_selection import KFold, cross_val_score
from sklearn.model_selection import train_test_split
import hyperopt
from hyperopt import tpe
from hyperopt import STATUS_OK
from hyperopt import Trials
from hyperopt import hp
from hyperopt import fmin
from sklearn.linear_model import LogisticRegression
def hyperopt_train_test(params):
cv = StratifiedShuffleSplit(n_splits = 1, test_size = .25, random_state = 0 )
clf =LogisticRegression(**params)
return cross_val_score(clf, df.loc[:,:'Amount'], df['Class'],cv = cv,scoring='average_precision').mean()
space = {
'C' : hp.uniform('C', 0.05, 1000),
'penalty': hp.choice('penalty',['l2','l1']),
'max_iter':hp.choice('max_iter',[200,300,400,500])
}
def f(params,scores=[]):
acc = hyperopt_train_test(params)
print(acc, "Accuracy")
scores.append(acc)
return {'scores':scores,'loss': 1-acc, 'status': STATUS_OK, 'scores':scores}
trials = Trials()
trials
best = fmin(f, space, algo=tpe.suggest, max_evals=10, trials=trials)
print('best:',best)
hyperopt.space_eval(space,best)