How to extract each value seperately from the object 'best_params_' returned by gridsearch method by sklearn?

34 Views Asked by At

I am using gridsearch in sklearn using the following code

How should I extract each numeric value i.e. 44.984326689694534 or 0.2811768697974237 from the object grid.best_params_?

param_grid= {'gamma': np.logspace(-9, 9, num=50, base=10),'C': np.logspace(-9, 9, num=50, base=10)}
base_estimator = SVC(kernel='rbf', random_state=42)
grid = HalvingGridSearchCV(base_estimator, param_grid, cv=10, factor=3, min_resources=20).fit(X_train_bal, Y_train_bal)
print(grid.best_params_)

The result is {'C': 44.984326689694534, 'gamma': 0.2811768697974237}

I tried grid.best_params_[0][0] but it gives error.

1

There are 1 best solutions below

0
gtomer On

The result is in a dictionary format (or dict). The first parameter is called key and the second value. If you want to get the value of 'C', for example, you need to:

grid.best_params_['C']

The output would be:

44.984326689694534