0

I used scorecardpy function to get a model:

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

Then I tried to save this model using following code:

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

After that I tried to reload this model:

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

Then I want to use the model to get the scores:

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

But it gives error:

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

How to resolve this problem?

2
  • 1
    This is (apparently) a bug inside the scorecard module. What version are you using, and is there a newer one available that might fix the bug? Commented Nov 24, 2024 at 4:24
  • scorecardpy package version is 0.1.9.7 Commented Nov 26, 2024 at 0:55

1 Answer 1

-1

Using Joblib instead of Numpy will solve this problem. Remember to place import joblib at the start of your program, and replace

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

with

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

This should not give you any errors.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.