我使用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
如何解决这个问题?
使用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')
这不应该给你任何错误。