ValueError:模型的要素数必须与输入匹配。模型n_features为45,输入n_features为2

时间:2018-04-10 09:44:13

标签: python-3.x machine-learning visualization random-forest valueerror

我正在尝试使用python 3绘制随机森林可视化以进行分类。

首先,我读取了所有必要数据所在的CSV文件。在这里,Read_CSV()是一个正确运行的方法,提供三个变量,功能(带有所有功能名称的向量,特别是45个),数据(仅限没有标签列的数据) 。有148000行和45列),标签(整数格式的标签列。有3个类可归类为整数0,1或2.此向量中还有148000行)。

    features,data,labels = Read_CSV()

    X_train,X_test,Y_train,Y_test = train_test_split(data,labels,test_size=0.35,random_state=0)

    X = np.array(X).astype(np.float)
    y = np.array(y).astype(np.float)
    ax = ax or plt.gca()
    ax.scatter(X[:, 0], X[:, 1], c=y, s=30, cmap=cmap,
               clim=(y.min(), y.max()), zorder=3)
    ax.axis('tight')
    ax.axis('off')
    xlim = ax.get_xlim()
    ylim = ax.get_ylim()
    # fit the estimator
    model.fit(X, y)
    xx, yy = np.meshgrid(np.linspace(*xlim, num=200),
                         np.linspace(*ylim, num=200))
    Z = model.predict(np.c_[xx.ravel(), yy.ravel()]).reshape(xx.shape)
    # Create a color plot with the results
    n_classes = len(np.unique(y))
    contours = ax.contourf(xx, yy, Z, alpha=0.3,
                           levels=np.arange(n_classes + 1) - 0.5,
                           cmap=cmap, clim=(y.min(), y.max()),
                           zorder=1)
    ax.set(xlim=xlim, ylim=ylim)

这部分代码显示完全致力于获得这样的情节:

enter image description here

当我运行此代码时,我获得以下内容:

Traceback (most recent call last):
  File "C:/Users/Carles/PycharmProjects/Article/main.py", line 441, in <module>
    main()
  File "C:/Users/Carles/PycharmProjects/Article/main.py", line 388, in main
    visualize_classifier(RandomForestClassifier(),X_train, Y_train)
  File "C:/Users/Carles/PycharmProjects/Article/main.py", line 353, in visualize_classifier
    Z = model.predict(np.c_[xx.ravel(), yy.ravel()]).reshape(xx.shape)
  File "C:\Users\Carles\PycharmProjects\Article\venv\lib\site-packages\sklearn\ensemble\forest.py", line 538, in predict
    proba = self.predict_proba(X)
  File "C:\Users\Carles\PycharmProjects\Article\venv\lib\site-packages\sklearn\ensemble\forest.py", line 578, in predict_proba
    X = self._validate_X_predict(X)
  File "C:\Users\Carles\PycharmProjects\Article\venv\lib\site-packages\sklearn\ensemble\forest.py", line 357, in _validate_X_predict
    return self.estimators_[0]._validate_X_predict(X, check_input=True)
  File "C:\Users\Carles\PycharmProjects\Article\venv\lib\site-packages\sklearn\tree\tree.py", line 384, in _validate_X_predict
    % (self.n_features_, n_features))
ValueError: Number of features of the model must match the input. Model n_features is 45 and input n_features is 2 

0 个答案:

没有答案