1
$\begingroup$

I am new to ML, and XGB is really confusing me. I understand that for Python XGB can be imported directly from the xgb library or via SKLearn.

The methods for xgb from the direct xgb library also differ from SKLearn xgb. Eg xgb library uses xgb.train while SKLearn xgb uses fit.

I tried the xgb from SKLearn xgb, but got the below error:

TypeError: fit() missing 1 required positional argument: 'y'

  1. What is wrong with my below code?
  2. And is the direct xgb better than SKLearn xgb?
    Would appreciate some help. Thank you
from xgboost.sklearn import XGBClassifier
from sklearn.utils import class_weight
from sklearn.model_selection import train_test_split

X1=s_matrix2 # a sparse  matrix
Y1=df['Label'].values 


X_train, X_test, y_train, y_test = train_test_split(X1, Y1, random_state=0, test_size=0.2)
classes_weights = class_weight.compute_sample_weight(
    class_weight='balanced',
    y=y_train
)

xgb_model=XGBClassifier.fit(X_train, y_train, sample_weight=classes_weights)
``` 
$\endgroup$
1
$\begingroup$

First the classifier needs to be created. Then it can be fit. sklearn API works in an object oriented interface here - create the object, call methods on the object.

...
xgb_model = xgb.XGBClassifier(parameters that you want for this model, find options in the documentation [here](https://xgboost.readthedocs.io/en/latest/python/python_api.html#xgboost.XGBClassifier) )

xgb_model.fit(X_train, y_train, sample_weight=classes_weights)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.