Search code examples
pythonscikit-learnjupyter-notebooklightgbm

AttributeError: 'LGBMRegressor' object has no attribute 'feature_name_'


I was trying to get the feature name after training the model using the code below, then i ran into such error.

I have checked the docs for lightgbm, lightgbm.LGBMRegressor has the attribute 'feature_name_',

(this is the link https://lightgbm.readthedocs.io/en/latest/pythonapi/lightgbm.LGBMRegressor.html#lightgbm.LGBMRegressor)

I was running this on jupyter notebook , and my lightGBM version is 2.3.1

I really have no idea, can someone give me a clue??

from lightgbm import LGBMRegressor
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import GridSearchCV
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.externals import joblib

# load data
iris = load_iris()
data = iris.data
target = iris.target

# split dataset
X_train, X_test, y_train, y_test = train_test_split(data, target, test_size=0.2)

# training
gbm = LGBMRegressor(objective='regression', num_leaves=31, learning_rate=0.05, n_estimators=20)
gbm.fit(X_train, y_train, eval_set=[(X_test, y_test)], eval_metric='l1', early_stopping_rounds=5)

# save the model
joblib.dump(gbm, 'loan_model.pkl')


# load the model
gbm = joblib.load('loan_model.pkl')

y_pred = gbm.predict(X_test, num_iteration=gbm.best_iteration_)

print('The rmse of prediction is:', mean_squared_error(y_test, y_pred) ** 0.5)

# importances and feature_name_
print('Feature importances:', list(gbm.feature_importances_))

print('Feature names',gbm.feature_name_)# this is where went wrong

This is the error log

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-1-d982fd40dcd0> in <module>
     32 print('Feature importances:', list(gbm.feature_importances_))
     33 
---> 34 print('Feature names',gbm.feature_name_)

AttributeError: 'LGBMRegressor' object has no attribute 'feature_name_'

Thank u a lot!


Solution

  • As it's stated on github:

    feature_name_ attribute has been merged in master recently and is not included in any official release yet. You can download nightly build or install from sources

    Note, in order to access feature names, you had to pass to regressor a pandas df, not a numpy array:

    data = pd.DataFrame(iris.data, columns=iris.feature_names)
    

    So, with this in mind, even without feature_name_ attribute, you may do just:

    iris.feature_names