我目前正在尝试使用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
>>> text_clf.predict("scaroly".split())
['word']
我一直在检查scikit documentation,但似乎仍然找不到如何打印输入单词的分数的方法。
最佳答案
尝试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]])
关于python - 获得猜测的准确性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42103002/