跨Val分数返回sklearn中的NAN

时间:2020-05-27 03:32:22

标签: python

我正在从cross_val_score获得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
df = pd.read_csv('creditcard.csv')
df.head()

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='f1').mean()

space = {
    'C' : hp.uniform('C', 0.05, 10),
    '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)

DataSet can be downloaded here

1 个答案:

答案 0 :(得分:0)

在cross_val_score中,尝试使用df.loc[:,:'Amount'].values, df['Class'].values