Skip to main content

A lightweight machine learning library with educational implementations built from scratch using NumPy.

Project description

DolphinBoost 🐬

A lightweight Machine Learning library built from scratch using NumPy.


Installation

pip install dolphinboost

Features

  • ✅ Simple Linear Regression
  • ✅ Multiple Linear Regression (5 solvers!)
  • ✅ Pure NumPy — no dependencies except NumPy
  • ✅ sklearn-compatible API

Quick Start

import numpy as np
from dolphinboost import SimpleLinearRegression
from dolphinboost import MultipleLinearRegression

# Simple Linear Regression
X = np.array([1, 2, 3, 4, 5]).reshape(-1, 1)
y = np.array([2, 4, 6, 8, 10], dtype=float)

model = SimpleLinearRegression()
model.fit(X, y)
print(model.predict(np.array([[6]])))  # [12.]

# Multiple Linear Regression
X = np.random.randn(100, 3)
y = X @ np.array([1.0, 2.0, 3.0])

model = MultipleLinearRegression()
model.fit(X, y)
print(model.coef_.round(2))  # [1. 2. 3.]

Simple Linear Regression

Usage

from dolphinboost import SimpleLinearRegression
import numpy as np

X = np.array([1, 2, 3, 4, 5]).reshape(-1, 1)
y = np.array([2, 4, 6, 8, 10], dtype=float)

model = SimpleLinearRegression()
model.fit(X, y)

print(model.coef_)        # 2.0
print(model.intercept_)   # 0.0
print(model.beta_)        # [0. 2.]

X_new = np.array([[6], [7]])
print(model.predict(X_new))  # [12. 14.]

Parameters

Parameter Type Default Description
fit_intercept bool True Whether to compute intercept

Attributes

Attribute Type Description
coef_ float Slope β₁
intercept_ float Intercept β₀
beta_ ndarray [β₀, β₁]
X_mean_ float Mean of training X
y_mean_ float Mean of training y

Methods

Method Description
fit(X, y) Fit model to training data
predict(X) Predict target values

Input Requirements

# X must be 2D with exactly 1 feature
X = np.array([[1], [2], [3]])       # ✅ correct
X = np.array([1, 2, 3])             # ❌ use X.reshape(-1, 1)
X = np.array([[1, 2], [3, 4]])      # ❌ use MultipleLinearRegression

# y must be 1D
y = np.array([1, 2, 3])             # ✅ correct
y = np.array([[1], [2], [3]])       # ❌ must be 1D

fit_intercept=False

model = SimpleLinearRegression(fit_intercept=False)
model.fit(X, y)
print(model.intercept_)  # 0

Errors

# 1. Wrong X dimensions
model.fit(np.array([1, 2, 3]), y)
# ValueError: X must be 2-dimensional, got 1D.
#             Shape: (3,). Use X.reshape(-1, 1) if needed.

# 2. Multiple features
model.fit(np.array([[1, 2], [3, 4]]), y)
# ValueError: SimpleLinearRegression only supports 1 feature,
#             but got 2 features.
#             Use MultipleLinearRegression for multiple features.

# 3. Empty X
model.fit(np.array([]).reshape(0, 1), y)
# ValueError: X has 0 samples

# 4. Predict before fit
m = SimpleLinearRegression()
m.predict(X)
# ValueError: Model not fitted yet! Call fit() first.

# 5. All X values identical
model.fit(np.array([[1], [1], [1]]), np.array([1, 2, 3]))
# ValueError: Cannot fit when all X values are identical

# 6. NaN or infinite values
model.fit(X_with_nan, y)
# ValueError: X contains NaN or infinite values

Multiple Linear Regression

Usage

from dolphinboost import MultipleLinearRegression
import numpy as np

np.random.seed(42)
X = np.random.randn(100, 3)
y = X @ np.array([1.0, 2.0, 3.0]) + np.random.randn(100) * 0.1

model = MultipleLinearRegression(solver='lstsq')
model.fit(X, y)

print(model.coef_.round(2))   # [1. 2. 3.]
print(model.intercept_)        # ~0.0
print(model.n_features_in_)    # 3

X_new = np.random.randn(5, 3)
print(model.predict(X_new))

Parameters

Parameter Type Default Description
fit_intercept bool True Whether to compute intercept
solver str 'lstsq' Solver to use

Attributes

Attribute Type Description
coef_ ndarray Feature coefficients [β₁, β₂, ...]
intercept_ float Intercept β₀
beta_ ndarray Full array [β₀, β₁, β₂, ...]
n_features_in_ int Number of features seen during fit

Methods

Method Description
fit(X, y) Fit model to training data
predict(X) Predict target values

Solvers

Solver Speed Stability Best For
normal ⚡⚡⚡ Fastest ❌ Least stable Small, clean data
cholesky ⚡⚡ Fast ⚠️ Moderate Medium, clean data
qr ⚡ Medium ✅ Good General purpose
svd 🐢 Slower ✅✅ Very stable Multicollinear data
lstsq 🐢 Slower ✅✅ Best Default — always safe!
# Choose based on your data
model = MultipleLinearRegression(solver='normal')    # fastest
model = MultipleLinearRegression(solver='cholesky')  # fast + stable
model = MultipleLinearRegression(solver='qr')        # general
model = MultipleLinearRegression(solver='svd')       # multicollinear
model = MultipleLinearRegression(solver='lstsq')     # default ✅

fit_intercept=False

model = MultipleLinearRegression(fit_intercept=False)
model.fit(X, y)
print(model.intercept_)  # 0

When solvers fail

