unhashable type: 'dict' when sampling values for class_weight in BayesSearchCV

82 Views Asked by At

I'm trying to tune the hyperparameters of a model using BayesSearchCV. This model have a class_weight parameter which is associated with classes in the form {class_label: weight}. The problem is I'm getting a unhashable type: 'dict' error message when I try to run the code. How can I fix this?

class_weights = [{0:1, 1:x} for x in np.arange(1.0, 5.0, 0.1)]
params = {'C': np.arange(1.0, 5.0, 0.1),
          'class_weight': class_weights}

bs_cv = BayesSearchCV(LogisticRegression(), params, cv=50, n_iter=5, random_state=42, scoring=auprc, verbose=False)
history = bs_cv.fit(X_train, y_train)
best_bayes = bs_cv.best_estimator_

print("Test set AUPRC: {}".format(bs_cv.score(X_test, y_test)))
print("Best parameters are: {}".format(bs_cv.best_params_))

The error message:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-433-d9f9e9b7926b> in <module>
      3           'class_weight': class_weights}
      4 
----> 5 bs_cv = BayesSearchCV(LogisticRegression(), params, cv=50, n_iter=5, random_state=42, scoring=auprc, verbose=False)
      6 history = bs_cv.fit(X_train, y_train)
      7 best_bayes = bs_cv.best_estimator_

6 frames
/usr/local/lib/python3.8/dist-packages/skopt/space/transformers.py in <dictcomp>(.0)
    111             List of categories.
    112         """
--> 113         self.mapping_ = {v: i for i, v in enumerate(X)}
    114         self.inverse_mapping_ = {i: v for v, i in self.mapping_.items()}
    115         self._lb.fit([self.mapping_[v] for v in X])

TypeError: unhashable type: 'dict'
0

There are 0 best solutions below