Python 值错误:未知标签类型:“未知”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45346550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:56:21  来源:igfitidea点击:

ValueError: Unknown label type: 'unknown'

pythonpandasnumpyscikit-learnlogistic-regression

提问by Ivan Zhovannik

I try to run following code. Btw, I am new to both python and sklearn.

我尝试运行以下代码。顺便说一句,我是 python 和 sklearn 的新手。

import pandas as pd
import numpy as np
from sklearn.linear_model import LogisticRegression


# data import and preparation
trainData = pd.read_csv('train.csv')
train = trainData.values
testData = pd.read_csv('test.csv')
test = testData.values
X = np.c_[train[:, 0], train[:, 2], train[:, 6:7],  train[:, 9]]
X = np.nan_to_num(X)
y = train[:, 1]
Xtest = np.c_[test[:, 0:1], test[:, 5:6],  test[:, 8]]
Xtest = np.nan_to_num(Xtest)


# model
lr = LogisticRegression()
lr.fit(X, y)

where y is a np.ndarray of 0's and 1's

其中 y 是 0 和 1 的 np.ndarray

I receive the following:

我收到以下信息:

File "C:\Anaconda3\lib\site-packages\sklearn\linear_model\logistic.py", line >1174, in fit check_classification_targets(y)

File "C:\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: 'unknown'

文件“C:\Anaconda3\lib\site-packages\sklearn\linear_model\logistic.py”,第 1174 行,适合 check_classification_targets(y)

文件“C:\Anaconda3\lib\site-packages\sklearn\utils\multiclass.py”,第 172 行,>in check_classification_targets raise ValueError("Unknown label type: %r" % y_type)

值错误:未知标签类型:“未知”

from sklearn documentation: http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html#sklearn.linear_model.LogisticRegression.fit

来自 sklearn 文档:http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html#sklearn.linear_model.LogisticRegression.fit

y : array-like, shape (n_samples,) Target values (class labels in classification, real numbers in regression)

y : 类数组,形状 (n_samples,) 目标值(分类中的类标签,回归中的实数)

What is my error?

我的错误是什么?

upd:

更新:

y is array([0.0, 1.0, 1.0, ..., 0.0, 1.0, 0.0], dtype=object) size is (891,)

y 是数组([0.0, 1.0, 1.0, ..., 0.0, 1.0, 0.0], dtype=object) 大小是 (891,)

回答by Miriam Farber

Your yis of type object, so sklearn cannot recognize its type. Add the line y=y.astype('int')right after the line y = train[:, 1].

y是 type object,所以 sklearn 无法识别它的类型。在行y=y.astype('int')之后添加行y = train[:, 1]