ML-From-Scratch: The Bare-Bones Approach to Machine Learning
eriklindernoren/ML-From-Scratch
I'd be happy to explain what eriklindernoren/ML-From-Scratch is all about and how it can be a valuable resource for a software engineer.
Think of eriklindernoren/ML-From-Scratch as a fantastic learning tool, specifically for understanding the core mechanics of machine learning. Instead of using high-level libraries like scikit-learn, TensorFlow, or PyTorch, this repository implements machine learning algorithms from the ground up using only NumPy.
From a software engineer's perspective, this is a huge advantage. Here's why
Deeper Understanding
You'll move beyond just calling a function like model.fit() and truly understand what's happening under the hood. For example, you'll see how gradient descent actually updates weights in a neural network or how a decision tree is built by recursively splitting data. This foundational knowledge is crucial for debugging, optimizing, and confidently applying ML models.
Problem-Solving Skills
By working with the bare-bones implementations, you'll develop a better intuition for common machine learning problems like overfitting, underfitting, and numerical stability.
Interview Preparation
This is an excellent resource for preparing for machine learning or data science-focused interviews. Many companies ask candidates to explain how an algorithm works or even to write a simplified version of one.
Customization and Specialization
If you ever need to create a custom algorithm or modify an existing one for a very specific task, having this low-level understanding is a must. You're not limited by the features of a library; you can build exactly what you need.
Getting started with this project is straightforward. You'll need to have Python and NumPy installed.
Clone the Repository
First, get a copy of the project on your local machine. Open your terminal or command prompt and run
git clone https://github.com/eriklindernoren/ML-From-Scratch.git
Explore the Code
Navigate into the cloned directory. You'll see different folders, each dedicated to a specific type of algorithm (e.g., supervised_learning, unsupervised_learning, deep_learning).
cd ML-From-Scratch
ls
Install Dependencies
While the project is built on NumPy, some examples might have other dependencies like Matplotlib for plotting. It's a good practice to set up a virtual environment and install them.
pip install -r requirements.txt
Run the Examples
Each algorithm's folder usually contains a Python script that you can run to see the algorithm in action. For example, to see how the K-Nearest Neighbors algorithm works, you can run
python supervised_learning/k_nearest_neighbors.py
Let's take a look at a simplified example to illustrate the "from scratch" approach. While the actual repository code is more robust, this shows the core idea.
Here's a very basic representation of a Linear Regression model. Instead of relying on sklearn.linear_model.LinearRegression, you would implement it like this:
import numpy as np
class LinearRegression:
def __init__(self, learning_rate=0.01, n_iterations=1000):
self.learning_rate = learning_rate
self.n_iterations = n_iterations
self.weights = None
self.bias = None
def fit(self, X, y):
n_samples, n_features = X.shape
self.weights = np.zeros(n_features)
self.bias = 0
# Gradient Descent optimization
for _ in range(self.n_iterations):
y_predicted = np.dot(X, self.weights) + self.bias
# Calculate gradients
dw = (1 / n_samples) * np.dot(X.T, (y_predicted - y))
db = (1 / n_samples) * np.sum(y_predicted - y)
# Update weights and bias
self.weights -= self.learning_rate * dw
self.bias -= self.learning_rate * db
def predict(self, X):
y_predicted = np.dot(X, self.weights) + self.bias
return y_predicted
# --- Example Usage ---
# You would then use this class in your main script
if __name__ == "__main__":
from sklearn.datasets import make_regression
import matplotlib.pyplot as plt
X, y = make_regression(n_samples=100, n_features=1, noise=20, random_state=42)
model = LinearRegression(learning_rate=0.01, n_iterations=1000)
model.fit(X, y)
y_pred = model.predict(X)
plt.scatter(X, y)
plt.plot(X, y_pred, color='red')
plt.show()
As you can see, this code explicitly shows the gradient descent loop, the calculation of the gradients (dw and db), and the updates to the weights. This level of detail is exactly what ML-From-Scratch provides, helping you build a solid and practical understanding of machine learning principles.