python – ValueError:未知标签类型:’unknown’
作者:互联网
我尝试运行以下代码.顺便说一句,我是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)
其中y是0和1的np.ndarray
我收到以下内容:
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’
y:类似数组,形状(n_samples,)
目标值(分类中的类别标签,回归中的实数)
我的错误是什么?
UPD:
y是数组([0.0,1.0,1.0,…,0.0,1.0,0.0],dtype = object)大小是(891,)
解决方法:
您的y是object类型,因此sklearn无法识别其类型.在y = train [:,1]行后面添加y = y.astype(‘int’)行.
标签:logistic-regression,python,pandas,numpy,scikit-learn 来源: https://codeday.me/bug/20190930/1835505.html