import numpy as np
import matplotlib.pyplot as plt
# If your package exports these at the top-level:
# from ridge_remake import get_reg_line, ridge_scatter, ridge_scatter_line, ridge_get_r2
# If not, import from the module files:
from ridge_remake.get_reg_line import get_reg_line
from ridge_remake.ridge_scatter import ridge_scatter
from ridge_remake.ridge_scatter_line import ridge_scatter_line
from ridge_remake.ridge_r2 import ridge_get_r2
# Example data
x = np.array([1, 2, 3, 4, 5], dtype=float)
y = np.array([2, 4, 5, 4, 5], dtype=float)
# 1) Fit predictions on the training x
X = x.reshape(-1, 1) # make it 2D for the regression math
y_pred = get_reg_line(X, y)
# 2) Plot scatter
fig, ax = plt.subplots()
ax, _ = ridge_scatter(ax, x, y, scatter_kwargs={"alpha": 0.8}, label="data")
# 3) Overlay fitted line (sort x so the line looks clean)
order = np.argsort(x)
x_line = x[order]
y_line = y_pred[order]
ax, _ = ridge_scatter_line(ax, x_line, y_line, line_kwargs={"linewidth": 2}, label="fit")
# 4) R² score
r2 = ridge_get_r2(y_true=y, y_pred=y_pred)
ax.set_title(f"RidgeMake demo (R² = {r2:.3f})")
ax.legend()
plt.show()RidgeMake
A lightweight regression + plotting toolkit (line fit, scatter, overlay line, and R²).
1 What this package does
RidgeMake helps you do one simple workflow:
- Fit a straight line to paired data (x, y)
- Plot the points
- Overlay the fitted line
- Report an R² score
It’s designed for users with minimal Python experience: you provide arrays, and you get back predictions + a Matplotlib plot.
2 Install
If you are developing locally:
pip install -e .
If you are installing from a released package (example):
pip install ridge_remake
3 Quick start (copy/paste)
4 Function1
get_reg_line(X_train, y_train)
4.1 What it does
Computes linear regression predictions using a least-squares (normal equation) fit, then returns predicted values for the input X_train.
4.2 Docstring (from the package)
Compute linear regression predictions for the provided training data.
Parameters - X_train: array-like or pd.DataFrame of shape (n_samples, n_features) - y_train: array-like or pd.DataFrame of shape (n_samples,) or (n_samples, n_targets)
Returns - y_pred: ndarray of shape (n_samples,) or (n_samples, n_targets)
4.3 Example
import numpy as np
from ridge_remake.get_reg_line import get_reg_line
x = np.array([1, 2, 3, 4, 5], dtype=float)
y = np.array([2, 4, 5, 4, 5], dtype=float)
X = x.reshape(-1, 1)
y_pred = get_reg_line(X, y)
print(y_pred)4.4 Common pitfalls
X_trainmust be 2D (usex.reshape(-1, 1)if you only have one feature).X_trainandy_trainmust have the same number of rows/samples.
5 Function 2
ridge_scatter(ax, x, y, *, scatter_kwargs=None, label=None)
5.1 What it does
Draws a scatter plot on an existing Matplotlib Axes. This function only plots points; it does not fit or draw a line.
5.2 Docstring (from the package)
Create a scatter plot on a provided Matplotlib Axes object. This function is the base plotting function for the ridge regression visualization workflow. It draws the raw data points (x, y) on the provided Axes.
Parameters - ax: matplotlib.axes.Axes - x: array-like (n_samples,) - y: array-like (n_samples,) - scatter_kwargs: dict, optional (passed to ax.scatter(...)) - label: str, optional
Returns - ax: the same axes (with points added) - points: the scatter artist
5.3 Example
import numpy as np
import matplotlib.pyplot as plt
from ridge_remake.ridge_scatter import ridge_scatter
x = np.array([1, 2, 3, 4, 5], dtype=float)
y = np.array([2, 4, 5, 4, 5], dtype=float)
fig, ax = plt.subplots()
ax, points = ridge_scatter(ax, x, y, scatter_kwargs={"alpha": 0.8}, label="data")
ax.legend()
plt.show()5.4 Common pitfalls
- You must pass a real Matplotlib
Axesobject (created byplt.subplots()). xandymust be numeric, non-empty, and the same length.
6 Function 3
ridge_scatter_line(ax, x_line, y_line, *, line_kwargs=None, label=None)
6.1 What it does
Overlays a line on an existing Matplotlib Axes. You provide the line coordinates (x_line, y_line). This function does not compute the regression line—it only draws it.
6.2 Recommended usage pattern
- Compute predictions first (using
get_reg_line) - Sort
xso the line doesn’t zig-zag - Call
ridge_scatter_line(ax, x_sorted, y_pred_sorted, ...)
6.3 Example
import numpy as np
import matplotlib.pyplot as plt
from ridge_remake.get_reg_line import get_reg_line
from ridge_remake.ridge_scatter import ridge_scatter
from ridge_remake.ridge_scatter_line import ridge_scatter_line
x = np.array([3, 1, 5, 2, 4], dtype=float)
y = np.array([5, 2, 5, 4, 4], dtype=float)
X = x.reshape(-1, 1)
y_pred = get_reg_line(X, y)
fig, ax = plt.subplots()
ax, _ = ridge_scatter(ax, x, y)
order = np.argsort(x)
x_line = x[order]
y_line = y_pred[order]
ax, line = ridge_scatter_line(ax, x_line, y_line, line_kwargs={"linewidth": 2}, label="fit")
ax.legend()
plt.show()6.4 Common pitfalls
x_lineandy_linemust be the same length.- Sort
x_linebefore plotting, or your line may look “scrambled”.
7 Function 4
ridge_get_r2(y_true, y_pred)
7.1 What it does
Computes the coefficient of determination (R²). This tells you how well your predicted values explain the variation in the true values.
- R² = 1 means perfect predictions
- R² = 0 means “no better than predicting the mean”
- R² < 0 means worse than predicting the mean
7.2 Example
import numpy as np
from ridge_remake.get_reg_line import get_reg_line
from ridge_remake.ridge_r2 import ridge_get_r2
x = np.array([1, 2, 3, 4, 5], dtype=float)
y = np.array([2, 4, 5, 4, 5], dtype=float)
y_pred = get_reg_line(x.reshape(-1, 1), y)
r2 = ridge_get_r2(y_true=y, y_pred=y_pred)
print(r2)7.3 Common pitfalls
y_trueandy_predmust be the same shape and aligned to the same observations.
8 “How do I explore more?”
- Go to the Reference tab in the navbar to see the auto-generated API docs from docstrings.
- Copy the examples above and change:
- your
xandy - point style (
scatter_kwargs) - line style (
line_kwargs) - number of points / range of values
- your
If you can run the Quick start successfully, you can run everything in this package.