1

我正在使用 lightGBM 来查找特征重要性,但出现错误LightGBMError: b'len of label is not same with #data'。X.shape (73147, 12) y.shape (73147,)

代码:

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_

请看下面的截图:

在此处输入图像描述

4

1 回答 1

1

您的代码中似乎有错字;代替

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

它应该是

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

就像现在一样,X和的长度y_train不一样是可以理解的,因此你的错误。

于 2018-06-27T14:18:25.097 回答