scorecardpy

repository·master·Indexed 21 days ago

https://github.com/shichenxie/scorecardpy

A Python implementation of the R 'scorecard' package for developing traditional credit risk scorecards. It provides tools for data partitioning, variable selection using Information Value (IV), Weight of Evidence (WOE) binning, scorecard scaling, and performance evaluation including Population Stability Index (PSI) analysis.

Tokens
1.4K
Snippets
3
Records
4
Agent score
24%

What's inside scorecardpy

  1. Core functions of scorecardpy

    master

    The scorecardpy package provides a suite of tools for developing traditional credit risk scorecard models. The core workflow involves:

    • Data Partitioning: Using split_df to divide datasets.
    • Variable Selection: Using iv (Information Value) and var_filter to select relevant features.
    • WOE Binning: Using woebin for Weight of Evidence binning, woebin_plot for visualization, woebin_adj for adjustment, and woebin_ply to transform data into WOE values.
    • Scorecard Scaling: Using scorecard to create the scaling object and scorecard_ply to apply it to data.
    • Performance Evaluation: Using perf_eva for model performance and perf_psi for Population Stability Index (PSI) analysis.
  2. Perform Weight of Evidence (WOE) binning and adjustment

    master

    The woebin function is used to create bins based on Weight of Evidence. You can adjust these bins either interactively or by providing a manual dictionary of breaks.

    To apply manual breaks, pass a dictionary to the breaks_list parameter in sc.woebin. The keys should be the column names, and the values should be lists of break points.

    Once bins are defined, use sc.woebin_ply to transform your training and testing DataFrames into their corresponding WOE values.

    # Generate initial bins
    bins = sc.woebin(dt_s, y="creditability")
    
    # Define manual breaks for specific columns
    breaks_adj = {
        'age.in.years': [26, 35, 40],
        'other.debtors.or.guarantors': ["none", "co-applicant%,%guarantor"]
    }
    
    # Re-run binning with manual breaks
    bins_adj = sc.woebin(dt_s, y="creditability", breaks_list=breaks_adj)
    
    # Transform data into WOE values
    train_woe = sc.woebin_ply(train, bins_adj)
    test_woe = sc.woebin_ply(test, bins_adj)
  3. Develop a full credit risk scorecard workflow

    master

    A typical end-to-end workflow using scorecardpy follows these steps:

    1. Prepare Data: Load data and filter variables using sc.var_filter.
    2. Split Data: Use sc.split_df to create training and testing sets.
    3. Binning: Perform WOE binning with sc.woebin and transform data with sc.woebin_ply.
    4. Modeling: Train a logistic regression model (e.g., using sklearn.linear_model.LogisticRegression) on the WOE-transformed training data.
    5. Evaluation: Evaluate performance using sc.perf_eva and check stability with sc.perf_psi.
    6. Scaling: Generate a scorecard using sc.scorecard and apply it to raw data using sc.scorecard_ply to get final credit scores.
    import scorecardpy as sc
    from sklearn.linear_model import LogisticRegression
    
    # 1. Data Preparation
    dat = sc.germancredit()
    dt_s = sc.var_filter(dat, y="creditability")
    train, test = sc.split_df(dt_s, 'creditability').values()
    
    # 2. WOE Binning
    bins_adj = sc.woebin(dt_s, y="creditability")
    train_woe = sc.woebin_ply(train, bins_adj)
    test_woe = sc.woebin_ply(test, bins_adj)
    
    y_train = train_woe.loc[:,'creditability']
    X_train = train_woe.loc[:,train_woe.columns != 'creditability']
    y_test = test_woe.loc[:,'creditability']
    X_test = test_woe.loc[:,train_woe.columns != 'creditability']
    
    # 3. Logistic Regression
    lr = LogisticRegression(penalty='l1', C=0.9, solver='saga', n_jobs=-1)
    lr.fit(X_train, y_train)
    
    # 4. Performance Evaluation
    train_pred = lr.predict_proba(X_train)[:,1]
    test_pred = lr.predict_proba(X_test)[:,1]
    sc.perf_eva(y_train, train_pred, title = "train")
    sc.perf_eva(y_test, test_pred, title = "test")
    
    # 5. Scorecard Scaling
    card = sc.scorecard(bins_adj, lr, X_train.columns)
    train_score = sc.scorecard_ply(train, card, print_step=0)
    test_score = sc.scorecard_ply(test, card, print_step=0)
    
    # 6. PSI
    sc.perf_psi(
      score = {'train':train_score, 'test':test_score},
      label = {'train':y_train, 'test':y_test}
    )
  4. Install scorecardpy

    master

    You can install the stable release of scorecardpy from PyPI or the latest development version directly from GitHub.

    To install the release version:

    pip install scorecardpy

    To install the latest version from GitHub:

    pip install git+git://github.com/shichenxie/scorecardpy.git