Apologies for the error

docx

School

Birla Institute of Technology, Mesra *

*We aren’t endorsed by this school

Course

AI

Subject

Computer Science

Date

Nov 24, 2024

Type

docx

Pages

2

Uploaded by UltraCrown1625

Report
Apologies for the error. It seems that the `param_grid` dictionary cannot be directly passed to the `set_params()` method of the XGBoost model because it expects a mapping object. To resolve this issue, you can modify the code as follows: ```python import xgboost as xgb from tqdm import tqdm from sklearn.metrics import accuracy_score from sklearn.model_selection import ParameterGrid from sklearn.model_selection import train_test_split # Initialize tqdm tqdm.pandas() # Define the parameter grid for grid search param_grid = { 'subsample': [0.8, 0.9, 1], 'colsample_bytree': [0.6, 0.7, 0.8], 'max_depth': [2, 3, 4], 'min_child_weight': [10, 15, 20], 'learning_rate': [0.01, 0.05, 0.1], 'n_estimators': [500, 1000, 2000] } # Split the data into training and validation sets train_x, val_x, train_y, val_y = train_test_split(X, y, test_size=0.2, random_state=42) # Create an instance of the XGBoost classifier xgb_model = xgb.XGBClassifier(objective='binary:logistic', n_jobs=-1) # Create a list to store the results of each parameter combination results = [] # Define the TqdmCallback class class TqdmCallback: def __init__(self, total): self.total = total self.pbar = tqdm(total=total, desc="Grid Search") def __call__(self, *args, **kwargs): self.pbar.update(1) # Perform the grid search param_combinations = list(ParameterGrid(param_grid)) total_combinations = len(param_combinations) for params in tqdm(param_combinations, total=total_combinations, desc="Grid Search"):
# Set the current parameter combination in the XGBoost model xgb_model.set_params(**params) # Fit the model xgb_model.fit(train_x, train_y, verbose=False, eval_metric=['error', 'logloss'], early_stopping_rounds=25, eval_set=[(train_x, train_y), (val_x, val_y)]) # Perform evaluation and get the mean accuracy score predictions = xgb_model.predict(val_x) mean_score = accuracy_score(val_y, predictions) # Store the result results.append((params, mean_score)) # Get the best parameter combination and its mean accuracy score best_params, best_score = max(results, key=lambda x: x[1]) print("Best Model Parameters:", best_params) print("Best Score:", best_score) ``` In this updated code, we use the `ParameterGrid` from `sklearn.model_selection` to generate all combinations of parameters from `param_grid`. We then iterate over each parameter combination, set the parameters in the XGBoost model using `set_params()`, fit the model, and evaluate it using the validation data. The best parameter combination is selected based on the highest mean accuracy score. Make sure to replace `X` and `y` with your actual dataset and adjust the code accordingly. I apologize for any inconvenience caused, and I hope this solution resolves the issue.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help