【发布时间】:2015-12-04 08:12:42
【问题描述】:
from sklearn.feature_extraction.text import TfidfVectorizer
import numpy as np
from sklearn import linear_model
arr=['dogs cats lions','apple pineapple orange','water fire earth air', 'sodium potassium calcium']
vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(arr)
feature_names = vectorizer.get_feature_names()
Y = ['animals', 'fruits', 'elements','chemicals']
T=["eating apple roasted in fire and enjoying fresh air"]
test = vectorizer.transform(T)
clf = linear_model.SGDClassifier(loss='log')
clf.fit(X,Y)
x=clf.predict(test)
#prints: elements
在上面的代码中,<strong>clf.predict()</strong> 只为 list X 中的样本打印 1 个最佳预测。
我对 list X 中特定样本的 前 3 个预测 感兴趣,我知道函数 <strong>predict_proba</strong>/<strong>predict_log_proba</strong> 返回每个特征的所有概率的列表列表 Y 中,但它必须先排序,然后与列表 Y 中的特征关联,然后才能获得 前 3 个结果。
有没有直接有效的方法?
【问题讨论】:
-
@user1269942:非常有用的补充!但是,我并不完全理解“真相”变量的功能。能详细点吗?
标签: python scikit-learn multilabel-classification