Skip to main content

Python Machine Learning (ML) Plot

Build Release CodeQL Codecov Release Version PyPI Python License

Modules containing reusable functions for machine learning visualization plotting

Compatibility

  • Python 3.12 or later

Setup

Add Dependency

python3 -m pip install opengood.py-ml-plot

Note: See Release version badge above for the latest version.


Features

Classification Model Plotting

Set Up and Display 2-D Classification Plot

Set up a 2-D classification model plot then display its result visualization.

Notes:

  • The example below uses a dataset to train a logistic regression model then display the plot for the training set
  • For feature scaling, if required, implement the feature scaling logic in the feature_scaling lambda
  • For predictions, implement the prediction logic in the predict lambda
import os

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from matplotlib.colors import ListedColormap
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

from src.opengood.py_ml_plot import setup_classification_plot

resource_path = os.path.join(os.path.dirname(__file__), "../resources", "data.csv")
dataset = pd.read_csv(resource_path)

x = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values

x_train, _, y_train, _ = train_test_split(x, y, test_size=0.2, random_state=0)

sc = StandardScaler()
x_train = sc.fit_transform(x_train)

classifier = LogisticRegression(random_state=0)
classifier.fit(x_train, y_train)

setup_classification_plot(
    x=x_train,
    y=y_train,
    cmap=ListedColormap(("salmon", "dodgerblue")),
    title="Logistic Regression",
    x_label="Age",
    y_label="Estimated Salary",
    meshgrid={
        0: {"min": 10, "max": 10, "step": 0.25},
        1: {"min": 1000, "max": 1000, "step": 0.25},
    },
    feature_scale=lambda x_set, y_set: (sc.inverse_transform(x_set), y_set),
    predict=lambda x1, x2: classifier.predict(
        sc.transform(np.array([x1.ravel(), x2.ravel()]).T)
    ).reshape(x1.shape),
)

plt.show()
Output

Setup Classification Plot Visualization


Visualization Logic
Feature Scale Lambda Logic

feature_scale lambda implementation logic for function setup_classification_plot is as follows:

  • Inverse feature scaling is invoked via a feature scaling object, such as the StandardScaler object sc created earlier for feature scaling
  • x_set and y_set are assigned non-feature scaled values of the matrix of features and the dependent variable
    • x_set values are inverted from their feature-scaled values in x
    • y_set values are not inverted and taken directly from y
Predict Lambda Logic

predict lambda implementation logic for function setup_classification_plot is as follows:

  • Classifier object classifier method predict is invoked
  • Since the values of the reshaped 2D array are not feature scaled, the values are feature scaled via the transform method on the sc object
    • This method call is not required for models that do not require feature scaling
  • ravel function from the NumPy library is used to flatten a multidimensional array into a one-dimensional array
    • x1 and x2 are flatten into a 1D array via the ravel function
    • They are then combined via the array function from the NumPy library into a 2D array
    • The result is then reshaped via the reshape function to match the shape of x1
Setup Plot Visualization Logic

