【问题标题】:Get accuracy of guess获得猜测的准确性
【发布时间】:2017-06-25 11:21:58
【问题描述】:

我目前正在尝试使用 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))

This 输出随机单词 this

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

我一直在检查scikit documentation,但我似乎仍然不知道如何让它打印输入单词的分数。

【问题讨论】:

  • 你所说的“分数”到底是什么意思?比如分类器对给定单词的发音有多大信心?
  • @not_a_robot 没错

标签: python scikit-learn classification text-classification


【解决方案1】:

试试sklearn.pipeline.Pipeline.predict_proba:

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

它返回给定输入(在本例中为"scaroly")属于您训练模型的类的可能性。所以"scaroly" 有 99.94% 的几率是发音的。

相反,威尔士语中的“新”一词可能无法发音:

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

【讨论】:

    猜你喜欢
    • 2017-08-07
    • 2021-10-18
    • 2020-06-01
    • 1970-01-01
    • 2011-11-04
    • 2015-11-21
    • 2018-10-18
    • 2020-03-23
    相关资源
    最近更新 更多