Skip to main content

A Python library for regressograms and kernel smoothing, designed for data analysis and visualisation.

Project description

Rgram

Rgram is a Python library for performing regression analysis and visualisation. It provides tools for creating regressograms (rgrams) and performing kernel smoothing using Polars, a high-performance DataFrame library. The library is designed to simplify data analysis workflows and is compatible with uv for dependency management.

Notes on regressograms can be found in section 4.4 of García-Portugués, E. (2023). Notes for nonparametric statistics. Carlos III University of Madrid: Madrid, Spain.

Features

  • Regressogram (rgram): Analyse relationships between variables with support for binning by index or distribution and optional Ordinary Least Squares (OLS) regression calculations.
  • Kernel Smoothing (kernel_smoothing): Perform kernel smoothing using the Epanechnikov kernel for regression analysis.
  • Flexible API: Designed for ease of use and high performance thanks to Polars DataFrames and LazyFrames.

Requirements

  • Python >= 3.11
  • uv for dependency management

Installation

To get started with Rgram, follow these steps:

  1. Clone the repository:

    git clone https://github.com/JackGreenaway/Rgram.git
    cd Rgram
    
  2. Install dependencies using uv:

    uv install
    
  3. Verify the installation:

    uv sync
    

Usage


Example: Regressograms and Kernel Smoothing with Polars

Imports and setup

import polars as pl
import numpy as np
import matplotlib.pyplot as plt

from rgram.rgram import Regressogram, KernelSmoother

plt.style.use("ggplot")

Generate sample data

n = 50
x = np.sort(np.random.normal(0, 1, n))
y = 1 + x
y_noise = y + np.random.normal(0, 2, n)
base function

Fit regressogram to noisy data

df = pl.DataFrame({"x": x, "y": y, "y_noise": y_noise})

rgramer = Regressogram(data=df, x="x", y="y_noise")
rgram = rgramer.fit_transform().collect()

fig, ax = plt.subplots(figsize=(6, 5))

ax.plot(x, y, lw=0.5, label="true function")
ax.scatter(x, y_noise, s=15, alpha=0.3, marker="o", color="black")
ax.step(rgram["x_val"], rgram["y_pred_rgram"], lw=0.5, label="rgram")
ax.fill_between(
    rgram["x_val"],
    rgram["y_pred_rgram_uci"],
    rgram["y_pred_rgram_lci"],
    alpha=0.2,
    label="ci",
)

ax.set_xlabel("x variable"), ax.set_ylabel("y variable")
ax.legend()
fig.tight_layout()
plt.show()
rgram

Kernel smoothing on regressogram output

smoother = KernelSmoother(data=rgram, x="x_val", y="y_pred_rgram")
ks_rgram = smoother.fit_transform().collect()

fig, ax = plt.subplots(figsize=(6, 5))

ax.plot(x, y, lw=0.5, label="true function")
ax.scatter(x, y_noise, s=15, alpha=0.3, marker="o", color="black")
ax.plot(ks_rgram["x_eval"], ks_rgram["y_kernel"], lw=0.5, label="smoothed rgram")

cis = []
for col in ["y_pred_rgram_lci", "y_pred_rgram_uci"]:
    smoother = KernelSmoother(data=rgram, x="x_val", y=col)
    cis += [smoother.fit_transform().collect()]

ax.fill_between(
    cis[0]["x_eval"],
    cis[0]["y_kernel"],
    cis[1]["y_kernel"],
    alpha=0.2,
    label="smoothed ci",
)

ax.set_xlabel("x variable"), ax.set_ylabel("y variable")
ax.legend()
fig.tight_layout()
plt.show()
smoothed rgram

This example demonstrates how to use the Regressogram and KernelSmoother classes to create a regressogram and apply kernel smoothing for visualisation.
Both classes follow a scikit-learn-like API with fit(), transform(), and fit_transform() methods.
For most use cases, fit_transform() is the recommended entry point.


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rgram-0.1.1.tar.gz (502.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rgram-0.1.1-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file rgram-0.1.1.tar.gz.

File metadata

  • Download URL: rgram-0.1.1.tar.gz
  • Upload date:
  • Size: 502.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.4

File hashes

Hashes for rgram-0.1.1.tar.gz
Algorithm Hash digest
SHA256 50255150410f7b95fd2e0c3876d53ef9ff0fafd34cd279b33d98bf7018f58748
MD5 dadeadda9cb9a977cbfcffd7f181abdf
BLAKE2b-256 50124961b74e19912197e01a353a3947338ae2e971796d7ba52daf31bc741722

See more details on using hashes here.

File details

Details for the file rgram-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: rgram-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.4

File hashes

Hashes for rgram-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bd3367d92499c8267bf95071c7884952bf9688c0a37c3908611f4361fb8683d4
MD5 e9556d8a5266e9d33bcb0c4aaa556958
BLAKE2b-256 f5152111f60d3ec7fde5182c148933c71584df98318e43eeeb836964b3027d3d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page