gpt4 book ai didi

python - SelectKBest 回归给出 "unknown label type"-error

转载 作者:太空宇宙 更新时间:2023-11-04 08:33:45 25 4
gpt4 key购买 nike

我试图让 SelectKBest 示例的一个稍微修改的版本工作,但不断收到 ValueError("Unknown label type: %s"% repr(ys))

这是我的代码:

# Importing dependencies
import numpy as np
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
from sklearn.datasets import load_iris

#The Example from:
#http://scikit-learn.org/stable/modules/feature_selection.html#univariate-feature-selection
iris = load_iris()
X, Y = iris.data, iris.target
print(X.shape, type(X), type(X[0,0]))
print(Y.shape, type(Y), type(Y[0]))
X_new = SelectKBest(chi2, k=2).fit_transform(X, Y)

#My toyproblem:
X = np.random.uniform(0,1, size=(5000, 10))
Y = np.random.uniform(0,1, size=(5000,))

#Type cast which might solve my problem by thi suggestion:
# https://stackoverflow.com/questions/45346550/valueerror-unknown-label-type-unknown
X=X.astype('float')
Y=Y.astype('float')

print(X.shape, type(X), type(X[0,0]))
print(Y.shape, type(Y), type(Y[0]))

X_new = SelectKBest(chi2, k=2).fit_transform(X, Y)

输入数据的形状完全相同,输入数据类型也几乎相同:

(150, 4) <class 'numpy.ndarray'> <class 'numpy.float64'>
(150,) <class 'numpy.ndarray'> <class 'numpy.int32'>
(5000, 10) <class 'numpy.ndarray'> <class 'numpy.float64'>
(5000,) <class 'numpy.ndarray'> <class 'numpy.float64'>

但是我的代码崩溃并抛出上述的 ValueError。阅读 SelectKBest Doc,从分类到回归的转换应该不是问题。谁能帮我找出问题所在?

Traceback (most recent call last):
File "featureSelection_toyproblem.py", line 26, in <module>
X_new = SelectKBest(chi2, k=2).fit_transform(X, Y)
File "C:\Users\mobrecht\AppData\Local\Continuum\anaconda3\envs\env_zipline\lib
\site-packages\sklearn\base.py", line 520, in fit_transform
return self.fit(X, y, **fit_params).transform(X)
File "C:\Users\mobrecht\AppData\Local\Continuum\anaconda3\envs\env_zipline\lib
\site-packages\sklearn\feature_selection\univariate_selection.py", line 349, in
fit
score_func_ret = self.score_func(X, y)
File "C:\Users\mobrecht\AppData\Local\Continuum\anaconda3\envs\env_zipline\lib
\site-packages\sklearn\feature_selection\univariate_selection.py", line 217, in
chi2
Y = LabelBinarizer().fit_transform(y)
File "C:\Users\mobrecht\AppData\Local\Continuum\anaconda3\envs\env_zipline\lib
\site-packages\sklearn\preprocessing\label.py", line 307, in fit_transform
return self.fit(y).transform(y)
File "C:\Users\mobrecht\AppData\Local\Continuum\anaconda3\envs\env_zipline\lib
\site-packages\sklearn\preprocessing\label.py", line 284, in fit
self.classes_ = unique_labels(y)
File "C:\Users\mobrecht\AppData\Local\Continuum\anaconda3\envs\env_zipline\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.42595241, 0.79859991, 0.22947246, .
.., 0.86011766,
0.52335991, 0.27046173]),)

最佳答案

查看chi2 文档 here ,它只能用于分类。您的玩具问题目标 Y 是真实值而不是标签。

关于python - SelectKBest 回归给出 "unknown label type"-error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50875192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com