【问题标题】:Running a classifer on trainset not working在火车上运行分类器不起作用
【发布时间】:2019-07-22 22:13:37
【问题描述】:

我正在使用 IRIS 数据集 (Iris.data) https://archive.ics.uci.edu/ml/machine-learning-databases/iris/

我首先准备了数据集进行分类,然后将数据集拆分为训练集和测试数据集

#Preparing for Data Classification X = np.array(iris.iloc[:, :-1].values) Y = np.array(iris.iloc[:, :-1].values)

#Splitting the iris data set into the training set and test set X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2, random_state = 1)

我在这里收到错误

#Run the fit using KNeighborsClassifier from sklearn.neighbors.
#First, instantiate the model then, run the classifier on the training set.
classifier = KNeighborsClassifier(n_neighbors=3)
classifier.fit(X_train, Y_train)
y_pred = classifier.predict(X_test)

我收到以下错误

---------------------------------------------------------------------------
`ValueError                  Traceback (most recent call last)                   <ipython-input-78-7c6d6884854b> in <module>()
2 #First, instantiate the model then, run the classifier on the training set.
3 classifier = KNeighborsClassifier(n_neighbors=3)
----> 4 classifier.fit(X_train, Y_train)
5 y_pred = classifier.predict(X_test)
~\Anaconda3\lib\site-packages\sklearn\neighbors\base.py in fit(self,  X, y)
777             self.outputs_2d_ = True
778 
--> 779         check_classification_targets(y)
780         self.classes_ = []
781         self._y = np.empty(y.shape, dtype=np.int)~\Anaconda3\lib\site-packages\sklearn\utils\multiclass.py in check_classification_targets(y)
169     if y_type not in ['binary', 'multiclass', 'multiclass-multioutput',
170                       'multilabel-indicator', 'multilabel-sequences']:
--> 171         raise ValueError("Unknown label type: %r" % y_type)
172 
173 
ValueError: Unknown label type: 'continuous-multioutput'`

【问题讨论】:

  • import pandas as pd import matplotlib.pyplot as plt import seaborn as sns %matplotlib inline import numpy as np from sklearn.model_selection import train_test_split from sklearn.neighbors import KNeighborsClassifier from sklearn.metrics import accuracy_score

标签: python python-3.x machine-learning knn


【解决方案1】:

ValueError: Unknown label type: 'continuous-multioutput' 表示输入类型错误。在这种特定情况下,问题是Y。它是连续的和多输出的(即多特征的),这两者都不适合分类器。

原因是在前几行中,您将XY 设置为相同的内容。您对X 的定义似乎是正确的。但是,您对Y 的定义不正确,应更改为Y = np.array(iris.iloc[:, -1].values)。 (您在 -1 之前有一个多余的冒号。)

【讨论】:

    猜你喜欢
    • 2021-02-17
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    • 2011-12-05
    • 1970-01-01
    • 2017-02-21
    • 1970-01-01
    • 2016-10-24
    相关资源
    最近更新 更多