首页 > 解决方案 > ValueError:特征名称的长度,2330 与特征数量不匹配,5064

问题描述

我正在解决决策树分类问题。我无法绘制决策树图。代码如下:

pima = pd.read_csv("Data.csv", sep=';', encoding ='latin1')
feature_cols = ['Name', 'Material number', 'Production status', 'PKD', 'Marketing Indicator',
             'Windmill release', 'Component type', 'Rail', 'token name']
features_df = pd.get_dummies(pima, columns=['Name', 'Material number', 'Production status', 'PKD', 'Marketing Indicator','Windmill release', 'Component type', 'Rail', 'token name'], dummy_na=True)
X = features_df.as_matrix()
y = pima['Error'].as_matrix()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=1)
clf = DecisionTreeClassifier()
clf = clf.fit(X_train,y_train)
y_pred = clf.predict(X_test)
print(y_pred)
dot_data = StringIO()
export_graphviz(clf, out_file=dot_data,filled=True, rounded=True,special_characters=True,feature_names = features_df,class_names=['0','1'])
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())  
graph.write_png('thesis.png')
Image(graph.create_png())

我收到一个错误

ValueError: Length of feature_names, 2330 does not match number of features, 5064

请给一些建议。

这是回溯:

C:/Users/Drashti Bhatt/Desktop/thesis.py:27: FutureWarning: Method .as_matrix will be removed in a future version. Use .values instead.
  X = features_df.as_matrix()
C:/Users/Drashti Bhatt/Desktop/thesis.py:28: FutureWarning: Method .as_matrix will be removed in a future version. Use .values instead.
  y = pima['Error'].as_matrix()
[0 1 1 ... 1 1 0]
C:\Users\Drashti Bhatt\Anaconda3\lib\site-packages\sklearn\externals\six.py:31: DeprecationWarning: The module is deprecated in version 0.21 and will be removed in version 0.23 since we've dropped support for Python 2.7. Please rely on the official version of six (https://pypi.org/project/six/).
  "(https://pypi.org/project/six/).", DeprecationWarning)
Traceback (most recent call last):
File "<ipython-input-1-a6b45e60092a>", line 1, in <module> runfile('C:/Users/Drashti Bhatt/Desktop/thesis.py', wdir='C:/Users/Drashti Bhatt/Desktop')
File "C:\Users\Drashti Bhatt\Anaconda3\lib\site packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)
File "C:\Users\Drashti Bhatt\Anaconda3\lib\site packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/Drashti Bhatt/Desktop/thesis.py", line 55, in <modulespecial_characters=True,feature_names = features_df,class_names=['0','1'])
File "C:\Users\Drashti Bhatt\Anaconda3\lib\site-packages\sklearn\tree\export.py", line 776, in export_graphviz exporter.export(decision_tree)
File "C:\Users\Drashti Bhatt\Anaconda3\lib\site-packages\sklearn\tree\export.py", line 401, in export decision_tree.n_features_))
ValueError: Length of feature_names, 2330 does not match number of features, 5064```




标签: pythonscikit-learn

解决方案


推荐阅读