Cheat sheets for machine learning are plentiful. Quality, concise technical cheat sheets, on the other hand... not so much. A good set of resources covering theoretical machine learning concepts would be invaluable.
- Machine Learning Cheat Sheet Pdf
- Pyspark Ml Cheat Sheet
- Machine Learning Algorithms Cheat Sheet
- Cheat Of Ml
Shervine Amidi, graduate student at Stanford, and Afshine Amidi, of MIT and Uber, have created just such a set of resources. The VIP cheat sheets, as Shervine and Afshine have dubbed them (Github repo with PDFs available here), are structured around covering key top-level topics in Stanford's CS 229 Machine Learning course, including:
This post compiles a list of all available Machine Learning Cheat Sheets on the Internet in one place. Machine Learning Cheat Sheet – Classical equations, diagrams and tricks in machine learning. Super VIP Cheat Sheet: Machine Learning. ML Cheat Sheet Documentation.
- Definitions of common machine learning terms. Accuracy Percentage of correct predictions made by the model. Algorithm A method, function, or series of instructions used to generate a machine learning model.Examples include linear regression, decision.
- Machine Learning tips and tricks cheatsheet Star. By Afshine Amidi and Shervine Amidi. Classification metrics. In a context of a binary classification, here are the.
- Notation and general concepts
- Linear models
- Classification
- Clustering
- Neural networks
- ... and much more
Links to individual cheat sheets are below:
You can visit Shervine's CS 229 resource page or the Github repo for more information, or can download the cheat sheets from the direct download links above.
You can also find all of the sheets bundled together into a single 'super VIP cheat sheet.'
Thanks to Shervine and Afshine for putting these fantastic resources together.
Related:
- Introduction
- Binary logistic regression
- Multiclass logistic regression
Logistic regression is a classification algorithm used to assign observations to a discrete set of classes. Unlike linear regression which outputs continuous number values, logistic regression transforms its output using the logistic sigmoid function to return a probability value which can then be mapped to two or more discrete classes.
Given data on time spent studying and exam scores. Linear Regression and logistic regression can predict different things:
- Linear Regression could help us predict the student’s test score on a scale of 0 - 100. Linear regression predictions are continuous (numbers in a range).
- Logistic Regression could help use predict whether the student passed or failed. Logistic regression predictions are discrete (only specific values or categories are allowed). We can also view probability scores underlying the model’s classifications.
- Binary (Pass/Fail)
- Multi (Cats, Dogs, Sheep)
- Ordinal (Low, Medium, High)
Say we’re given data on student exam results and our goal is to predict whether a student will pass or fail based on number of hours slept and hours spent studying. We have two features (hours slept, hours studied) and two classes: passed (1) and failed (0).
Studied | Slept | Passed |
4.85 | 9.63 | 1 |
8.62 | 3.23 | 0 |
5.43 | 8.23 | 1 |
9.21 | 6.34 | 0 |
Graphically we could represent our data with a scatter plot.
In order to map predicted values to probabilities, we use the sigmoid function. The function maps any real value into another value between 0 and 1. In machine learning, we use sigmoid to map predictions to probabilities.
Math
Note
- (s(z)) = output between 0 and 1 (probability estimate)
- (z) = input to the function (your algorithm’s prediction e.g. mx + b)
- (e) = base of natural log
Graph
Code
Our current prediction function returns a probability score between 0 and 1. In order to map this to a discrete class (true/false, cat/dog), we select a threshold value or tipping point above which we will classify values into class 1 and below which we classify values into class 2.
For example, if our threshold was .5 and our prediction function returned .7, we would classify this observation as positive. If our prediction was .2 we would classify the observation as negative. For logistic regression with multiple classes we could select the class with the highest predicted probability.
Using our knowledge of sigmoid functions and decision boundaries, we can now write a prediction function. A prediction function in logistic regression returns the probability of our observation being positive, True, or “Yes”. We call this class 1 and its notation is (P(class=1)). As the probability gets closer to 1, our model is more confident that the observation is in class 1.
Math
Let’s use the same multiple linear regression equation from our linear regression tutorial.
This time however we will transform the output using the sigmoid function to return a probability value between 0 and 1.
If the model returns .4 it believes there is only a 40% chance of passing. If our decision boundary was .5, we would categorize this observation as “Fail.””
Code
We wrap the sigmoid function over the same prediction function we used in multiple linear regression
Unfortunately we can’t (or at least shouldn’t) use the same cost function MSE (L2) as we did for linear regression. Why? There is a great math explanation in chapter 3 of Michael Neilson’s deep learning book [5], but for now I’ll simply say it’s because our prediction function is non-linear (due to sigmoid transform). Squaring this prediction as we do in MSE results in a non-convex function with many local minimums. If our cost function has many local minimums, gradient descent may not find the optimal global minimum.
Math
Instead of Mean Squared Error, we use a cost function called Cross-Entropy, also known as Log Loss. Cross-entropy loss can be divided into two separate cost functions: one for (y=1) and one for (y=0).
The benefits of taking the logarithm reveal themselves when you look at the cost function graphs for y=1 and y=0. These smooth monotonic functions [7] (always increasing or always decreasing) make it easy to calculate the gradient and minimize cost. Image from Andrew Ng’s slides on logistic regression [1].
The key thing to note is the cost function penalizes confident and wrong predictions more than it rewards confident and right predictions! The corollary is increasing prediction accuracy (closer to 0 or 1) has diminishing returns on reducing cost due to the logistic nature of our cost function.
Above functions compressed into one
Multiplying by (y) and ((1-y)) in the above equation is a sneaky trick that let’s us use the same equation to solve for both y=1 and y=0 cases. If y=0, the first side cancels out. If y=1, the second side cancels out. In both cases we only perform the operation we need to perform.
Vectorized cost function
Code
To minimize our cost, we use Gradient Descent just like before in Linear Regression. There are other more sophisticated optimization algorithms out there such as conjugate gradient like BFGS, but you don’t have to worry about these. Machine learning libraries like Scikit-learn hide their implementations so you can focus on more interesting things!
Math
One of the neat properties of the sigmoid function is its derivative is easy to calculate. If you’re curious, there is a good walk-through derivation on stack overflow [6]. Michael Neilson also covers the topic in chapter 3 of his book.
Which leads to an equally beautiful and convenient cost function derivative:
Note
- (C') is the derivative of cost with respect to weights
- (y) is the actual class label (0 or 1)
- (s(z)) is your model’s prediction
- (x) is your feature or feature vector.
Notice how this gradient is the same as the MSE (L2) gradient, the only difference is the hypothesis function.
Pseudocode
Code
Machine Learning Cheat Sheet Pdf
The final step is assign class labels (0 or 1) to our predicted probabilities.
Decision boundary
Convert probabilities to classes
Example output
Our training code is the same as we used for linear regression.
If our model is working, we should see our cost decrease after every iteration.
Final cost: 0.2487. Final weights: [-8.197, .921, .738]
Cost history
Accuracy
Accuracy measures how correct our predictions were. In this case we simply compare predicted labels to true labels and divide by the total.
Decision boundary
Another helpful technique is to plot the decision boundary on top of our predictions to see how our labels compare to the actual labels. This involves plotting our predicted probabilities and coloring them with their true labels.
Code to plot the decision boundary
Instead of (y = {0,1}) we will expand our definition so that (y = {0,1...n}). Basically we re-run binary classification multiple times, once for each class.
- Divide the problem into n+1 binary classification problems (+1 because the index starts at 0?).
- For each class…
- Predict the probability the observations are in that single class.
- prediction = <math>max(probability of the classes)
For each sub-problem, we select one class (YES) and lump all the others into a second class (NO). Then we take the class with the highest predicted value.
The softmax function (softargmax or normalized exponential function) is a function that takes as input a vector of K real numbers, and normalizes it into a probability distribution consisting of K probabilities proportional to the exponentials of the input numbers. That is, prior to applying softmax, some vector components could be negative, or greater than one; and might not sum to 1; but after applying softmax, each component will be in the interval [ 0 , 1 ] , and the components will add up to 1, so that they can be interpreted as probabilities.The standard (unit) softmax function is defined by the formula
In words: we apply the standard exponential function to each element (z_i) of the input vector (z) and normalize these values by dividing by the sum of all these exponentials; this normalization ensures that the sum of the components of the output vector (σ(z)) is 1. [9]
Pyspark Ml Cheat Sheet
Let’s compare our performance to the LogisticRegression
model provided by scikit-learn [8].
Scikit score: 0.88. Our score: 0.89
References
[1] | http://www.holehouse.org/mlclass/06_Logistic_Regression.html |
[2] | http://machinelearningmastery.com/logistic-regression-tutorial-for-machine-learning |
[3] | https://scilab.io/machine-learning-logistic-regression-tutorial/ |
[4] | https://github.com/perborgen/LogisticRegression/blob/master/logistic.py |
[5] | http://neuralnetworksanddeeplearning.com/chap3.html |
[6] | http://math.stackexchange.com/questions/78575/derivative-of-sigmoid-function-sigma-x-frac11e-x |
Machine Learning Algorithms Cheat Sheet
[7] | https://en.wikipedia.org/wiki/Monotoniconotonic_function |
[8] | http://scikit-learn.org/stable/modules/linear_model.html#logistic-regression> |
Cheat Of Ml
[9] | https://en.wikipedia.org/wiki/Softmax_function |