'Getting the error: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I have written a function that takes on features, target and split them into train and test. furthermore, it fits a regressor lightgbm model and use RandomizedSearchCV followed by fitting it.

Code looks like this:

def search_lgb_regressor(X, y, categorical_cols, test_size=0.2):

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, random_state=1110)
params = {
    'n_estimators': [500],
    'max_bin': [10, 50],
    'min_child_samples': [20, 50],
    'subsample': [0.7, 1],
    'learning_rate': [0.1, 0.05],
    'num_leaves': [32, 64, 128],
    'max_depth': [4, 7],
    'colsample_bytree': [.7, 1]
}
print('Hyperparameter optimization using gridsearch ...')
model = LGBMRegressor(boosting_type='gbdt', verbosity=1)
search = RandomizedSearchCV(model, params, scoring='neg_root_mean_squared_error',n_iter=25, cv=3, n_jobs=-1, random_state=42)
search.fit(X_train, y_train, categorical_feature=categorical_cols,eval_set=[(X_test, y_test)], early_stopping_rounds = 10)

the X and y have shape: (1400, 261) (1400, )

The error is exactly on search.fit part where it says: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

How to resolve this?



Solution 1:[1]

I was giving this function an incorrect list of categorical features which gave the error

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rida Amjad