python - np.where 在函数内部不起作用,但在外部起作用
问题描述
下面是我正在尝试的代码。我有一个词汇表和模型pickle文件,我正在加载它们并检查预测,但之后我也想要得分,因此我使用以下代码在哪里可以找到预测字符串的索引model.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),)**
解决方案
推荐阅读
- regex - 如何在 perl 中将 XX1/XXSomething/XX1/Something 之类的模式更改为 XXSomething/XX1/Something
- vb.net - 如何在字符串列表中对字符串类型日期进行排序?
- javascript - 无法为我的反应应用禁用 LiveReloading webapck 开发服务器
- python - 如何获取外键对象以在 django rest 框架中显示完整对象
- activiti - Activiti:添加任务局部变量即席
- python - 使用递归实现二分查找功能
- c - 在 C 程序中列出全局数组变量
- php - 在 opencart home.twig 上显示不同类别的产品
- node.js - 如何使用braintree BTDropInController 来识别所选支付选项的paymentMethod Token 以创建订阅?
- c# - 我将如何合并两个数据库,即一个在普通的旧 sql 服务器中创建,另一个在实体框架中创建?