ANALYSIS TOOL BOX

Predictive Analytics

CreateLogisticRegressionModel

Train, evaluate, and visualize a logistic regression model for binary classification.

logistic-regressionclassificationmachine-learningprobabilitybaseline

Introduction

When the outcome is binary — churn or not, default or not, approve or reject — the useful answer usually isn't just a label but a calibrated probability you can act on. CreateLogisticRegressionModel fits a scikit-learn logistic regression for exactly that: it cleans the data with complete-case analysis, optionally scales features, splits train/test, and generates a confusion-matrix heatmap plus a training-vs-test error-rate comparison, giving you a fast, well-calibrated baseline classifier with a visible read on overfitting.

Teaching Note

Logistic regression is essential for:

  • Predicting the likelihood of customer conversion or subscription renewal
  • Assessing the probability of default in credit risk analysis
  • Identifying the drivers behind binary choices in consumer behavior
  • Classifying medical or technical reports into specific categories (e.g., critical/standard)
  • Forecasting the outcome of competitive bids or project approvals
  • Evaluating the impact of different features on a categorical classification
  • Building efficient baseline classifiers for machine learning pipelines

Parameters

ParameterTypeDefaultDescription
dataframerequiredThe input pandas DataFrame containing the training and testing data.
outcome_variablerequiredThe name of the categorical target column to be predicted.
list_of_predictor_variablesrequiredA list of column names used as features for the classification.
scale_predictor_variablesFalseIf True, scales the features using StandardScaler prior to training. Defaults to False.
print_peak_to_peak_range_of_each_predictorFalseIf True, prints the statistical range of each predictor column to monitor data dispersion. Defaults to False.
test_size0.2The proportion of the dataset used for testing. Defaults to 0.2.
show_classification_plotTrueWhether to display a heatmap of the confusion matrix labeled with the overall accuracy score. Defaults to True.
lambda_for_regularization0.001The regularization parameter. Internally mapped to C = 1 - lambda. Lower values increase regularization strength. Defaults to 0.001.
max_iterations1000The maximum number of iterations allowed for the solver to converge. Defaults to 1000.
random_seed412Controls the randomness of the data split and solver initialization. Defaults to 412.
print_model_training_performanceFalseIf True, prints training error rate and test error rate after fitting. Defaults to False.
plot_training_and_test_performanceTrueWhether to render a bar chart comparing training error rate and test error rate. Defaults to True.
training_bar_color'#3a86ff'Bar color for the training error rate bar. Defaults to '#3a86ff'.
test_bar_color'#b0170c'Bar color for the test error rate bar. Defaults to '#b0170c'.
figure_size_for_performance_comparison_plot(7, 5)Dimensions (width, height) for the comparison chart. Defaults to (7, 5).
title_for_performance_comparison_plotNoneTitle text for the comparison chart. Defaults to a sensible built-in title when None.
subtitle_for_performance_comparison_plotNoneSubtitle text for the comparison chart. Defaults to a sensible built-in subtitle when None.
caption_for_performance_comparison_plotNoneOptional caption text for the comparison chart. Defaults to None.
title_y_indent_for_performance_comparison_plot1.1Vertical position of the title on the comparison chart. Defaults to 1.1.
subtitle_y_indent_for_performance_comparison_plot1.05Vertical position of the subtitle on the comparison chart. Defaults to 1.05.
caption_y_indent_for_performance_comparison_plot-0.15Vertical position of the caption on the comparison chart. Defaults to -0.15.
x_indent_for_performance_comparison_plot-0.115Horizontal starting position for text on the comparison chart. Defaults to -0.115.

Returns

If scale_predictor_variables=False, returns the fitted sklearn.linear_model.LogisticRegression. If True, returns a dictionary containing both the 'model' and the 'scaler' object so new observations can be scaled consistently before prediction.

Example

python
from analysistoolbox.predictive_analytics import CreateLogisticRegressionModel

# Basic model to predict customer churn
model = CreateLogisticRegressionModel(
    df,
    outcome_variable='is_churn',
    list_of_predictor_variables=['tenure', 'monthly_spend'],
)