My code is as follows:
from sklearn.model_selection import train_test_split
from xgboost import XGBClassifier
import pandas as pd
RANDOM_STATE = 55 ## You will pass it to every sklearn call so we ensure reproducibility
n = int(len(X_train)*0.8) ## Let's use 80% to train and 20% to eval
This will replace the columns with the one-hot encoded ones and keep the columns outside 'columns' argument as it is.
df = pd.read_csv("doc/heart.csv")
cat_variables = ['Sex',
'ChestPainType',
'RestingECG',
'ExerciseAngina',
'ST_Slope'
]
df = pd.get_dummies(data = df,
prefix = cat_variables,
columns = cat_variables)
var = [x for x in df.columns if x not in 'HeartDisease'] ## Removing our target variable
X_train, X_test, y_train, y_test = train_test_split(df[var], df['HeartDisease'], train_size = 0.8, random_state = RANDOM_STATE)
print(X_train.shape)
X_train_fit, X_train_eval, y_train_fit, y_train_eval = X_train[:n], X_train[n:], y_train[:n], y_train[n:]
import xgboost
print(xgboost.__version__) # 2.1.0
xgb_model = XGBClassifier(n_estimators = 500, learning_rate = 0.1,verbosity = 1, random_state = RANDOM_STATE)
xgb_model.fit(X_train_fit,y_train_fit, eval_set = [(X_train_eval,y_train_eval)],early_stopping_rounds = 10)
The detailed error message is as follows:
Traceback (most recent call last):
File "C:\my_document\11_Python\exercise\main.py", line 153, in <module>
xgb_model.fit(X_train_fit,y_train_fit, eval_set = [(X_train_eval,y_train_eval)],early_stopping_rounds = 10)
File "C:\Users\samc\AppData\Local\Programs\Python\Python312\Lib\site-packages\xgboost\core.py", line 726, in inner_f
return func(**kwargs)
^^^^^^^^^^^^^^
TypeError: XGBClassifier.fit() got an unexpected keyword argument 'early_stopping_rounds'
How can I resolve it?
Try passing the early_stopping_rounds
parameter to the XGBClassifier
rather than to the fit
method which does not have a early_stopping_rounds
parameter.
So using your code:
xgb_model = XGBClassifier(n_estimators = 500, learning_rate = 0.1,verbosity = 1, random_state = RANDOM_STATE, early_stopping_rounds = 10)
xgb_model.fit(X_train_fit,y_train_fit, eval_set = [(X_train_eval,y_train_eval)])
Reference: https://xgboost.readthedocs.io/en/stable/python/python_api.html#xgboost.XGBClassifier