【发布时间】:2015-11-02 14:47:30
【问题描述】:
我是 scikit 的新手,所以玩弄它。
问题背景: 我正在尝试在hackerRank 上玩“Byte the correct apple”比赛。 其中我们有两个文件,一个包含 apple the company 的文本,另一个包含 apple the fruit。现在我们必须从中学习,然后对新文本进行预测。
虽然代码运行,但我的问题是: - 因为'line'(在下面的代码中)是一个单一的输入,我应该得到一个数字输出零或一。但我得到一个数组作为输出。 - 我是否已经接近使用下面的代码学习任何东西了?
import numpy as np
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.linear_model import SGDClassifier
from sklearn import svm
from sklearn.svm import LinearSVC
from sklearn.pipeline import Pipeline
appleComputers = [];
appleFruits = [];
labels = [];
with open('apple-computers.txt','r') as f:
for line in f:
appleComputers.append(line)
labels.append(1);
with open('apple-fruit.txt','r') as f:
for line in f:
appleFruits.append(line)
labels.append(0);
text = appleComputers + appleFruits;
labels = np.asarray(labels)
#text_clf = Pipeline([('vect', CountVectorizer()),('tfidf', TfidfTransformer()),('clf', MultinomialNB()),])
text_clf = Pipeline([('vect', CountVectorizer()),('tfidf', TfidfTransformer()),('clf', LinearSVC(loss='hinge', penalty='l2')),])
text_clf = text_clf.fit(text, labels)
line = 'I am talking about apple the fruit we eat.'
line = 'I am talking about the product apple computer by Steve Jobs'
predicted = text_clf.predict(line);
print predicted
【问题讨论】:
-
我是否正确假设输出是
[0]或[1] -
是的,你是对的。输出为 0 或 1。
标签: python machine-learning scikit-learn text-classification