Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best parameters solved by Hyperopt is unsuitable

I used hyperopt to search best parameters for SVM classifier, but Hyperopt says best 'kernel' is '0'. {'kernel': '0'} is obviously unsuitable.

Does anyone know whether it's caused by my fault or a bag of hyperopt ?

Code is below.

from hyperopt import fmin, tpe, hp, rand
import numpy as np
from sklearn.metrics import accuracy_score
from sklearn import svm
from sklearn.cross_validation import StratifiedKFold

parameter_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 datasets
iris = datasets.load_digits()

train_data = iris.data
train_target = iris.target

count = 0

def 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_avg

best = fmin(function, parameter_space_svc, algo=tpe.suggest, max_evals=100)
print("best estimate parameters",best)

Output is below.

best estimate parameters {'C': 13.271912841932233, 'gamma': 0.0017394328334592358, 'kernel': 0}
like image 885
横尾修平 Avatar asked Aug 14 '17 12:08

横尾修平


People also ask

What is HP Quniform?

hp. quniform(label, low, high, q) Returns a value like round(uniform(low, high) / q) * q. Suitable for a discrete value with respect to which the objective is still somewhat "smooth", but which should be bounded both above and below. hp.

What is HyperOpt Sklearn?

HyperOpt and HyperOpt-Sklearn HyperOpt is an open-source Python library for Bayesian optimization developed by James Bergstra. It is designed for large-scale optimization for models with hundreds of parameters and allows the optimization procedure to be scaled across multiple cores and multiple machines.


1 Answers

First, you are using sklearn.cross_validation which has been deprecated as of version 0.18. So please update that to sklearn.model_selection.

Now to the main issue, the best from fmin always returns the index for parameters defined using hp.choice.

So in your case, 'kernel':0 means that the first value ('rbf') is selected as best value for kernel.

See this issue which confirms this:

  • https://github.com/hyperopt/hyperopt/issues/216

To get the original values from best, use space_eval() function like this:

from hyperopt import space_eval
space_eval(parameter_space_svc, best)

Output:
{'C': 13.271912841932233, 'gamma': 0.0017394328334592358, 'kernel': 'rbf'}
like image 154
Vivek Kumar Avatar answered Nov 03 '22 10:11

Vivek Kumar