ElegantML is a NumPy-first machine learning library focused on clear, minimal, and mathematically grounded implementations of classical ML algorithms and neural networks.
ElegantML is designed for:
- Education: Clear, readable implementations that help understand ML algorithms from first principles
- Transparency: Every component is implemented from scratch using NumPy
- Simplicity: Minimal dependencies, maximum clarity
- Mathematical Rigor: Implementations stay true to the underlying mathematics
pip install elegantMLgit clone https://github.com/AshutoshKumar1007/elegantML.git
cd elegantML
pip install -e .pip install -e ".[dev]"Then in Python:
import elegantmlimport numpy as np
from elegantml.nn import Sequential, Linear, Relu, Softmax
from elegantml.nn import CrossEntropyLoss, Adam
# Create a simple neural network
model = Sequential([
Linear(784, 128),
Relu(),
Linear(128, 64),
Relu(),
Linear(64, 10),
Softmax()
])
# Define loss and optimizer
criterion = CrossEntropyLoss()
optimizer = Adam(model.parameters(), lr=0.001)
# Training loop
for epoch in range(num_epochs):
# Forward pass
y_pred = model(X_train)
loss = criterion(y_pred, y_train)
# Backward pass
dloss = criterion.backward()
model.backprop(dloss)
# Update weights
optimizer.step()from elegantml.ml import LinearRegression
import numpy as np
# Create and train model
X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]])
y = np.dot(X, np.array([1, 2])) + 3
model = LinearRegression()
model.fit(X, y)
# Make predictions
predictions = model.predict(X)from elegantml.clustering import KMeans
import matplotlib.pyplot as plt
# Create and fit model
kmeans = KMeans(n_clusters=3, random_state=42)
kmeans.fit(X)
# Get cluster assignments
labels = kmeans.labels_
centroids = kmeans.centroids
# Plot results
plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis')
plt.scatter(centroids[:, 0], centroids[:, 1], c='red', marker='X', s=200)
plt.show()from elegantml.preprocessing import StandardScaler, LabelEncoder
# Standardize features
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Encode labels
encoder = LabelEncoder()
y_encoded = encoder.fit_transform(y)- Layers: Linear, BatchNorm, Dropout, Sequential
- Activations: ReLU, LeakyReLU, Sigmoid, Tanh, Softmax
- Optimizers: BGD, BGD with Momentum, Adam
- Losses: MSE, Binary Cross-Entropy, Cross-Entropy, L2 Regularization
- Features: Model saving/loading, training/eval modes
- Linear Regression
- Logistic Regression
- Softmax Regression (multiclass)
- Support Vector Machines (Classifier, Regressor)
- Gaussian Process Regressor
- Relevance Vector Machine
- Gaussian Discriminant Analysis
- K-Means Clustering
- StandardScaler
- MinMaxScaler
- LabelEncoder
- OneHotEncoder
- Classification: accuracy, precision, recall, F1-score, confusion matrix
- Regression: MSE, MAE, RΒ² score
ElegantML/
βββ elegantml/
β βββ __init__.py
β βββ clustering/
β β βββ __init__.py
β βββ metrics/
β β βββ __init__.py
β βββ ml/
β β βββ __init__.py
β β βββ base.py
β β βββ gaussianProcess.py
β β βββ gda.py
β β βββ linearRegression.py
β β βββ logisticRegression.py
β β βββ rvm.py
β β βββ softmaxRegression.py
β β βββ svm.py
β βββ nn/
β β βββ __init__.py
β β βββ activations.py
β β βββ layers.py
β β βββ losses.py
β β βββ optimizers.py
β βββ preprocessing/
β βββ __init__.py
βββ notebooks/
β βββ dev-history/
β β βββ GDA.ipynb
β β βββ linear_reg.ipynb
β β βββ logistic.ipynb
β β βββ neural.ipynb
β β βββ shocastic.ipynb
β β βββ softmax.ipynb
β β βββ SVm1.ipynb
β β βββ SVM2.ipynb
β β βββ Gaussian Process/
β β βββ Gaussian.ipynb
β β βββ Gaussian2.ipynb
β β βββ GP.ipynb
β βββ tutorials/
βββ README.md
βββ LICENSE
See the notebooks for worked examples and development history:
- notebooks/dev-history/linear_reg.ipynb
- notebooks/dev-history/logistic.ipynb
- notebooks/dev-history/softmax.ipynb
- notebooks/dev-history/GDA.ipynb
- notebooks/dev-history/SVm1.ipynb
- notebooks/dev-history/SVM2.ipynb
- notebooks/dev-history/Gaussian%20Process/Gaussian.ipynb
- notebooks/dev-history/Gaussian%20Process/Gaussian2.ipynb
- notebooks/dev-history/Gaussian%20Process/GP.ipynb
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
# Clone the repository
git clone https://github.com/AshutoshKumar1007/ElegantML.git
cd ElegantML
# Create virtual environment
python -m venv venv
# Windows PowerShell
venv\Scripts\Activate.ps1
# macOS/Linux
source venv/bin/activate
# Install core dependencies if needed
pip install numpy pandas matplotlib- Python >= 3.8
- NumPy >= 1.20.0
- Matplotlib >= 3.3.0
- Pandas >= 1.2.0
- Add more activation functions (GELU, Swish, etc.)
- Implement Convolutional layers
- Add Recurrent layers (RNN, LSTM, GRU)
- Random Forest implementation
- Support Vector Machines (SVM)
- Naive Bayes classifiers
- Principal Component Analysis (PCA)
- More comprehensive documentation
- Tutorial notebooks
- Performance benchmarks
This project is licensed under the MIT License - see the LICENSE file for details.
ElegantML is built from scratch with inspiration from:
- scikit-learn for API design
- PyTorch for neural network architecture
- Various academic papers and textbooks on machine learning
For questions, issues, or suggestions, please open an issue on GitHub.
Made with β€οΈ for ML education and transparency