如何获取数据的特征名称?

How to get the features names for the data?

我对 python 环境中包含的鸢尾花数据集使用了以下说明

iris_data=load_iris()
feature_names = iris_data.feature_names 
k= tree.export_text(model.estimators_[i],feature_names)

我通过这个形状得到规则

"""\
|--- petal length (cm) <= 2.35
|   |--- class: 0.0
|--- petal length (cm) >  2.35
|   |--- petal width (cm) <= 1.65
|   |   |--- class: 1.0
|   |--- petal width (cm) >  1.65
|   |   |--- petal width (cm) <= 1.75
|   |   |   |--- sepal length (cm) <= 5.80
|   |   |   |   |--- class: 2.0"

但是当我尝试对外部数据集使用相同的指令时出现此错误 AttributeError: 'DataFrame' 对象没有属性 'feature_names'
当我试图通过这条指令获取特征名称时也是如此

fnm = list(dataset.columns.values.tolist())
k= tree.export_text(model.estimators_[i],fnm)

并使用与 expert_text 相同的行我得到以下错误File "C:\Anaconda3\lib\site-packages\sklearn\tree_export.py", line 886, in export_textlen(feature_names))) ValueError: feature_names must contain 3 elements, got 53

方法 sklearn.datasets.load_iris returns 具有 feature_names 属性的 sklearn.utils.Bunch 对象。

您的新数据集是一个具有 columns 属性的 pandas.DataFrame 对象。