0

I am getting a ValueError while doing Bayesian optimization for LinearSVC in a multi-label classification problem.

logger = JSONLogger(path=LOGS_PATH)


lSVC_param = {'C':(0.001, 0.01, 0.1, 1, 10),
                   'penalty':('l1','l2'),
                  'loss':('hinge','squared_hinge')}


def optimise_bayes_opt(X, y):
    def target(C_param,penalty_param,loss_param):       
        clf = LinearSVC(C=C_param,penalty=penalty_param,loss=loss_param)
        text_clf = Pipeline([('tfidf', TfidfVectorizer(ngram_range=(1,1),
                                                      norm='l2',
                                                      min_df=1,
                                                      use_idf=True)), 
                             ('clf', OneVsRestClassifier(clf))])
        cv_results = cross_val_score(text_clf, X_test, y_test, scoring='accuracy',cv=5)        
        print("CV",cv_results,cv_results.mean())
        return cv_results.mean()

    optimizer = BayesianOptimization(
                f=target,
                pbounds={'C_param':lSVC_param['C'],
                         'penalty_param':lSVC_param['penalty'],
                         'loss_param':lSVC_param['loss']},
                verbose=2,
                random_state=1)
    optimizer.subscribe(Events.OPTMIZATION_STEP, logger)
    optimizer.maximize(init_points=2, n_iter=2)
    return optimizer


with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    optimizer = optimise_bayes_opt(X_train,y_train)
    best_params = optimizer.max 

print(best_params)

Error

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-269-dc7962d023ef> in <module>
     34 with warnings.catch_warnings():
     35     warnings.simplefilter("ignore")
---> 36     optimizer = optimise_bayes_opt(X_train,y_train)
     37     best_params = optimizer.max
     38 

<ipython-input-269-dc7962d023ef> in optimise_bayes_opt(X, y)
     26                          'loss_param':lSVC_param['loss']},
     27                 verbose=2,
---> 28                 random_state=1)
     29     optimizer.subscribe(Events.OPTMIZATION_STEP, logger)
     30     optimizer.maximize(init_points=2, n_iter=2)

~/.local/lib/python3.6/site-packages/bayes_opt/bayesian_optimization.py in __init__(self, f, pbounds, random_state, verbose)
     71         # Data structure containing the function to be optimized, the bounds of
     72         # its domain, and a record of the evaluations we have done so far
---> 73         self._space = TargetSpace(f, pbounds, random_state)
     74 
     75         # queue

~/.local/lib/python3.6/site-packages/bayes_opt/target_space.py in __init__(self, target_func, pbounds, random_state)
     47         self._bounds = np.array(
     48             [item[1] for item in sorted(pbounds.items(), key=lambda x: x[0])],
---> 49             dtype=np.float
     50         )
     51 

ValueError: setting an array element with a sequence.

I understood this valueError after referring this question. Also, that line mentions that values should be of type float. So the values of C_param are correct and others are not correct. Now I don`t know how to optimize for parameters with non-float values like penalty, loss etc.

joel
  • 1,156
  • 3
  • 15
  • 42

1 Answers1

0

because the shape of the input list isn't a (generalised) "box" that can be turned into a multidimensional array. So probably

  [item[1] for item in sorted(pbounds.items(), key=lambda x: x[0])] 

contains sequences of different lengths.

try change

lSVC_param = {'C':(0.001, 0.01, 0.1, 1, 10),
                   'penalty':('l1','l2'),
                  'loss':('hinge','squared_hinge')

with same length which is sent to pbounds

joyzaza
  • 186
  • 1
  • 8