Python 属性错误:';LGBM回归器';对象没有属性';功能名称';
在使用下面的代码训练模型之后,我试图获取特性名称,然后遇到了这样的错误 我已经检查了lightgbm的文档,lightgbm.LGBMRegressionor的属性为“feature\u name\uUx” (这是链接) 我在jupyter笔记本上运行这个,我的lightGBM版本是2.3.1 我真的不知道,有人能给我一个线索吗Python 属性错误:';LGBM回归器';对象没有属性';功能名称';,python,scikit-learn,jupyter-notebook,lightgbm,Python,Scikit Learn,Jupyter Notebook,Lightgbm,在使用下面的代码训练模型之后,我试图获取特性名称,然后遇到了这样的错误 我已经检查了lightgbm的文档,lightgbm.LGBMRegressionor的属性为“feature\u name\uUx” (这是链接) 我在jupyter笔记本上运行这个,我的lightGBM版本是2.3.1 我真的不知道,有人能给我一个线索吗 from lightgbm import LGBMRegressor from sklearn.metrics import mean_squared_error fr
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
这是错误日志
---------------------------------------------------------------------------
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_'
---------------------------------------------------------------------------
AttributeError回溯(最近一次呼叫上次)
在里面
32打印('要素重要性:',列表(gbm.要素重要性))
33
--->34打印(“功能名称”,gbm.功能名称)
AttributeError:“LGBMRegressionr”对象没有属性“feature\u name”
非常感谢你 如以下所述:
feature_name_uu属性最近已合并到master中,尚未包含在任何正式版本中。您可以从源代码下载夜间构建或安装
注意,为了访问特征名称,您必须向Regressionor传递一个熊猫df,而不是numpy数组:
data = pd.DataFrame(iris.data, columns=iris.feature_names)
因此,记住这一点,即使没有feature\u name\u
属性,您也可以只执行以下操作:
iris.feature_names
正如上面所述:
feature_name_uu属性最近已合并到master中,尚未包含在任何正式版本中。您可以从源代码下载夜间构建或安装
注意,为了访问特征名称,您必须向Regressionor传递一个熊猫df,而不是numpy数组:
data = pd.DataFrame(iris.data, columns=iris.feature_names)
因此,记住这一点,即使没有feature\u name\u
属性,您也可以只执行以下操作:
iris.feature_names
要获取
LGBMRegressor
或lightgbm
的任何其他ML模型类的功能名称,您可以使用存储此模型的基础增压器的booster\uu
属性
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)
boost = gbm.booster_
print('Feature names',boost.feature_name())
要获取
LGBMRegressor
或lightgbm
的任何其他ML模型类的功能名称,您可以使用存储此模型的基础增压器的booster\uu
属性
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)
boost = gbm.booster_
print('Feature names',boost.feature_name())