Python Scorecardpy:UnboundLocalError:赋值前引用了局部变量“card_df”

问题描述 投票:0回答:1

我使用scorecardpy函数来获取模型:

import scorecardpy as ac
card=sc.scorecard(bins_adj, lr, X_train.columns)

然后我尝试使用以下代码保存此模型:

import numpy as np
np.save('card.npy',card)

之后我尝试重新加载这个模型:

card=np.load('card.npy',allow_pickle=True)

然后我想用模型来得到分数:

score=sc.scorecard_ply(data_train, card, print_step=0)

但它给出了错误:

UnboundLocalError          Traceback (most recent call last)
Cell In [91], line 1
score=sc.scorecard_ply(data_train, card, print_step=0)

File ~/.local/lib/python3.9/site-packages/scorecardpy/scorecard.py:330, in scorecard_ply(dt, card, only_total_score, print_step, replace_blank_na, var_kp)
     card_df=card.copy(deep=True)
  # x variables
  xs=card_df.loc[card_df.variable != 'basepoints', 'variable'].unique()
  # length of x variables
  xs_len=len(xs)
  
UnboundLocalError: local variable 'card_df' referenced before assignment

如何解决这个问题?

python python-3.x machine-learning logistic-regression python-3.9
1个回答
0
投票

使用Joblib代替Numpy可以解决这个问题。请记住将

import joblib
放在程序的开头,然后替换

import numpy as np
np.save('card.npy',card)
card=np.load('card.npy',allow_pickle=True)

joblib.dump(card, 'card.joblib')
card = joblib.load('card.joblib')

这不应该给你任何错误。

© www.soinside.com 2019 - 2024. All rights reserved.