Machine Learning (Coursera): Part 2/2
167 flashcards · Computer Science · by charlie42
The recommended approach to solving machine learning problems (3 steps)
Quick and dirty model Print learning curves to see how to improve (more examples, more features, etc.) Examine errors in CV set to see if there is a pattern
What's error analysis in machine learning?
Examining errors in CV set to see if there is a pattern, to improve performance of the model
What does precision (machine learning metric) measure?
How many, out of predicted positive cases, are actually positive
Formula for precision (machine learning metric)
True positive / (True positive + False positive)
What does recall (machine learning metric) measure?
Out of all cases of actual positives, how many were predicted as positive
Formula for recall (machine learning metric)
True positive / (True positive + False negative)
What class to we usually call positive?
A more rare one
What happens to precision and recall if we choose to change the standard threshold of logistic regression of 0.5 to 0.7?
Precision will increase Recall will decrease
Why precision is not 0 when the model always predicts y=1
Because we have some actual y=1 values that were predicted correctly
Why do we need a single metric, that puts together precision and recall?
To speed up decision making process when choosing the best classifier. Considering if the ratio is good manually every time is time consuming.
Why the average of precision and recall is not a good metric?
It can give high values when recall is 1 but precision is almost 0 (Model predicts all values as y=1)
Common metric (and it's formula) that combines precision and recall in a way that it's high when both are high, and low when one is low
When is the F1 score is 0?
When either precision or recall is 0
When is the F1 score is 1 (perfect)?
When both precision and recall are 1
Two rules for when adding more data will help to improve performance
An expert could predict y given features X The model is complex enough to have low bias
What does the cost function for a single training example looks like for the SVM when y=1?
What does the cost function for a single training example looks like for the SVM when y=0?
Why can we get rid of the 1/m constant when going from the cost function of logistic regression to the cost function of SVM?
When minimizing a function, multiplying it by a constant doesn't change the resulting value
When minimizing these two equations, what C should be equal to to obtain the same values?
C = 1/lambda
Cost function of SVM
Why do we add 1/2 before the regularization term in cost function
To not have 2 in front of it after we take the partial derivative
What's the difference in the hypothesis between logistic regression and SVM?
Logistic regression outputs the probability, SVM outputs a discrete value (1 or 0)
What does the plot of the cost function of logistic regression over z for one training example looks like when y=1?
What does the plot of the cost function of logistic regression over z for one training example looks like when y=0?
Why does the plot of the cost function of logistic regression over z when y=1 look like this?
For the cost function to be low, h(x) = g(z) should be 1. g(z) is 1 when z is large.
What z is equals to in logistic regression for a single example?
z = θTx
Three differences between SVM and logistic regression cost functions?
In SVM we don't divide the sum of individual cost functions by m In SVM the lambda constant is moved to the other term The cost function for each case (y=1 and y=0) is changes
The difference between SVM and logistic regression hypothesis functions?
The SVM hypothesis function outputs 1 or 0, not a probability
When is the hypothesis function of the SVM is 1?
When θTx >> 0
Why there is no - (minus) sign before each term (for y=1 and y=0) in the SVM cost function, as oppose to the logistic regression cost function?
The - signs just make the logistic regression cost functions for y=0 and y=1 look the way they do In the SVM cost function the shape is fully defined by the cost1 and cost2 terms
Term for multiplying to vectors of the same length?
Vector inner product
How can we represent this vector visually
What's ||u|| and how to calculate it?
Norm of a vector u = euclidian length of the vector u
Hypothesis function for SVMs
How to calculate the vector inner product of u and v using the projection of v onto u?
Two ways to calculate inner vector product of u and v
When is the projection of v onto u is negative?
When the angle between v and u is more than 90o
When is the vector inner product of v and u negative? Why?
When the projection of v onto u is negative => When the angle between v and u is more than 90o
What is a kernel function in SVMs?
Similarity function between a training example and a landmark
Gaussian similarity function formula
Range of similarity function values
(0, 1)
What does the gaussian similarity function with a single landmark look like on a plot, when we have two features, with these two values of sigma
What does the new hypothesis function for SVM looks like with three landmarks
What could be the values of theta if we want to predict y=1 when the training example is close to landmarks l1 and l2, but far from l3? Why?
[-0.5, 1, 1, 0] If x is close to l1 or l2, but far from l3, the value of the equation will be 1 If x is close to l3, but far from l1 and l2, the value of the equation will be -0.5
Where the landmarks are usually located for non-linear SVM kernels?
At the location of each training example
How is one training example represented in SVMs with kernels?
List of similarities of this training example x to all landmarks (instead of list of values at each coordinate x1, x2, etc.) One of the values will be equal to 1, because one of the landmarks is located exactly at the location of this training example
Vectorized hypothesis of SVMs with kernels
Difference between the cost function of the SVM with linear and non-linear kernels Write the cost function of the SVM with non-linear kernel (with vectorized hypothesis function and regularization)
In hypothesis function, the training example is replaced with it's representation with the values of it's similarity with each landmark
Why we shouldn't use kernels with other algorithms like logistic regression?
SVMs are optimized for fast computations with kernels, logistic regression would be very slow with n=m
To decrease bias (increase variance) in an SVM with the gaussian kernel, what should we do with σ?
We should reduce it, it will make the similarity function vary less smoothly
When do we want to use the linear kernel (no kernel) or logistic regression over a non-linear SVM? Why? (2 cases)
When we n >= m. If we try to fit a complex non-linear function there is a risk of overfitting. When m is very large. Gaussian kernel can struggle computationally. Can try adding features manually and running logistic regression or no kernel SVM
Why do we need to use feature scaling when using the SVMs with gaussian kernel?
The norm (the numerator in the similarity function) is decomposed by dimensions (features). If some features take large value ranges, the smaller ranged values won't change the similarity value much.
Two advantages of SVMs over neural networks
Often faster Convex cost function - no local optima (not really a problem in practice with NNs)
3 steps of the k-means algorithm
Randomly initialize k centroids Repeat { Assign each point to the nearest centroid Re-calculate centroids }
k-means: notation for: Centroids Number of clusters Index of a centroid closest to x(i)
μ1, μ2, ... μK K c(i)
k-means: how to assign each point to the nearest centroid (pseudocode)
k-means: how to calculate the closest centroid to a point
k-means: how to recalculate centroids (pseudocode)
k-means: how to calculate the average of points
Sum up the feature vectors, divide the resulting vector by the number of vectors
The optimization objective of k-means (equation)
With regards to what the cost function is minimized during the first step of the inner loop of k-means (assigning each point to a cluster)? Which values are fixed?
c(i) We fix the location of centroids (μK), and minimize the distance between the points and their centroids by assigning points to their closest centroid
With regards to what the cost function is minimized during the second step of the inner loop of k-means (recalculating the centroids)? Which values are fixed?
μi We fix what centroid a point is assigned to (c(i)), and minimize the distance between the point and their centroid by moving the centroid
Cost function of k-means in words
Average distance between each point and the cluster it belongs to
Where are the initial cluster centroids usually initialized?
Exactly at the location of K random training example
What the cost function of k-means is called?
Distortion function
Write the algorithm for avoiding local optima for k-means. When does it have more chance to give good results?
About 100 times: Randomly initialize centroids Run k-means, get c(i...m) and m1...K Compute the final cost function (J) Choose c(i...m) and m1...K values that give the smallest J It gives better results if K is low (2-10). When discovering more clusters, different random initializations are unlikely to change the value of J much
What's the elbow method and how is it used for k-means?
It's used to choose the number of clusters.
Why the elbow method is not used often to choose the number of clusters in k-means?
In real life, the cost function curve doesn't have a clear "elbow"
k-means: two explanations for k=5 having a lower final cost function value than k=3
There are 3 natural clusters in the data The k=5 run fell into a local minimum, try to rerun it with another random centroid initialization
k-means: what's an alternative to the elbow method for choosing k?
Think why you're running k-means, and which number of clusters serves that purpose better
Suppose we apply dimensionality reduction to a dataset of m examples {x(1), x(2), …, x(m)}, where x(i) ∈ Rn. As a result of this, we will get:
A lower dimensional dataset {z(1), z(2), …, z(m)} of m examples where z(i) ∈ Rk, and k ≤ n
Two common use-cases for dimensionality reduction
Data compression (k ≤ n) Data visualization (k ≤ n, k = 2 or 3)
In words, what's the goal of 2D to 1D PCA?
To find a direction (a vector u ∈ Rn) onto which to project all data points so as to minimize the projection error
In words, what's the goal of PCA?
To find k vectors u(1), u(2),..., u(k) onto which to project all data points so as to minimize the projection error
2 differences between linear regression and PCA?
In linear regression we minimize the difference in z. In PCA we minimize projection error (shortest distance between the point and the line) In linear regression there is a distinct variable y that we want to predict. In PCA we treat all variables equally.
What x(i) ∈ ℝ2 becomes after dimensionality reduction?
z(i) ∈ ℝ (Now we only need one number to represent the data points)
Notation for covarience matrix
Σ
Two functions in octave to compute eigenvectors
eig svd (singular value decomposition)
What does covariance matrix look like for two and three variables?
Covariance matrix formula when mean = 0
What does this part of covariance matrix formula represent?
"Instantaneous" covariance matrix, for one training example
Dimensions of the covariance matrix (explain why)
Which eigenvector is used for the PCA algorithm? What does it represent?
U Each column will be the u vector onto which the data will be projected. We take the first k vectors to which we want to reduce the dimensions
Dimensions of the U matrix
n x n
PCA: How to get new training examples, in reduced dimensions?
(Ureduced)T * X Ureduced is U with only k first columns
What do we need to do with the data before running the PCA algorithm?
Mean normalization (mandatory) Feature scaling (highly recommended)
Full PCA algorithm
Difference between the X matrix for PCA and for Linear regression
We don't add the x0 = 1 column for PCA
Explain (PCA)
To calculate column j of z we need to project all columns of x to the new j vector
What approximate reconstruction of the original data from the result of PCA looks like on a graph (2D)?
Formula to calculate approximate reconstruction of the original data after the PCA
What PCA wants to minimize (+ formula)
Average squared projection error
Formula for total variation in the data and what it represents
Average length of between each data point and the origin
Formula to calculate how much varience is retained by PCA
(99% varience is retained)
Alternative (faster) way to calculate how much varience in retained by PCA
where
What does the matrix S look like
Visualize what does this formula calculates on the matrix S
For k=3,
Common acceptable % of varience retained (PCA)
95-99%
Describe the procedure regarding PCA and training and test sets
Learn the PCA mapping on training set (Ureduced, parameters for mean normalization and feature scaling) Apply the parameters on the test set.
2 misuses of PCA
to prevent overfitting not trying to run the algorithm without it first