LinkWindy 2022-11-08 16:08 采纳率: 100%
浏览 50
已结题

python在赋值前引用的局部变量

我在想要使用保存的模型提取模型的SHAP 值来解释经过训练的 ML 模型时遇到了赋值前引用的局部变量这个问题
import tensorflow as tf
from tensorflow.keras.models import save_model, load_model

tf.keras.backend.set_floatx('float64')
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
import pickle
import argparse
import os
import shap
from DNN_functions import nanmean_squared_error

'''
python step35.py --modelname 'DNN_BLR_fing'
'''


def SHAP(args):
    modeltype = args.modelname.split('_')[0]
    imputation = args.modelname.split('_')[1]
    features = args.modelname.split('_')[2]

    maindirectory = os.getcwd() + '/models/' + args.modelname

    X_df = pd.read_csv(os.getcwd() + '/datasets/datasetAX_' + features + '.csv')
    os.chdir(maindirectory)

    # obtain the proper Xscaler
    X = np.array(X_df)
    if features == 'desc':
        Xscaler = StandardScaler()
        X = Xscaler.fit_transform(X)

    if modeltype == 'RF':
        filename = args.modelname + '.sav'
        model = pickle.load(open(filename, 'rb'))

        background = X
        explainer = shap.TreeExplainer(model)
        shap_values = explainer.shap_values(background)

    if modeltype == 'DNN':
        max_index = 15

        background = X
        avg_shap_values = np.zeros((6, X.shape[0], X.shape[1]))
        for i in range(max_index + 1):
            print('Calculating SHAP values ' + str(i) + '/' + str(max_index + 1))
            directory = maindirectory + '/DNN_' + str(i)
            model = tf.keras.models.load_model(directory,
                                               custom_objects={'nanmean_squared_error': nanmean_squared_error})
            e = shap.explainers._deep.Deep(model, background)
            shap_values = e.shap_values(background)
            avg_shap_values = avg_shap_values + shap_values

        avg_shap_values = avg_shap_values / (max_index + 1)
        shap_values = avg_shap_values

    Columns = ['H2', 'He', 'O2', 'N2', 'CO2', 'CH4']
    for i in range(len(shap_values)):
        gas = Columns[i]
        np.savetxt('shap_' + str(i) + '.csv', shap_values[i], delimiter=",")
    print('SHAP values saved to ' + maindirectory)


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--modelname', type=str, required=True,
                        help='provide the name of the folder in the "model" folder')
    parsed_args = parser.parse_args()

    SHAP(parsed_args)

运行结果及报错内容
File "step35.py", line 81, in <module>
    SHAP(parsed_args)
  File "step35.py", line 69, in SHAP
    for i in range(len(shap_values)):
UnboundLocalError: local variable 'shap_values' referenced before assignment

请问大家要怎样修改我的这个脚本中的变量呢
  • 写回答

5条回答

      报告相同问题?

      相关推荐 更多相似问题

      问题事件

      • 系统已结题 11月16日
      • 已采纳回答 11月8日
      • 创建了问题 11月8日

      悬赏问题

      • ¥15 template模板的参数问题
      • ¥15 C++程序设计 搭建项目框架
      • ¥50 搭建青柚H5过程中出现的问题
      • ¥15 查找处理学生信息问题,含多个文件,显示问题是无法调用其中一个文件
      • ¥15 simulink生成代码后提示告警
      • ¥16 jieba提取高频词,生成文件是空的
      • ¥15 怎么读取服务器中的文件去配置mongo的连接
      • ¥20 Python如何统计文本中两字及以上的词语个数
      • ¥15 MapReduce自定义对象怎么写
      • ¥15 看下哪里错了,程序应该没错