A from-scratch ML package for house price prediction
Project description
mylinreg-pkg
A from-scratch machine learning package for house price prediction, implementing OLS, Ridge, Lasso, KNN, and Perceptron — built with only NumPy, Pandas, and Matplotlib.
Installation
pip install mylinreg-pkg
Features
| Module | Class | Algorithm |
|---|---|---|
linear_model |
LinearRegressionOLS |
Ordinary Least Squares |
ridge |
RidgeRegression |
Ridge (L2 regularisation) |
lasso |
LassoRegression |
Lasso (L1 coordinate descent) |
preprocessing |
Preprocessing |
Missing values, outliers, standardisation |
metrics |
Metrics |
MAE, MSE, R² |
feature_selection |
FeatureSelection |
Forward selection, backward elimination |
diagnostics |
Diagnostics |
Multicollinearity, residuals |
visualization |
Visualization |
Actual vs predicted, residual plots |
Quick Start
import numpy as np
import pandas as pd
from mylinreg_pkg import LinearRegressionOLS, RidgeRegression, LassoRegression
from mylinreg_pkg import Metrics, Preprocessing
# Load and preprocess data
df = pd.read_csv("dataset.csv")
df = Preprocessing.missing_values(df)
# Prepare features and target
X = df[["bhk", "area_sqft", "price_per_sqft"]].to_numpy(dtype=float)
y = df["price_lakhs"].to_numpy(dtype=float)
# Train/test split
split = int(len(X) * 0.8)
X_train, X_test = X[:split], X[split:]
y_train, y_test = y[:split], y[split:]
# Normalise
mean, std = X_train.mean(0), X_train.std(0)
std[std == 0] = 1
X_train = (X_train - mean) / std
X_test = (X_test - mean) / std
# OLS
ols = LinearRegressionOLS()
ols.fit(X_train, y_train)
y_pred = ols.predict(X_test)
print("OLS R²:", round(Metrics.r2(y_test, y_pred), 4))
# Ridge
ridge = RidgeRegression(alpha=1.0)
ridge.fit(X_train, y_train)
y_pred = ridge.predict(X_test)
print("Ridge R²:", round(Metrics.r2(y_test, y_pred), 4))
# Lasso
lasso = LassoRegression(alpha=10.0, iterations=1000)
lasso.fit(X_train, y_train)
y_pred = lasso.predict(X_test)
print("Lasso R²:", round(Metrics.r2(y_test, y_pred), 4))
Algorithms
OLS Regression
Closed-form solution: β = (XᵀX)⁻¹ Xᵀy using Moore-Penrose pseudo-inverse for numerical stability.
Ridge Regression
L2-penalised: β = (XᵀX + αI)⁻¹ Xᵀy — shrinks coefficients to reduce variance.
Lasso Regression
L1-penalised via coordinate descent with soft-thresholding — can zero out coefficients for feature selection.
KNN Classifier (standalone knn.py)
Euclidean distance with min-max normalisation. Use odd k to avoid tie votes.
Perceptron (standalone perceptron.py)
Single-layer binary classifier with Heaviside activation and online weight updates.
Results on Pune Housing Dataset (N=150)
| Model | MAE | RMSE | R² |
|---|---|---|---|
| OLS | 7,957 | 11,002 | 0.6844 |
| Ridge (α=1.0) | 7,757 | 10,678 | 0.7027 |
| Lasso (α=10.0) | 7,953 | 10,993 | 0.6849 |
KNN BHK classification accuracy: 73.3% at k=3.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mylinreg_pkg-1.0.0.tar.gz.
File metadata
- Download URL: mylinreg_pkg-1.0.0.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6e8c016ad64e916a814de343c4564f7916a232f1768e4b1f21f3c38bda68d5c5
|
|
| MD5 |
66e324285eeee604fed69f80221a5744
|
|
| BLAKE2b-256 |
709a66d1e2570c1c7941c1341eed835510f8c57c562187764ef42b8b180d5ef1
|
File details
Details for the file mylinreg_pkg-1.0.0-py3-none-any.whl.
File metadata
- Download URL: mylinreg_pkg-1.0.0-py3-none-any.whl
- Upload date:
- Size: 9.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dd5ea7874d9982b561a78a1ce02590cc632f33e023f4e816dd8fed9070eaec0d
|
|
| MD5 |
26241cc66e2d3c5938080bc4ff8e203e
|
|
| BLAKE2b-256 |
2333e30f9b757dec076e0bb7cdc042456837c5256131af9906396429c67d0377
|