通过Hyperopt解决的最佳参数不合适

我使用hyperopt来搜索SVM分类器的最佳参数,但Hyperopt表示最佳的’kernel’是’0’。{‘kernel’: ‘0’} 显然是不合适的。

是否有人知道这是我的错误还是hyperopt的一个bug导致的?

代码如下。

from hyperopt import fmin, tpe, hp, randimport numpy as npfrom sklearn.metrics import accuracy_scorefrom sklearn import svmfrom sklearn.cross_validation import StratifiedKFoldparameter_space_svc = {   'C':hp.loguniform("C", np.log(1), np.log(100)),   'kernel':hp.choice('kernel',['rbf','poly']),   'gamma': hp.loguniform("gamma", np.log(0.001), np.log(0.1)),    }from sklearn import datasetsiris = datasets.load_digits()train_data = iris.datatrain_target = iris.targetcount = 0def function(args):  print(args)  score_avg = 0  skf = StratifiedKFold(train_target, n_folds=3, shuffle=True, random_state=1)  for train_idx, test_idx in skf:    train_X = iris.data[train_idx]    train_y = iris.target[train_idx]    test_X = iris.data[test_idx]    test_y = iris.target[test_idx]    clf = svm.SVC(**args)    clf.fit(train_X,train_y)    prediction = clf.predict(test_X)    score = accuracy_score(test_y, prediction)    score_avg += score  score_avg /= len(skf)  global count  count = count + 1  print("round %s" % str(count),score_avg)  return -score_avgbest = fmin(function, parameter_space_svc, algo=tpe.suggest, max_evals=100)print("best estimate parameters",best)

输出如下。

best estimate parameters {'C': 13.271912841932233, 'gamma': 0.0017394328334592358, 'kernel': 0}

回答:

首先,你使用的是sklearn.cross_validation,该模块自0.18版本起已被弃用。因此请更新为sklearn.model_selection

现在回到主要问题,来自fminbest总是返回使用hp.choice定义的参数的索引。

所以在你的例子中,'kernel':0意味着选择第一个值('rbf')作为kernel的最佳值。

请查看以下链接以确认这一点:

要从best中获取原始值,请使用space_eval()函数,如下所示:

from hyperopt import space_evalspace_eval(parameter_space_svc, best)Output:{'C': 13.271912841932233, 'gamma': 0.0017394328334592358, 'kernel': 'rbf'}

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注