引发ValueError(“未知标签类型:%s”%repr(ys))ValueError:未知标签类型:(数组

时间:2018-10-17 06:40:02

标签: machine-learning scipy valueerror

我正在尝试采用机器学习方法,但是遇到了一些问题。这是我的代码:

import sys
import scipy
import numpy
import matplotlib
import pandas
import sklearn

from pandas.plotting import scatter_matrix
import matplotlib.pyplot as plt
from sklearn import model_selection
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC

dataset = pandas.read_csv('Libro111.csv')
array = numpy.asarray(dataset,dtype=numpy.float64) #all values are float64

X = array[:,1:49]
Y = array[:,0]
validation_size = 0.2
seed = 7.0
X_train, X_validation, Y_train, Y_validation = model_selection.train_test_split(X, Y, test_size=validation_size, random_state=seed)

scoring = 'accuracy'
models = []
models.append(('LR', LogisticRegression()))
models.append(('LDA', LinearDiscriminantAnalysis()))
models.append(('KNN', KNeighborsClassifier()))
models.append(('CART', DecisionTreeClassifier()))
models.append(('NB', GaussianNB()))
models.append(('SVM', SVC()))
results = []
names = []
for name, model in models:
    kfold = model_selection.KFold(n_splits=10, random_state=seed)
    cv_results = model_selection.cross_val_score(model, X_train, Y_train, cv=kfold, scoring=scoring)
    results.append(cv_results)
    names.append(name)
    msg = "%s: %f (%f)" % (name, cv_results.mean(), cv_results.std())
    print(msg)

然后我得到两个不同的错误。

对于Logistic回归:

File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\multiclass.py", line 172, in check_classification_targets
    raise ValueError("Unknown label type: %r" % y_type)

ValueError: Unknown label type: 'continuous'

我找到了一个有同样问题的人,但是我还无法解决。.

(最重要的是)

File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\multiclass.py", line 97, in unique_labels
    raise ValueError("Unknown label type: %s" % repr(ys))

ValueError: Unknown label type: (array([ 0.5,  0. ,  1. ,  1. ,  0.5,  0.5,  1. ,  0.5,  0. ,  0.5,  1. ,
        0. ,  0. ,  0. ,  1. ,  1......

在两种情况下,当我执行“ cv_result”行时都会出现错误...因此,希望您能为我提供帮助...

1 个答案:

答案 0 :(得分:0)

“ ValueError:未知标签类型:'continuous'”表示您的“ Y”值不是数据的类类型(多行共享相同的整数值,每个整数代表一个类)。因此,您不能使用“ DecisionTreeClassifier”,“ KNeighborsClassifier”,“ LogisticRegression”(不要被其名称所迷惑,LogisticRegression是布尔分类方法)或任何其他分类机器学习方法。实际上,您的“ Y”值都是不同的或“连续的”(可能是浮点数),因此您只能使用回归机器学习(即“ RandomForestRegressor”)。

以下是两种解决方案:

a)将Y值分组到bin(类)中。对数据应用分类建模。

b)如果您希望预测具有值(浮点数),则需要使用回归机器学习方法来预测Y值。

顺便说一句,“评分='准确性'”评估方法用于分类建模。