Visualization implementation logic for function setup_classification_plot is as follows:

  • If the meshgrid dict is not defined, a default set of dict attributes are set providing min, max, and step values for each axis:
    • 0 and 1 are used for the keys
    • 10 and 1000 are used for min and max values
    • 0.25 is used for step value
  • If the feature_scale lambda is not defined, x_set and y_set are assigned the values of x and y, respectively
  • If the feature_scale lambda is defined, x_set and y_set are assigned non-feature scaled values of the matrix of features and the dependent variable from the sets using a feature scaling object, such as the StandardScaler object created earlier for feature scaling
    • x_set values are inverted from their feature-scaled values in x
    • y_set values are not inverted and taken directly from y
  • meshgrid function from the NumPy library returns a tuple of coordinate matrices from coordinate vectors
    • The ranges for each axis are controlled by the meshgrid dict parameter
      • Two sets of matrices (x1 and x2) are returned with coordinate vectors
      • x1
        • arange function is called with a defined start and stop interval
          • x_set[:, 0] returns all the rows for feature x1
          • start parameter
            • Start of an interval
            • x_set[:, 0].min() returns the minimum value for feature x1
            • Value of 10 is subtracted for padding
          • stop parameter
            • End of an interval
            • x_set[:, 0].max() returns the maximum value for feature x1
            • Value of 10 is added for padding
          • step parameter
            • Spacing between values
            • Value of 0.25 is added for spacing
      • x2
        • arange function is called with a defined start and stop interval
          • x_set[:, 1] returns all the rows for feature x2
          • start parameter
            • Start of an interval
            • x_set[:, 1].min() returns the minimum value for feature x2
            • Value of 1000 is subtracted for padding
            • Value of 1000 is used instead of 10 due to the difference in scaling for feature x2 vs. feature x1
          • stop parameter
            • End of interval
            • x_set[:, 1].max() returns the maximum value for feature x2
            • Value of 1000 is added for padding
          • step parameter
            • Spacing between values
            • Value of 0.25 is added for spacing
  • The prediction logic implemented in the predict lambda is executed, and the result is assigned to y_pred, containing the predictions
  • contourf function from the Matplotlib library is used for creating filled contour plots
    • It visualizes 3D data in 2D by drawing filled contours representing constant z-values (heights) on an x-y plane
    • These plots are useful for displaying data like temperature distributions, terrain elevations, or any scalar field where the magnitude varies over 2 dimensions
    • The most basic use case of contourf involves providing a 2D array representing the z-values
    • Matplotlib automatically determines the x and y coordinates based on the array's indices
    • X and Y parameters
      • The coordinates of the values in Z
      • X and Y must both be 2D arrays with the same shape as Z
      • x1 is used for X containing x1 values
      • x2 is used for Y containing x2 values
    • Z parameter
      • The height values over which the contour is drawn
      • ravel function from the NumPy library is used to flatten a multidimensional array into a one-dimensional array
        • x1 and x2 are flatten into a 1D array via the ravel function
        • They are then combined via the array function from the NumPy library into a 2D array
        • The result is then reshaped via the reshape function to match the shape of x1
      • Since the values of the reshaped 2D array are not feature scaled, the values are feature scaled via the transform method on the sc object
    • alpha parameter
      • The alpha blending value, between 0 (transparent) and 1 (opaque)
      • Value of 0.75 is used to make the blending mostly opaque
    • cmap parameter
      • The Colormap object instance or registered colormap name used to map scalar data to colors
      • salmon and dodgerblue are used for a ListedColormap object
        • salmon = 0 or negative classifier
        • dodgerblue = 1 or positive classifier
  • xlim function from the Matplotlib library is used to get or set the x-axis limits of the current axes
    • min() and max() for x1 are used for the limits
  • ylim function from the Matplotlib library is used to get or set the y-axis limits of the current axes
    • min() and max() for x2 are used for the limits
  • The values from y_set are iterated over in a for-in loop
    • unique function from the NumPy library returns sorted, unique elements of an array
      • Values of y_set are made unique and sorted
    • Iterator variable i represents the current row of iteration
    • Iterator variable j represents the classification value for the dependent variable
      • 0 negative classifier
      • 1 positive classifier
  • scatter method from the Matplotlib library creates a scatter plot of data points with the shaded contour showing the classification for the dependent variable
    • x-axis uses values from x_set where y_set value = 0 (negative classifier)
    • y-axis uses values from x_set where y_set value = 1 (positive classifier)
    • c parameter
      • The marker colors
      • Uses the ListedColormap with the classification colors for the current row at index i
    • label parameter
      • Sets the label
      • Values
        • 0 negative classifier
        • 1 positive classifier

Development

Python Virtual Environment

Create and activate Python virtual environment:

python3 -m venv .venv
source .venv/bin/activate

Install Dependencies

Install the package in editable mode with all dependencies:

python3 -m pip install -e .

Install with development dependencies (includes pip-tools, pytest, pytest-cov):

python3 -m pip install -e ".[dev]"

Generate Requirements File

Generate a pinned requirements.txt file from pyproject.toml:

pip-compile pyproject.toml -o requirements.txt

This creates a requirements.txt with exact versions of all dependencies and their transitive dependencies.

Run Tests

Run all tests with coverage:

python -m pytest tests/

Run tests with verbose output and coverage report:

python -m pytest tests/ -v --cov=src.opengood.py_ml_plot --cov-report=term-missing

Run a specific test:

python -m pytest tests/py_ml_plot/test_classification_plot.py::TestClassificationPlot::test_logistic_regression_setup_classification_plot_with_shaded_regions

Metadata

Release files for opengood.py-ml-plot 2.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for opengood.py-ml-plot 2.6.0
File Size Uploaded Provenance
opengood_py_ml_plot-2.6.0.tar.gz 11.7 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for opengood.py-ml-plot 2.6.0
File Interpreter ABI Platform
opengood_py_ml_plot-2.6.0-py3-none-any.whl Python 3 none any Details

Total release size: 20.2 kB

Release files / opengood_py_ml_plot-2.6.0.tar.gz

Download URL opengood_py_ml_plot-2.6.0.tar.gz
Size 11.7 kB
Tags Source
SHA-256 checksum
How to use checksums
f49fabb83dc8bd753f536cb770b562bad7065ffd3e09cab94e9ed584df96b194
BLAKE2b-256 checksum
How to use checksums
5b8930d8aed358fc900a7205bdb552b9a6463b3de7b1f1fd8076075863187f5a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 25, 2026.

Transparency log

Release files / opengood_py_ml_plot-2.6.0-py3-none-any.whl

Download URL opengood_py_ml_plot-2.6.0-py3-none-any.whl
Size 8.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
73f6a8f60952f3a86176f7bbfb08b9591b7a4102e15ae34498856307053597b1
BLAKE2b-256 checksum
How to use checksums
5b6664b0ea112b64214a1828b0f56527b561522500521bc0d303e66b301ffe6d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 25, 2026.

Transparency log

Security for opengood.py-ml-plot 2.6.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for opengood_py_ml_plot-2.6.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 25, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page