You might be interested in 🤖 Interactive Machine Learning Experiments
For Octave/MatLab version of this repository please check machine-learning-octave project.
This repository contains examples of popular machine learning algorithms implemented in Python with mathematics behind them being explained. Each algorithm has interactive Jupyter Notebook demo that allows you to play with training data, algorithms configurations and immediately see the results, charts and predictions right in your browser. In most cases the explanations are based on this great machine learning course by Andrew Ng.
The purpose of this repository is not to implement machine learning algorithms by using 3rd party library one-liners but rather to practice implementing these algorithms from scratch and get better understanding of the mathematics behind each algorithm. That's why all algorithms implementations are called "homemade" and not intended to be used for production.
In supervised learning we have a set of training data as an input and a set of labels or "correct answers" for each training set as an output. Then we're training our model (machine learning algorithm parameters) to map the input to the output correctly (to do correct prediction). The ultimate purpose is to find such model parameters that will successfully continue correct input→output mapping (predictions) even for new input examples.
In regression problems we do real value predictions. Basically we try to draw a line/plane/n-dimensional plane along the training examples.
Usage examples: stock price forecast, sales analysis, dependency of any number, etc.
Linear Regression is one of the most fundamental and widely known Machine Learning Algorithms which people start with. Building blocks of a Linear Regression Model are:
Discreet/continuous independent variables
A best-fit regression line
Continuous dependent variable. i.e., A Linear Regression model predicts the dependent variable using a regression line based on the independent variables. The equation of the Linear Regression is:
Y=a+b*X + e
Where, a is the intercept, b is the slope of the line, and e is the error term. The equation above is used to predict the value of the target variable based on the given predictor variable(s).
- 📗 Math | Linear Regression - theory and links for further readings
- ⚙️ Code | Linear Regression - implementation example
▶️ Demo | Univariate Linear Regression - predictcountry happiness
score byeconomy GDP
▶️ Demo | Multivariate Linear Regression - predictcountry happiness
score byeconomy GDP
andfreedom index
▶️ Demo | Non-linear Regression - use linear regression with polynomial and sinusoid features to predict non-linear dependencies
In classification problems we split input examples by certain characteristic.
Usage examples: spam-filters, language detection, finding similar documents, handwritten letters recognition, etc.
Logistic regression is one such regression algorithm which can be used for performing classification problems. It calculates the probability that a given value belongs to a specific class. If the probability is more than 50%, it assigns the value in that particular class else if the probability is less than 50%, the value is assigned to the other class. Therefore, we can say that logistic regression acts as a binary classifier.
Working of a Logistic Model
For linear regression, the model is defined by:
𝑦=𝛽0+𝛽1𝑥 - (i)
and for logistic regression, we calculate probability, i.e. y is the probability of a given variable x belonging to a certain class. Thus, it is obvious that the value of y should lie between 0 and 1.
But, when we use equation(i) to calculate probability, we would get values less than 0 as well as greater than 1. That doesn’t make any sense . So, we need to use such an equation which always gives values between 0 and 1, as we desire while calculating the probability.
- 📗 Math | Logistic Regression - theory and links for further readings
- ⚙️ Code | Logistic Regression - implementation example
▶️ Demo | Logistic Regression (Linear Boundary) - predict Iris flowerclass
based onpetal_length
andpetal_width
▶️ Demo | Logistic Regression (Non-Linear Boundary) - predict microchipvalidity
based onparam_1
andparam_2
▶️ Demo | Multivariate Logistic Regression | MNIST - recognize handwritten digits from28x28
pixel images▶️ Demo | Multivariate Logistic Regression | Fashion MNIST - recognize clothes types from28x28
pixel images
Unsupervised learning is a branch of machine learning that learns from test data that has not been labeled, classified or categorized. Instead of responding to feedback, unsupervised learning identifies commonalities in the data and reacts based on the presence or absence of such commonalities in each new piece of data.
In clustering problems we split the training examples by unknown characteristics. The algorithm itself decides what characteristic to use for splitting.
Usage examples: market segmentation, social networks analysis, organize computing clusters, astronomical data analysis, image compression, etc.
K-nearest neighbors (KNN) is a type of supervised learning algorithm which is used for both regression and classification purposes, but mostly it is used for the later. Given a dataset with different classes, KNN tries to predict the correct class of test data by calculating the distance between the test data and all the training points. It then selects the k points which are closest to the test data. Once the points are selected, the algorithm calculates the probability (in case of classification) of the test point belonging to the classes of the k training points and the class with the highest probability is selected. In the case of a regression problem, the predicted value is the mean of the k selected training points.
- 📗 Math | K-means Algorithm - theory and links for further readings
- ⚙️ Code | K-means Algorithm - implementation example
▶️ Demo | K-means Algorithm - split Iris flowers into clusters based onpetal_length
andpetal_width
Anomaly detection (also outlier detection) is the identification of rare items, events or observations which raise suspicions by differing significantly from the majority of the data.
Usage examples: intrusion detection, fraud detection, system health monitoring, removing anomalous data from the dataset etc.
- 📗 Math | Anomaly Detection using Gaussian Distribution - theory and links for further readings
- ⚙️ Code | Anomaly Detection using Gaussian Distribution - implementation example
▶️ Demo | Anomaly Detection - find anomalies in server operational parameters likelatency
andthreshold
The neural network itself isn't an algorithm, but rather a framework for many different machine learning algorithms to work together and process complex data inputs.
Usage examples: as a substitute of all other algorithms in general, image recognition, voice recognition, image processing (applying specific style), language translation, etc.
- 📗 Math | Multilayer Perceptron - theory and links for further readings
- ⚙️ Code | Multilayer Perceptron - implementation example
▶️ Demo | Multilayer Perceptron | MNIST - recognize handwritten digits from28x28
pixel images▶️ Demo | Multilayer Perceptron | Fashion MNIST - recognize the type of clothes from28x28
pixel images
The source of the following machine learning topics map is this wonderful blog post
Make sure that you have Python installed on your machine.
You might want to use venv standard Python library
to create virtual environments and have Python, pip
and all dependent packages to be installed and
served from the local project directory to avoid messing with system wide packages and their
versions.
Install all dependencies that are required for the project by running:
pip install -r requirements.txt
All demos in the project may be run directly in your browser without installing Jupyter locally. But if you want to launch Jupyter Notebook locally you may do it by running the following command from the root folder of the project:
jupyter notebook
After this Jupyter Notebook will be accessible by http://localhost:8888
.
Each algorithm section contains demo links to Jupyter NBViewer. This is fast online previewer for Jupyter notebooks where you may see demo code, charts and data right in your browser without installing anything locally. In case if you want to change the code and experiment with demo notebook you need to launch the notebook in Binder. You may do it by simply clicking the "Execute on Binder" link in top right corner of the NBViewer.
The list of datasets that is being used for Jupyter Notebook demos may be found in data folder.