下面是我正在尝试的代码。 我有一个词汇表和模型泡菜文件,我正在同时加载它们和 检查预测,但是在那之后我也想得分,因此我正在使用以下代码,在其中可以找到模型中预测字符串的索引。classes_我正在使用np.where模块,但不是在函数内部工作,但在函数外部工作。
我的代码:
import os
import numpy as np
from sklearn.externals import joblib
def predict_score(vect, model, search_key):
prediction = ''
index = ''
score = ''
prediciton = model.predict(vect.transform([search_key]))[0]
index = np.squeeze(np.where(model.classes_ == prediction))
# this line block is giving **(array([], dtype=int64),)**
score = model.predict_proba(vect.transform([search_key]))[:,
index][0]
return prediciton, score
vect = joblib.load(r'vect.pkl')
model = joblib.load(r'model.pkl')
print(predict_score(vect, model, 'Help to predict me'))
print(np.where(model.classes_ == predict_score(vect, model, 'Help to
predict me')[0]))
# this is working as expected **(array([42], dtype=int64),)**