gpt4 book ai didi

python - Knn 分类器的 Scikit learn 中出现“不支持多类多输出”错误

转载 作者:行者123 更新时间:2023-11-30 09:16:14 26 4
gpt4 key购买 nike

我有两个变量 X 和 Y。

X 的结构(即 np.array):

[[26777 24918 26821 ...    -1    -1    -1]
[26777 26831 26832 ... -1 -1 -1]
[26777 24918 26821 ... -1 -1 -1]
...
[26811 26832 26813 ... -1 -1 -1]
[26830 26831 26832 ... -1 -1 -1]
[26830 26831 26832 ... -1 -1 -1]]

Y的结构:

[[1252, 26777, 26831], [1252, 26777, 26831], [1252, 26777, 26831], [1252, 26777, 26831], [1252, 26777, 26831], [1252, 26777, 26831], [25197, 26777, 26781], [25197, 26777, 26781], [25197, 26777, 26781], [26764, 25803, 26781], [26764, 25803, 26781], [25197, 26777, 26781], [25197, 26777, 26781], [1252, 26777, 16172], [1252, 26777, 16172]]

Y 中的数组,例如 [1252, 26777, 26831] 是三个独立的特征。

我正在使用 scikit learn 模块中的 Knn 分类器

classifier = KNeighborsClassifier(n_neighbors=3)
classifier.fit(X,Y)
predictions = classifier.predict(X)
print(accuracy_score(Y,predictions))

但我收到一条错误消息:

ValueError: multiclass-multioutput is not supported

我猜“Y”的结构不受支持,我需要进行哪些更改才能执行程序?

输入:

  Deluxe Single room with sea view

预期输出:

c_class = Deluxe
c_occ = single
c_view = sea

最佳答案

如错误中所述,KNN 不支持多输出回归/分类。

对于您的问题,您需要 MultiOutputClassifier() .

from sklearn.multioutput import MultiOutputClassifier

knn = KNeighborsClassifier(n_neighbors=3)
classifier = MultiOutputClassifier(knn, n_jobs=-1)
classifier.fit(X,Y)

工作示例:

>>> from sklearn.feature_extraction.text import TfidfVectorizer
>>> corpus = [
... 'This is the first document.',
... 'This document is the second document.',
... 'And this is the third one.',
... 'Is this the first document?',
... ]
>>> vectorizer = TfidfVectorizer()
>>> X = vectorizer.fit_transform(corpus)

>>> Y = [[124323,1234132,1234],[124323,4132,14],[1,4132,1234],[1,4132,14]]

>>> from sklearn.multioutput import MultiOutputClassifier
>>> from sklearn.neighbors import KNeighborsClassifier
>>> knn = KNeighborsClassifier(n_neighbors=3)
>>> classifier = MultiOutputClassifier(knn, n_jobs=-1)
>>> classifier.fit(X,Y)
>>> predictions = classifier.predict(X)

array([[124323, 4132, 14],
[124323, 4132, 14],
[ 1, 4132, 1234],
[124323, 4132, 14]])

>>> classifier.score(X,np.array(Y))
0.5

>>> test_data = ['I want to test this']
>>> classifier.predict(vectorizer.transform(test_data))
array([[124323, 4132, 14]])

关于python - Knn 分类器的 Scikit learn 中出现“不支持多类多输出”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55489792/

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