gpt4 book ai didi

python - 使用 scikit 处理多标签数据的问题

转载 作者:太空宇宙 更新时间:2023-11-03 11:25:40 25 4
gpt4 key购买 nike

我使用以下代码进行多标签数据分类:-

import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.svm import LinearSVC
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.multiclass import OneVsRestClassifier
from sklearn import preprocessing

X_train = np.array(["new york is a hell of a town",
"new york was originally dutch",
"the big apple is great",
"new york is also called the big apple",
"nyc is nice",
"people abbreviate new york city as nyc",
"the capital of great britain is london",
"london is in the uk",
"london is in england",
"london is in great britain",
"it rains a lot in london",
"london hosts the british museum",
"new york is great and so is london",
"i like london better than new york"])
y_train_text = [[1],[1],[1],[1],[1],[1],[2],[2],[2],[2],[2],[2],[12],[12]]

X_test = np.array(['nice day in nyc',
'welcome to london',
'london is rainy',
'it is raining in britian',
'it is raining in britian and the big apple',
'it is raining in britian and nyc',
'hello welcome to new york. enjoy it here and london too'])
target_names = ['New York', 'London']

lb = preprocessing.MultiLabelBinarizer()
Y = lb.fit_transform(y_train_text)

classifier = Pipeline([
('vectorizer', CountVectorizer()),
('tfidf', TfidfTransformer()),
('clf', OneVsRestClassifier(LinearSVC()))])

classifier.fit(X_train, Y)
predicted = classifier.predict(X_test)

======输出=====

[1, 0, 0],'New York'
[0, 1, 0],'London'
[0, 1, 0],'London'
[0, 1, 0],'London'
[1, 0, 0],'New York'
[0, 0, 0],
[0, 0, 0]]

最后两个预测错误,它们应该都是[0,0,1] for ['New York', 'London']

所以我有这些问题:-1.]我的代码到底有什么问题2.] 这是处理“多标签”数据的正确方法吗?或者有没有其他更好的方法。因为这个和一两个代码是我在互联网上可以找到的关于“多标签”数据的全部内容。而对于二进制分类,有数千个。请帮我解决这个问题

最佳答案

12 不是“1”,“2”是十二,因此

[[1],[1],[1],[1],[1],[1],[2],[2],[2],[2],[2],[2],[12],[12]]

应该是

[[1],[1],[1],[1],[1],[1],[2],[2],[2],[2],[2],[2],[1, 2],[1, 2]]

关于python - 使用 scikit 处理多标签数据的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34496527/

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