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.
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
| Parameter | Type | Default | Description |
|---|---|---|---|
dataframerequired | | — | The input pandas DataFrame containing the training and testing data. |
outcome_variablerequired | | — | The name of the categorical target column to be predicted. |
list_of_predictor_variablesrequired | | — | A list of column names used as features for the classification. |
scale_predictor_variables | | False | If True, scales the features using StandardScaler prior to training. Defaults to False. |
print_peak_to_peak_range_of_each_predictor | | False | If True, prints the statistical range of each predictor column to monitor data dispersion. Defaults to False. |
test_size | | 0.2 | The proportion of the dataset used for testing. Defaults to 0.2. |
show_classification_plot | | True | Whether to display a heatmap of the confusion matrix labeled with the overall accuracy score. Defaults to True. |
lambda_for_regularization | | 0.001 | The regularization parameter. Internally mapped to C = 1 - lambda. Lower values increase regularization strength. Defaults to 0.001. |
max_iterations | | 1000 | The maximum number of iterations allowed for the solver to converge. Defaults to 1000. |
random_seed | | 412 | Controls the randomness of the data split and solver initialization. Defaults to 412. |
print_model_training_performance | | False | If True, prints training error rate and test error rate after fitting. Defaults to False. |
plot_training_and_test_performance | | True | Whether 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_plot | | None | Title text for the comparison chart. Defaults to a sensible built-in title when None. |
subtitle_for_performance_comparison_plot | | None | Subtitle text for the comparison chart. Defaults to a sensible built-in subtitle when None. |
caption_for_performance_comparison_plot | | None | Optional caption text for the comparison chart. Defaults to None. |
title_y_indent_for_performance_comparison_plot | | 1.1 | Vertical position of the title on the comparison chart. Defaults to 1.1. |
subtitle_y_indent_for_performance_comparison_plot | | 1.05 | Vertical position of the subtitle on the comparison chart. Defaults to 1.05. |
caption_y_indent_for_performance_comparison_plot | | -0.15 | Vertical position of the caption on the comparison chart. Defaults to -0.15. |
x_indent_for_performance_comparison_plot | | -0.115 | Horizontal 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
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'],
)