获得猜测的准确性

时间:2017-02-08 01:17:39

标签: python scikit-learn classification text-classification

我目前正在尝试使用this SO question

找到单词列表的可发音性

以下代码如下:

import random
def scramble(s):
    return "".join(random.sample(s, len(s)))

words = [w.strip() for w in open('/usr/share/dict/words') if w == w.lower()]
scrambled = [scramble(w) for w in words]

X = words+scrambled
y = ['word']*len(words) + ['unpronounceable']*len(scrambled)

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y)

from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB

text_clf = Pipeline([
    ('vect', CountVectorizer(analyzer='char', ngram_range=(1, 3))),
    ('clf', MultinomialNB())
    ])

text_clf = text_clf.fit(X_train, y_train)
predicted = text_clf.predict(X_test)

from sklearn import metrics
print(metrics.classification_report(y_test, predicted))

这用随机词输出

>>> text_clf.predict("scaroly".split())
['word']

我一直在检查scikit documentation,但我似乎还无法知道如何打印输入字的分数。

1 个答案:

答案 0 :(得分:1)

尝试sklearn.pipeline.Pipeline.predict_proba

>>> text_clf.predict_proba(["scaroly"])
array([[  5.87363027e-04,   9.99412637e-01]])

它返回给定输入(在本例中为"scaroly")属于您训练模型的类的可能性。因此,"scaroly"可能有99.94%的可能性。

相反,威尔士语中的" new"很可能是不可发音的:

>>> text_clf.predict_proba(["newydd"])
array([[ 0.99666533,  0.00333467]])