python-3.x - 从多类分类算法输出前 2 个类
问题描述
我正在研究 text 的多类分类问题,其中我有很多不同的类(15+)。我已经训练了一个 Linearsvc svm 方法(方法只是示例)。但是它只输出概率最高的单个类,有没有一种算法可以同时输出两个类
我正在使用的示例代码:
from sklearn.svm import LinearSVC
import matplotlib.pyplot as plt
from sklearn.feature_extraction.text import TfidfVectorizer,CountVectorizer
count_vect = CountVectorizer(max_df=.9,min_df=.002,
encoding='latin-1',
ngram_range=(1, 3))
X_train_counts = count_vect.fit_transform(df_upsampled['text'])
tfidf_transformer = TfidfTransformer(sublinear_tf=True,norm='l2')
X_train_tfidf = tfidf_transformer.fit_transform(X_train_counts)
clf = LinearSVC().fit(X_train_tfidf, df_upsampled['reason'])
y_pred = model.predict(X_test)
电流输出:
source user time text reason
0 hi neha 0 0:neha:hi 1
1 there ram 1 1:ram:there 1
2 ball neha 2 2:neha:ball 3
3 item neha 3 3:neha:item 6
4 go there ram 4 4:ram:go there 7
5 kk ram 5 5:ram:kk 1
6 hshs neha 6 6:neha:hshs 2
7 ggsgs neha 7 7:neha:ggsgs 15
所需的输出:
source user time text reason reason2
0 hi neha 0 0:neha:hi 1 2
1 there ram 1 1:ram:there 1 6
2 ball neha 2 2:neha:ball 3 7
3 item neha 3 3:neha:item 6 4
4 go there ram 4 4:ram:go there 7 9
5 kk ram 5 5:ram:kk 1 2
6 hshs neha 6 6:neha:hshs 2 3
7 ggsgs neha 7 7:neha:ggsgs 15 1
如果我只在一列中获得输出就可以了,因为我可以从中拆分并制作两列。
解决方案
LinearSVC
没有提供predict_proba
,但它提供了decision_function
与超平面的有符号距离。
从文档:
决策函数(自我,X):
预测样本的置信度分数。
样本的置信度分数是该样本到超平面的有符号距离。
基于@warped 评论,
我们可以使用decision_function
输出,从模型中找到n
预测最高的类。
import pandas as pd
from sklearn.datasets import make_classification
from sklearn.svm import LinearSVC
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
X, y = make_classification(n_samples=1000,
n_clusters_per_class=1,
n_informative=10,
n_classes=5, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2,
random_state=42)
clf = make_pipeline(StandardScaler(),
LinearSVC(random_state=0, tol=1e-5))
clf.fit(X, y)
top_n_classes = 2
predictions = clf.decision_function(
X_test).argsort()[:,-top_n_classes:][:,::-1]
pred_df = pd.DataFrame(predictions,
columns= [f'{i+1}_pred' for i in range(top_n_classes)])
df = pd.DataFrame({'true_class': y_test})
df = df.assign(**pred_df)
df
推荐阅读
- azure - 自定义脚本扩展是否只能临时使用?
- javascript - 向后循环仅打印未定义
- css - 如何在 Django 中加载字体
- php - 电子邮件发送后如何得到回复。Symfony 4.3 邮件组件
- python - 查找大于或等于较短搜索列中每个值的较大列中的第一个值
- php - 如何在 laravel 中使用 dompdf 将 boucle 中的数据从控制器传递到查看
- django-rest-framework - 如何在 Django RESTFramework 中将查询集作为 JSON 响应返回?
- python - 尽管进行了转换,如何修复类型错误?
- html - 在 typescript 中将 JSON 对象转换为 html 表格格式
- python - Python 从 SQL Server 导出 Blob