Scorecard
- class optbinning.scorecard.Scorecard(binning_process: BinningProcess, estimator: object, scaling_method: str | None = None, scaling_method_params: dict | None = None, intercept_based: bool = False, reverse_scorecard: bool = False, rounding: bool = False, verbose: bool = False, target_dtype: str | None = None)
Bases:
Base,BaseEstimatorScorecard development given a binary or continuous target dtype.
- Parameters:
binning_process (object) – A
BinningProcessinstance.estimator (object) – A supervised learning estimator with a
fitandpredictmethod that provides information about feature coefficients through acoef_attribute. For binary classification, the estimator must include apredict_probamethod.scaling_method (str or None (default=None)) – The scaling method to control the range of the scores. Supported methods are “pdo_odds” and “min_max”. Method “pdo_odds” is only applicable for binary classification. If None, no scaling is applied.
scaling_method_params (dict or None (default=None)) – Dictionary with scaling method parameters. If
scaling_method="pdo_odds"parameters required are: “pdo”, “odds”, and “scorecard_points”. Ifscaling_method="min_max"parameters required are “min” and “max”. Ifscaling_method=None, this parameter is not used.intercept_based (bool (default=False)) – Build a intercept-based scorecard. A intercept-based scorecard modifies the original scorecard by setting the smallest point for each variable to zero and updating the intercept accordingly.
reverse_scorecard (bool (default=False)) – Whether to change the sense of the relationship between predictions and scorecard points to ascending/descending.
rounding (bool (default=False)) – Whether to round scorecard points. If
scaling_method="min_max"a mixed-integer programming problem is solved to guarantee the minimum/maximum score after rounding. Otherwise, the scorecard points are round to the nearest integer.target_dtype (str or None, optional (default=None)) –
The target type, one of “binary” or “continuous”. If None, uses
binning_process.target_dtypewhen supplied, otherwise inferred viasklearn.utils.multiclass.type_of_target. Set explicitly to override auto-detection, e.g. for an integer-valued continuous target (see GH issue #296).Added in version 1.1.0.
verbose (bool (default=False)) – Enable verbose output.
- binning_process_
The external binning process.
- Type:
object
- estimator_
The external estimator fit on the reduced dataset.
- Type:
object
- intercept_
The intercept if
intercept_based=True.- Type:
float
- decision_function(X: DataFrame) ndarray
Predict confidence scores for samples. The confidence score for a sample is proportional to the signed distance of that sample to the hyperplane.
- Parameters:
X (pandas.DataFrame (n_samples, n_features)) – The data matrix for which we want to get the confidence scores.
- Returns:
scores – Confidence scores per (n_samples, n_classes) combination.
- Return type:
array of shape (n_samples, n_classes)
- fit(X: DataFrame, y: list | NDArray, sample_weight: list | NDArray | None = None, metric_special: float | str = 0, metric_missing: float | str = 0, show_digits: int = 2, check_input: bool = False) Self
Fit scorecard.
- Parameters:
X (pandas.DataFrame (n_samples, n_features)) – Training vector, where n_samples is the number of samples.
y (array-like of shape (n_samples,)) – Target vector relative to x.
sample_weight (array-like of shape (n_samples,) (default=None)) – Array of weights that are assigned to individual samples. If not provided, then each sample is given unit weight. Weights are used for the weighted bin counts and statistics (WoE, IV, event rate or mean, p-value), for any
prebinning_method. Withprebinning_method="cart", they are also used to fit the decision tree. This option is only available for binary and continuous targets.metric_special (float or str (default=0)) – The metric value to transform special codes in the input vector. Supported metrics are “empirical” to use the empirical WoE or event rate, and any numerical value.
metric_missing (float or str (default=0)) – The metric value to transform missing values in the input vector. Supported metrics are “empirical” to use the empirical WoE or event rate and any numerical value.
check_input (bool (default=False)) – Whether to check input arrays.
show_digits (int, optional (default=2)) – The number of significant digits of the bin column.
- Returns:
self – Fitted scorecard.
- Return type:
- information(print_level: int = 1) None
Print overview information about the options settings and statistics.
- Parameters:
print_level (int (default=1)) – Level of details.
- classmethod load(path: str) Scorecard
Load scorecard from pickle file.
- Parameters:
path (str) – Pickle file path.
Example
>>> from optbinning import Scorecard >>> scorecard = Scorecard.load("my_scorecard.pkl")
- predict(X: DataFrame) ndarray
Predict using the fitted underlying estimator and the reduced dataset.
- Parameters:
X (pandas.DataFrame (n_samples, n_features)) – Training vector, where n_samples is the number of samples.
- Returns:
pred – The predicted target values.
- Return type:
array of shape (n_samples)
- predict_proba(X: DataFrame) ndarray
Predict class probabilities using the fitted underlying estimator and the reduced dataset.
- Parameters:
X (pandas.DataFrame (n_samples, n_features)) – Training vector, where n_samples is the number of samples.
- Returns:
p – The class probabilities of the input samples.
- Return type:
array of shape (n_samples, n_classes)
- save(path: str) None
Save scorecard to pickle file.
- Parameters:
path (str) – Pickle file path.
- score(X: DataFrame) ndarray
Score of the dataset.
- Parameters:
X (pandas.DataFrame (n_samples, n_features)) – Training vector, where n_samples is the number of samples.
- Returns:
score – The score of the input samples.
- Return type:
array of shape (n_samples)
- table(style: str = 'summary') DataFrame
Scorecard table.
- Parameters:
style (str, optional (default="summary")) – Scorecard’s style. Supported styles are “summary” and “detailed”. Summary only includes columns variable, bin description and points. Detailed contained additional columns with bin information and estimator coefficients.
- Returns:
table – The scorecard table.
- Return type:
pandas.DataFrame
- transform(X: DataFrame) DataFrame
Transform the dataset in to scores.
- Parameters:
X (pandas.DataFrame (n_samples, n_features)) – Training vector, where n_samples is the number of samples.
- Returns:
score (dataframe of shape (n_samples, n_features)) – The score of the input samples.
import pandas as pd
from optbinning import Scorecard, BinningProcess
from sklearn.datasets import load_breast_cancer
data = load_breast_cancer()
X_train = pd.DataFrame( – data=data.data, columns=data.feature_names
)
y_train = data.target
- model = Scorecard(
binning_process=BinningProcess(variable_names=data.feature_names), estimator=LogisticRegression(random_state=34), rounding=True, scaling_method=”min_max”, scaling_method_params={“min”: 0, “max”: 250}
) model.fit(X_train, y_train)
Monitoring
- class optbinning.scorecard.ScorecardMonitoring(scorecard: Scorecard, psi_method: str = 'cart', psi_n_bins: int = 20, psi_min_bin_size: float = 0.05, show_digits: int = 2, verbose: bool = False)
Bases:
BaseEstimatorScorecard monitoring.
- Parameters:
scorecard (object) – A
Scorecardfitted instance.psi_method (str, optional (default="cart")) – The binning method to compute the Population Stability Index (PSI). Supported methods are “cart” for a CART decision tree, “quantile” to generate prebins with approximately same frequency and “uniform” to generate prebins with equal width. Method “cart” uses sklearn.tree.DecistionTreeClassifier.
psi_n_bins (int (default=20)) – The maximum number of bins to compute PSI.
psi_min_bin_size (float (default=0.05)) – The fraction of mininum number of records for PSI bin.
show_digits (int, optional (default=2)) – The number of significant digits of the bin column.
verbose (bool (default=False)) – Enable verbose output.
- fit(X_actual: DataFrame, y_actual: list | NDArray, X_expected: DataFrame, y_expected: list | NDArray) Self
Fit monitoring with actual and expected data.
- Parameters:
X_actual (pandas.DataFrame) – New/actual/test data input samples.
y_actual (array-like of shape (n_samples,)) – Target vector relative to X actual.
X_expected (pandas.DataFrame) – Trainning data used for fitting the scorecard.
y_expected (array-like of shape (n_samples,)) – Target vector relative to X expected.
- Returns:
self – Fitted monitoring.
- Return type:
- information(print_level: int = 1) None
Print overview information about the options settings and statistics.
- Parameters:
print_level (int (default=1)) – Level of details.
- psi_plot(savefig: str | None = None) None
Plot Population Stability Index (PSI).
- Parameters:
savefig (str or None (default=None)) – Path to save the plot figure.
- property psi_splits: ndarray
List of splits points used to compute system PSI.
- Returns:
splits
- Return type:
numpy.ndarray
- psi_table() DataFrame
System Population Stability Index (PSI) table.
- Returns:
psi_table
- Return type:
pandas.DataFrame
- psi_variable_table(name: str | None = None, style: str = 'summary') DataFrame
Population Stability Index (PSI) at variable level.
- Parameters:
name (str or None (default=None)) – The variable name. If name is None, a table with all variables is returned.
style (str, optional (default="summary")) – Supported styles are “summary” and “detailed”. Summary only includes the total PSI for each variable. Detailed includes the PSI for each variable at bin level.
- Returns:
psi_table
- Return type:
pandas.DataFrame
- system_stability_report() None
Print overview information and statistics about system stability. It includes qualitative suggestions regarding the necessity of scorecard updates.
- tests_table() DataFrame
Compute statistical tests to determine if event rate (Chi-square test - binary target) or mean (Student’s t-test - continuous target) are significantly different. Null hypothesis (actual == expected).
- Returns:
tests_table
- Return type:
pandas.DataFrame
Plot functions
- optbinning.scorecard.plot_auc_roc(y: list | NDArray, y_pred: list | NDArray, title: str | None = None, xlabel: str | None = None, ylabel: str | None = None, savefig: bool = False, fname: str | None = None, **kwargs: object) None
Plot Area Under the Receiver Operating Characteristic Curve (AUC ROC).
- Parameters:
y (array-like, shape = (n_samples,)) – Array with the target labels.
y_pred (array-like, shape = (n_samples,)) – Array with predicted probabilities.
title (str or None, optional (default=None)) – Title for the plot.
xlabel (str or None, optional (default=None)) – Label for the x-axis.
ylabel (str or None, optional (default=None)) – Label for the y-axis.
savefig (bool (default=False)) – Whether to save the figure.
fname (str or None, optional (default=None)) – Name for the figure file.
**kwargs (keyword arguments) – Keyword arguments for matplotlib.pyplot.savefig().
- optbinning.scorecard.plot_cap(y: list | NDArray, y_pred: list | NDArray, title: str | None = None, xlabel: str | None = None, ylabel: str | None = None, savefig: bool = False, fname: str | None = None, **kwargs: object) None
Plot Cumulative Accuracy Profile (CAP).
- Parameters:
y (array-like, shape = (n_samples,)) – Array with the target labels.
y_pred (array-like, shape = (n_samples,)) – Array with predicted probabilities.
title (str or None, optional (default=None)) – Title for the plot.
xlabel (str or None, optional (default=None)) – Label for the x-axis.
ylabel (str or None, optional (default=None)) – Label for the y-axis.
savefig (bool (default=False)) – Whether to save the figure.
fname (str or None, optional (default=None)) – Name for the figure file.
**kwargs (keyword arguments) – Keyword arguments for matplotlib.pyplot.savefig().
- optbinning.scorecard.plot_ks(y: list | NDArray, y_pred: list | NDArray, title: str | None = None, xlabel: str | None = None, ylabel: str | None = None, savefig: bool = False, fname: str | None = None, **kwargs: object) None
Plot Kolmogorov-Smirnov (KS).
- Parameters:
y (array-like, shape = (n_samples,)) – Array with the target labels.
y_pred (array-like, shape = (n_samples,)) – Array with predicted probabilities.
title (str or None, optional (default=None)) – Title for the plot.
xlabel (str or None, optional (default=None)) – Label for the x-axis.
ylabel (str or None, optional (default=None)) – Label for the y-axis.
savefig (bool (default=False)) – Whether to save the figure.
fname (str or None, optional (default=None)) – Name for the figure file.
**kwargs (keyword arguments) – Keyword arguments for matplotlib.pyplot.savefig().