Search code examples
pythonmachine-learninglightgbm

LightGBM Error - length not same as data


I am using lightGBM for finding feature importance but I am getting error LightGBMError: b'len of label is not same with #data' . X.shape (73147, 12) y.shape (73147,)

Code:

from sklearn.model_selection import train_test_split
import lightgbm as lgb

# Initialize an empty array to hold feature importances
feature_importances = np.zeros(X.shape[1])

# Create the model with several hyperparameters
model = lgb.LGBMClassifier(objective='binary', boosting_type = 'goss', n_estimators = 10000, class_weight = 'balanced')

# Fit the model twice to avoid overfitting
for i in range(2):

    # Split into training and validation set
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = i)

    # Train using early stopping
    model.fit(X, y_train, early_stopping_rounds=100, eval_set = [(X_test, y_test)], 
              eval_metric = 'auc', verbose = 200)

    # Record the feature importances
    feature_importances += model.feature_importances_

See screenshot below:

enter image description here


Solution

  • You seem to have a typo in your code; instead of

    model.fit(X, y_train, [...])
    

    it should be

    model.fit(X_train, y_train, [...])
    

    As it is now, it is understandable that the length of X and y_train is not the same, hence your error.