值错误:未知标签类型:“未知" [英] ValueError: Unknown label type: 'unknown'

查看:41
本文介绍了值错误:未知标签类型:“未知"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I try to run following code. Btw, I am new to both python and 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)

其中 y 是 0 和 1 的 np.ndarray

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

我收到以下信息:

文件C:Anaconda3libsite-packagessklearnlinear_modellogistic.py",第 1174 行,适合check_classification_targets(y)

File "C:Anaconda3libsite-packagessklearnlinear_modellogistic.py", line >1174, in fit check_classification_targets(y)

文件C:Anaconda3libsite-packagessklearnutilsmulticlass.py",第 172 行,>在 check_classification_targetsraise ValueError("未知标签类型:%r" % y_type)

File "C:Anaconda3libsite-packagessklearnutilsmulticlass.py", line 172, >in check_classification_targets raise ValueError("Unknown label type: %r" % y_type)

ValueError: Unknown label type: 'unknown'

ValueError: Unknown label type: 'unknown'

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

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

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

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

我的错误是什么?

更新:

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

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

推荐答案

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

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

这篇关于值错误:未知标签类型:“未知"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