首页 > 解决方案 > 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),)**

标签: pythonnumpy

解决方案


推荐阅读