# Perfect multicollinearity — col2 = 2 × col1
X = np.array([[1, 2], [2, 4], [3, 6], [4, 8]])
y = np.array([1, 2, 3, 4])

MultipleLinearRegression(solver='normal').fit(X, y)
# ValueError: Normal equation failed because XᵀX is
#             singular or ill-conditioned.
#             Try solver='lstsq' or solver='svd'.

MultipleLinearRegression(solver='cholesky').fit(X, y)
# ValueError: Cholesky decomposition failed because XᵀX
#             is not positive definite.
#             Try solver='lstsq', 'svd', or 'qr'.

MultipleLinearRegression(solver='qr').fit(X, y)
# ValueError: QR solver failed because the matrix
#             is rank deficient.
#             Try solver='lstsq' or solver='svd'.

MultipleLinearRegression(solver='svd').fit(X, y)    # ✅ works!
MultipleLinearRegression(solver='lstsq').fit(X, y)  # ✅ works!

Errors

# 1. Wrong X dimensions
model.fit(np.array([1, 2, 3]), y)
# ValueError: X must be 2-dimensional, got 1D.

# 2. Empty X
model.fit(np.array([]).reshape(0, 3), y)
# ValueError: X has 0 samples.

# 3. Length mismatch
model.fit(X_100, y_50)
# ValueError: X and y must have same length

# 4. NaN or infinite values
model.fit(X_with_nan, y)
# ValueError: X contains Nan or Infinite values

# 5. Unknown solver
MultipleLinearRegression(solver='unknown').fit(X, y)
# ValueError: Unknown solver 'unknown'.
#             Choose from ['normal', 'cholesky', 'qr', 'svd', 'lstsq']

# 6. Predict before fit
m = MultipleLinearRegression()
m.predict(X)
# ValueError: Model not fitted yet! Call fit() first.

# 7. Feature mismatch in predict
model.fit(X_3features, y)
model.predict(X_4features)
# ValueError: Expected 3 features, got 4.

Comparison with sklearn

import numpy as np
from sklearn.linear_model import LinearRegression
from dolphinboost import MultipleLinearRegression

np.random.seed(42)
X = np.random.randn(1000, 5)
y = X @ np.array([1.0, 2.0, 3.0, 4.0, 5.0]) + np.random.randn(1000) * 0.1

# DolphinBoost
db = MultipleLinearRegression()
db.fit(X, y)
print("DolphinBoost:", db.coef_.round(2))
# [1. 2. 3. 4. 5.]  ✅

# Sklearn
sk = LinearRegression()
sk.fit(X, y)
print("Sklearn:     ", sk.coef_.round(2))
# [1. 2. 3. 4. 5.]  ✅

Mathematical Background

Simple Linear Regression

Model:

$$y = \beta_0 + \beta_1 x$$

where

$$\beta_1 = \frac{\sum (x_i - \bar{x})(y_i - \bar{y})}{\sum (x_i - \bar{x})^2}$$

$$\beta_0 = \bar{y} - \beta_1 \bar{x}$$

Multiple Linear Regression

Model:

$$y = X\beta$$

where $X$ is the design matrix, $y$ is the target vector, $\beta$ is the coefficient vector.

Normal Equation

$$\beta = (X^TX)^{-1}X^Ty$$

Cholesky Decomposition

$$X^TX = LL^T$$

Solve $Lz = X^Ty$, then $L^T\beta = z$

QR Decomposition

$$X = QR$$

Solve $R\beta = Q^Ty$

Singular Value Decomposition (SVD)

$$X = U\Sigma V^T$$

$$\beta = V\Sigma^{-1}U^Ty$$

Least Squares

Find $\beta$ that minimizes:

$$|X\beta - y|^2$$


Requirements

python >= 3.8
numpy >= 1.20.0

Links


License

MIT License


Author

Peddakotla Karthikeya Built with ❤️ from scratch using NumPy!


Contributing

Found a bug or have an idea? Open an issue on PyPI or contact via PyPI page! 🐬

Contributions are welcome!

If you'd like to improve DolphinBoost, you can contribute by:

  • 🐛 Fixing bugs
  • ✨ Adding new machine learning algorithms
  • ⚡ Improving performance
  • 📚 Improving documentation
  • ✅ Writing tests
  • 💡 Suggesting new features

Please open an issue or submit a pull request describing your changes.

Becoming a Contributor

Anyone who makes meaningful contributions to DolphinBoost will be recognized in the project's contributors list.

Becoming a Co-author

Developers who consistently make significant contributions—such as implementing major features, improving the architecture, or helping maintain the project over time—may be invited to become co-authors of the project.

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

dolphinboost-0.2.1.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

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

dolphinboost-0.2.1-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file dolphinboost-0.2.1.tar.gz.

File metadata

  • Download URL: dolphinboost-0.2.1.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for dolphinboost-0.2.1.tar.gz
Algorithm Hash digest
SHA256 b302cef1bd37fe226560a3b8f4e2011099b9ed5fcbdfa4d217853c53d143d253
MD5 f9fe0d9ef9d4d7e91f0b3402b17e46fc
BLAKE2b-256 e3a8b039a8852647530bc3223fa45ca95b9981ae9da4371741684890d06b13d0

See more details on using hashes here.

File details

Details for the file dolphinboost-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: dolphinboost-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for dolphinboost-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fabe16f2a215b19f20a64eb791f7ff9b8b8e44e9817059f812ddf2c892e69dc9
MD5 621d9e469d5a790c25cf5022f4ebeb8a
BLAKE2b-256 380ed88c7b698dd62d8c17ad58786c2993dc568b01ee0ef8715e7d3d4050781f

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