Wednesday, January 8, 2014

Simplifying ML: Impact of degree of polynomial degree on bias, variance and other insights

This post takes off from my earlier post Simplifying Machine Learning: Bias, variance, regularization and odd facts- Part 4. As discussed earlier a poor hypothesis function could either underfit or overfit the data.  If the number of features selected were small of the order of 1 or 2 features, then we could plot the data and try to determine how the hypothesis function fits the data. We could also see whether the function is capable of predicting output target values for new data.

However if the number of features were large for e.g. of the order of 10’s of features then there needs to be method by which one can determine if the learned hypotheses is a ‘just right’ fit for all the data.
The following technique can be used to determine the ‘goodness’ of a hypothesis or how well the hypothesis can fit the data and can also generalize to new examples not in the training set.
Several insights on how to evaluate a hypothesis is  given below
Consider a hypothesis function
hƟ (x) = Ɵ0 + Ɵ1x1 + Ɵ2x22 + Ɵ3x33  +  Ɵ4x44
a1
The above hypothesis does not generalize well enough for new examples in the data set.
Let us assume that there 100 training examples or data sets. Instead of using the entire set of 100 examples to learn the hypothesis function, the data set is divided into training set and test set in a 70%:30% ratio respectively
The hypothesis is learned from the training set. The learned hypothesis is then checked against the 30% test set data to determine whether the hypothesis is able to generalize on the test set also.
This is done by determining the error when the hypothesis is used against the test set.
For linear regression the error is computed by determining the average mean square error of the output value against the actual value as follows
The test set error is computed as follows
Jtest(Ɵ) = 1/2mtest Σ(hƟ (xtesti  - ytesti)2
For logistic regression the test set error is similarly determined as
Jtest(Ɵ) = = 1/mtest Σ -ytest * log(hƟ (xtest))  - (1-ytest) * (log(1 - hƟ (xtest))
The idea is that the test set error should as low as possible.
Model selection
A typical problem in determining the hypothesis is to choose the degree of the polynomial or to choose an appropriate model for the hypothesis
The method that can be followed is to choose 10 polynomial models
  1. hƟ (x) = Ɵ0 + Ɵ1x1
  2. hƟ (x) = Ɵ0 + Ɵ1x1 + Ɵ2x22
  3. hƟ (x) = Ɵ0 + Ɵ1x12 + Ɵ2x22 + Ɵ3x33
  4. …
Here‘d’ is the degree of the polynomial. One method is to train all the 10 models. Run each of the model’s hypotheses against the test set and then choose the model with the smallest error cost.
While this appears to a good technique to choose the best fit hypothesis, in reality it is not so. The reason is that the hypothesis chosen is based on the best fit and the least error for the test data. However this does not generalize well for examples not in the training or test set.
So the correct method is to divide the data into 3 sets  as 60:20:20 where 60% is the training set, 20% is used as a test set to determine the best fit and the remaining 20% is the cross-validation set.
The steps carried out against the data is
  1. Train all 10 models against the training set (60%)
  2. Compute the cost value J against the cross-validation set (20%)
  3. Determine the lowest cost model
  4. Use this model against the test set and determine the generalization error.
Degree of the polynomial versus bias and variance
How does the degree of the polynomial affect the bias and variance of a hypothesis?
Clearly for a given training set when the degree is low the hypothesis will underfit the data and there will be a high bias error. However when the degree of the polynomial is high then the fit will get better and better on the training set (Note: This does not imply a good generalization)
We run all the models with different polynomial degrees on the cross validation set. What we will observe is that when the degree of the polynomial is low then the error will be high. This error will decrease as the degree of the polynomial increases as we will tend to get a better fit. However the error will again increase as higher degree polynomials that overfit the training set will be a poor fit for the cross validation set.
This is shown below
a2
Effect of regularization on bias & variance
Here is the technique to choose the optimum value for the regularization parameter λ
When λ is small then Ɵi values are large and we tend to overfit the data set. Hence the training error will be low but the cross validation error will be high. However when λ is large then the values of Ɵi become negligible almost leading to a polynomial degree of 1. These will underfit the data and result in a high training error and a cross validation error. Hence the chosen value of λ should be such that the cross validation error is the lowest
a3
Plotting learning curves
This is another technique to identify if the learned hypothesis has a high bias or a high variance based on the number of training examples
A high bias indicates an underfit. When the number of samples in training set if low then the training error and cross validation error will be low as it will be easy to create a hypothesis if there are few training examples. As the number of samples increase the error will increase for the training set and will slightly decrease for the cross validation set. However for a high bias, or underfit, after a certain point increasing the number of samples will not change the error. This is the case of a high bias
a4
In the case of high variance where a high degree polynomial is used for the hypothesis the training error will be low for smaller number of training examples. As the number of training examples increase the error will increase slowly. The cross validation error will be high for lesser number of training samples but will slowly decrease as the number of samples grow as the hypothesis will learn better. Hence for the case of high variance increasing the number of samples in the training set size will decrease the gap between the cross validation and the training error as shown below
a5
Note: This post, line previous posts on Machine Learning,  is based on the Coursera course on Machine Learning by Professor Andrew Ng

Simplifying Machine Learning: Bias, Variance, regularization and odd facts – Part 4

In both linear and logistic regression the choice of the degree of the polynomial for the hypothesis function is extremely critical. A low degree for the polynomial can result in an underfit, while a very high degree can overfit the data as shown below
41
The figure on the left the data is underfit as we try to fit the data with a first order polynomial which is a straight line. This is a case of strong ‘bias’
The rightmost figure a much higher polynomial is used. All the data points are covered by the polynomial curve however it is not effective in predicting other values. This is a case of overfitting or a high variance.
The middle figure is just right as it intuitively fits the data points the best possible way.
A similar problem exists with logistic regression as shown below
42
There are 2 ways to handle overfitting
a)      Reducing the number of features selected
b)      Using regularization
In regularization the magnitude of the parameters Ɵ is decreased to reduce the effect of overfitting
Hence if we choose a hypothesis function
hƟ (x) = Ɵ0 + Ɵ1x12 + Ɵ2x22 + Ɵ3x33 +  Ɵ4x44

The cost function for this without regularization as mentioned in earlier posts
J(Ɵ) = 1/2m Σ(hƟ (xi  - yi)2
Where the key is minimize the above function for the least error
The cost function with regularization becomes
J(Ɵ) = 1/2m Σ(hƟ (xi  - yi)2 + λ Σ Ɵj2

As can be seen the regularization now adds a factor Ɵj2  as a part of the cost function which needs to be minimized.
Hence with the regularization factor the problem of underfitting/overfitting can be solved
43
However the trick is determine the value of λ. If λ is too big then it would result in underfitting or resulting in a high bias.
Similarly the regularized equation for logistic regression is as shown below
J(Ɵ) = |1/m Σ  -y * log(hƟ (x))  - (1-y) * (log(1 - hƟ (x))  | + λ/2m Σ Ɵj2
Some tips suggested by Prof Andrew Ng while determining the parameters and features for regression
a)      Get as many training examples. It is worth spending more effort in getting as much examples
b)      Add additional features
c)      Observe changes to the learning algorithm with different values of λ
Note: This post, in line with my previous posts on Machine Learning,  is based on the Coursera course on Machine Learning by Professor Andrew Ng


Wednesday, December 11, 2013

A method to crowd source pothole marking on (Indian) roads

In, India, roads and potholes are 2 sides of the same coin! You cannot think of one in exclusion of another. This post of mine looks at a novel technique of rapidly identifying & marking potholes in (Indian) roads. This approach can be used for any city in the world but is very pertinent to Indian roads.
This idea of mine provides a technique of quickly marking pothole in roads through the method of crowd sourcing

Introduction: It is a well known fact that Indian roads are riddled with potholes. Some may even say that there are potholes with patches of road in between them. This disclosure looks at a novel technique of rapidly identifying & marking potholes in (Indian) roads. The approach can be used for any city in the world. However this disclosure will focus on Indian roads. This disclosure proposes a novel technique of crowd-sourcing the marking of potholes on roads rather than having any single government body (NHAI etc) travel on roads to make the markings.
Description: This post proposes a novel crowd-sourced method for pothole marking that will be easy to conduct and extremely rapid The crowd-sourced pothole marking application will be made of the following components namely Pot-hole marking app, Backend server, Map Matching utility, Pothole ranking utility.
Pothole marking App: A location based smartphone app will need to be created preferably both on Android and iOS. The app will display the map with buttons to mark the following
a)   Points in map of potholes
b)   Bad segments of roads with potholes
Backend Server: The backend server will collect all the data (marked potholes) and bad segments of roads and will update a database.  A map-matching utility will map the latitude, longitude of the marked point on to a map. When the geographical location of a pothole is received (latitude, longitude) the backend server will also store the time stamp.
Pothole ranker: This module will run on a periodical basis, say once every 3 minutes. This module will determine all the potholes that have been entered in the last 3 minutes and add to the accumulated count of marked potholes. Each marked pothole will hold the count of the marks and also the time stamp of the mark. It will also rank the criticality of the pothole based on the accumulated count of potholes over the period.
The pothole ranker will maintain the following metrics
  1. Pothole criticality = Total accumulated count/ Total time
  2. Pothole impact measure = Max rate of pothole marks (Pothole marks/hr)
  3. Bad stretches of roads with many potholes =
Number of adjacent potholes/ Distance in meters
cs
Description: This how the scheme will work in practice. The app will be uploaded into Google Play and Apple’s App store.  All users who would like to participate in the pothole marking exercise can download and install the app on their smart phones. These users when they are traveling on a road can mark potholes as they encounter them. It is assumed that the users are passengers in vehicles or pillion riders. The fact that users all over the city can simultaneously mark potholes as they encounter them will make the gathering of pothole data rapid and extremely accurate. A map of a city would need to be generated with the circles/points for locations of potholes, color-coded appropriately. We could use the color red for higher ranked potholes and yellow for lower ranked potholes with intermediate colors like purple, pink etc.  This data can then be used by Government bodies in addressing roads in fixing the roads.
There are three advantages of crowd sourcing the pothole marking
1)   The process of gathering data is rapid
2)   Roads where the traffic is heaviest will have potholes with a higher rank and can be addressed first
3)   The process will be very accurate
Crowd sourcing of pothole marking will have the following benefits
  1. The marking of potholes will be extremely rapid
  2. The potholes will be ranked based on accumulated count
  3. Ranking of potholes can be done on
- Total accumulated count/Total time
- Rate of pothole mark
- Critical segments with major potholes
4. It will be easy to segregate
- Critical potholes
- Max impactful potholes
- Bad road segment
  1. The process will be very accurate
Conclusion: The process of crowd sourcing pothole marking of Indian roads will be extremely efficient in marking potholes and bringing it to the attention of the Government.
A map of a city with the circles for locations of potholes, color-coded appropriately, to indicate higher marked potholes versus the lower ranked potholes could be generated. This map can be used to bring to the attention of the government the really bad roads and terrible road segments. Rather than having a couple of vehicles trying to ply roads and mark roads this will be very fast and extremely accurate.
Afterword: The concept of crowd sourcing for traffic is not new. Waze, which Google bought for close to $2bn does just that. It crowd sources traffic conditions and alerts users of the app. Also I did a Google search on using mobile apps for potholes marking and, not surprisingly, there were others who had also thought of a similar idea in Boston & Florida see the links below
  1. http://www.cityofboston.gov/doit/apps/citizensconnect.asp
  2. http://dailycrowdsource.com/20-resources/projects/421-crowdsourced-pothole-database-to-track-road-repair
However, I personally think that the situation in India is different, where there are ‘roads in between potholes’ ;-). While in the above 2 cases in US, only the location of the potholes is important, my idea ranks potholes based on the accumulated count and the rate of pothole marks. These metrics can be used by the government in addressing those sections of roads where the potholes have a higher rank i.e. where the traffic is highest.
Your thoughts are welcome.

Saturday, November 16, 2013

Simplifying ML: Neural networks- Part 3

Neural networks try to overcome the shortcomings of logistic regression in which  we have to choose a non-linear hypothesis. Logistic regression requires that we choose an appropriate combination of polynomial terms and the order of the equation. The problem with this is sometimes we either tend to overfit or underfit. Neural networks allow the ability to learns new model parameters from the basis raw parameters.
The neural network is modeled on the neural networking ability of the human brain. The brain is made of trillions of neurons. Each neuron is a processing unit which has several inputs in the dendrites and an output the axon. The neurons communicate thro a combination of electro chemical signal at the synapses or the spaces between the neuron.
neuron
A neural network mimics the working of the neuron.
So in a neural network the features of the problem serve as input. For e.g in the case of being able to determine if a mail is spam or not the features could be the words in the subject line, the from address, the contents etc. Based on a combination of these features we need to classify whether the mail is spam or not.
31
The above diagram shows a simple neural network with features x1, x2, x3 and a bias unit x0

With a hypothesis function hƟ(x) = 1/(1 + e-x)
The edges from the features xi  are the model parameters Ɵ. In other words the edges represent weights.
A typical neural network is a network of many logistic units organized in layers. The output of each layer forms the input to the next subsequent layer. This is shown below
32
As can be seen in a multi-layer neural network at the left we have the features x1,x2, .. xn.
This at the layer becomes the activation unit. The key advantage of neural networks over regular logistic regression that learns the models parameters is that learned model parameters are input to the next subsequent layers which learn the model parameters more finely. Hence this gives a better fit for the combination of parameters.
The activation parameters at the next layer are
a12 = g(Ɵ101x0+ Ɵ111x1+ Ɵ121x2 + Ɵ131x3) where g is the logistic function or the sigmoid function discussed in my previous post Simplifying ML: Logistic regression - Part 2
33
Here a12 is the activation parameter at layer 1
Ɵ10 is the model parameter at layer 1 and is the 0th parameter. Similarly Ɵ11 is the model parameter at layer 1 and is the 1st parameter and so on.
Similarly the other activation parameters can be written as
a22 = g(Ɵ201x0+ Ɵ211x1+ Ɵ221x2 + Ɵ231x3)
a32 = g(Ɵ301x0+ Ɵ311x1+ Ɵ321x2 + Ɵ331x3)
hƟ(x) = a13 = g(Ɵ102a0+ Ɵ112a1+ Ɵ122a2 + Ɵ132a3  - (A)

The crux of neural networks is that instead of creating a hypothesis based on the set of raw features, the neural network with multiple hidden layers can learn its own features. In the equation (A) we can see that the hypothesis is not a function of the input raw features x1,x2,… xn  but on a new set of features or the activation units a1,a2, … an . In other words the network has ‘learned’ its own features.
As mentioned above the output of each layer is the logistic function or the sigmoid function
The beauty of neural networks based on logistic functions is that we can easily realize the equivalent of logic gates like AND, OR, NOT, NOR etc.
The hypothesis for the above network would be
34
hƟ(x) = g(-30 + 20 * x1 + 20 * x2)
So for x1= 0 and x2 = 0 we would have
hƟ(x) = g(-30 + 0 + 0) = g(-30)
Since g(-30) < g(0) < 0.5 = 0
37
Similarly a NOT gate can be constructed with a neural network as follows
35
38
Neural networks can also be used for multi class classification.
36
Hence there are multiple advantages to neural networks. Neural networks are amenable to a) creating complex logic models of combinations of AND, NOT, OR gates
b) The model parameters are learned from the raw parameters and can be more flexible.
It appears that the interest in neural networks surged in the 1980s and then waned, The neural networks were similar to the above and were based on forward propagation. However it appears that in recent time’s backward propagation has been used successfully in areas of research known as ‘deep learning’
This is based on the Coursera course on Machine Learning by Professor Andrew Ng. A highy enjoyable and classic course!!!

Simplifying ML: Logistic regression – Part 2

Logistic regression is another class of Machine Learning algorithms which comes under supervised learning. In this regression technique we need to classify data. Take a look at my earlier post Simplifying Machine Learning algorithms - Part 1 I had discussed linear regression. For e.g if we had data on tumor sizes versus the fact that the tumor was benign or malignant, the question is whether given a tumor size we can predict whether this tumor would be benign or cancerous. So we need to have the ability to classify this data.
This is shown below
4
It is obvious that a line with a certain slope could easily separate the two.
As another example we could have an algorithm that is able to automatically classify mail as either spam or not spam based on the subject line. So for e.g if the subject line had words like medicine, prize, lottery etc we could with a fair degree of probability classify this as spam.
However some classification problems could be far more complex.  We may need to classify another problem as shown below.
5
From the above it can be seen that hypothesis function is second order equation which is either a circle or an ellipse.
In the case of logistic regression the hypothesis function should be able to switch between 2 values 0 or 1 almost like a transistor either being in cutoff or in saturation state.
In the case of logistic regression 0 <= hƟ <= 1
The hypothesis function uses function of the following form
g(z) = 1/(1 + e‑z)
and hƟ (x) = g(ƟTX)
6
The function g(z) shown above has the characteristic required for logistic regression as it has the following shape
The function rapidly asymptotes at 1 when hƟ (x) >= 0.5 and  hƟ (x) asymptotes to 0 when hƟ (x) < 0.5
As in linear regression we can have hypothesis function be of an appropriate order. So for e.g. in the ellipse figure above one could choose a hypothesis function as follows
hƟ (x) = Ɵ0 + Ɵ1x12 + Ɵ2x22 + Ɵ3x1 +  Ɵ4x2

or

hƟ (x) = 1/(1 + e –(Ɵ0 + Ɵ1x12 + Ɵ2x22 + Ɵ3x1 +  Ɵ4x2))
We could choose the general form of a circle which is
f(x) = ax2 + by2 +2gx + 2hy + d
The cost function for logistic regression is given below
Cost(hƟ (x),y) = { -log(hƟ (x))             if y = 1
-log(1 - hƟ (x)))       if y = 0
In the case of regression there was a single cost function which could determine the error of the data against the predicted value.
The cost in the event of logistic regression is given as above as a set of 2 equations one for the case where the data is 1 and another for the case where the data is 0.
The reason for this is as follows. If we consider y =1 as a positive value, then when our hypothesis correctly predicts 1 then we have a ‘true positive’ however if we predict 0 when it should be 1 then we have a false negative. Similarly when the data is 0 and we predict a 1 then this is the case of a false positive and if we correctly predict 0 when it is 0 it is true negative.
Here is the reason as how the cost function
Cost(hƟ (x),y) = { -log(hƟ (x))             if y = 1
-log(1 - hƟ (x)))       if y = 0
Was arrived at. By definition the cost function gives the error between the predicted value and the data value.
The logic for determining the appropriate function is as follows
For y = 1
y=1 & hypothesis = 1 then cost = 0
y= 1 & hypothesis = 0 then cost = Infinity
Similarly for y = 0
y = 0 & hypotheses  = 0 then cost = 0
y = 0 & hypothesis = 1 then cost = Infinity
and the the functions above serve exactly this purpose as can be seen
7
Hence the cost can be written as
J(Ɵ) = Cost(hƟ (x),y) = -y * log(hƟ (x))  - (1-y) * (log(1 - hƟ (x))
This is the same as the equation above
The same gradient descent algorithm can now be used to minimize the cost function
So we can iterate througj
Ɵj =   Ɵj – α δ/δ Ɵj J(Ɵ0, Ɵ1,… Ɵn)
This works out to a function that is similar to linear regression
Ɵj = Ɵj – α 1/m { Σ hƟ (xi) – yi} xj i
This will enable the machine to fairly accurately determine the parameters Ɵj for the features x and provide the hypothesis function.
This is based on the Coursera course on Machine Learning by Professor Andrew Ng. Highly recommended!!!

Friday, November 15, 2013

Simplifying Machine Learning algorithms – Part 1

Machine learning or the ability to use computers to predict values, classify data or identify patterns is truly a fascinating field. It is amazing how algorithms can come to conclusions on data. Detecting patterns is a inborn ability of the human mind. But our mind cannot handle large quantities of data with many features. It is here that machines have an edge over us.
This post is inspired by the Machine Learning course at Coursera conducted by Professor Andrew Ng of Stanford. The lectures are truly lucid and delivered with amazing clarity. In a series of post I will be trying to distil the meaning and motivation behind the algorithms that are part of machine learning.
There are 2 major types of learning
a)      Supervised learning b) Unsupervised learning
Supervised learning: In supervised learning we have to infer the relationship between input data and output values. The intention of supervised learning is determine the possible out for some random input once the relationship has been determined. Some examples of supervised learning are linear regression, logistic regression etc.
Unsupervised learning: In unsupervised learning the problem is to determine patterns and structure in unlabeled data. Some examples of unsupervised learning are K-Means clustering, hidden Markov models etc.
In this post I would like to take a look at Supervised Learning algorithms
Linear Regression
In regression problems we try to infer the relationship between a set of input parameters to an output value. Let us we have data for the number of rooms vs. price of the house as shown below
1
Depending on the data we could either fit a straight line or use a linear fit. Alternatively we could fit a higher order curve to data.
The function that determines the relationship is also known as hypothesis function. This can be represented as follows for e.g a hypothesis function with a single feature
hƟ(x) = Ɵ1x+ Ɵ0

The above equation is the hypothesis function where Ɵ is the parameter and x is the feature
We could have a higher order hypothesis function as follows
hƟ(x) = Ɵ2x2+ Ɵ1x+Ɵ0

To evaluate whether the hypothesis function is able to map the input and related output accurately is known as the ‘cost function’.
The cost function can be represented as
J(Ɵ) = 1/2m Σ(hƟ (xi)  - y i)2
The cost function really calculates the ‘mean squared error’ of the actual data points (y) with the points on the hypothesis function (hƟ). Clearly higher the value of J(Ɵ) the greater is the error in predicting the output based on a set of input parameters. If we just took the error instead of the squared error then if there were data points on either side of the predicted line then the positive & negative errors could cancel out. Hence the approach is usually to take the mean of the squared error.
2
The goal would be to minimize the error which will result in the best fit.
So the approach would be to choose values for the parameters Ɵi
The algorithm that is used for determining the values of the parameters that will result in the minimum error is gradient descent
The formula is
Ɵj := Ɵj – αd/d Ɵj J(Ɵ)
Where α is the learning rate
Gradient descent starts by picking a random value for Ɵi. Then the algorithm looks around to search for the next combination that will take us down fastest. By continuing this process the local minima is determined.
Gradient descent is based on the observation that if the multivariable function  is defined and differentiable in a neighborhood of a point , then  decreases fastest if one goes from  in the direction of the negative gradient. This is shown in the below diagram taken from Wikipedia.
Gradient_descent.svg
For e.g for a curve as shown below
3
This how I think the gradient descent works. In the above diagram at point A the slope is +ve and taking the negative of the slope multiplied by the learning factor α and subtracting it from Ɵj will result in a value that is less than Ɵj. That is we move towards the minima or C. Similarly at point B the slope will be -ve. If we multiply by  - α then we will add to Ɵj. Hence we will move to the right or towards point C.
By applying the iterative process of gradient descent we can get the combination of parameter values for  Ɵ that will provide the best fit for the set of data points
The iterative process of gradient descent is applied to minimize the cost function which is function of the error in the current hypothesis
δ/δ J(Ɵ) = δ/ δ Ɵ * 1/2m Σ(hƟ (xi)  - y i)2

This process is applied iteratively to the below equation to arrive at the values of Ɵi
The formula is
Ɵj := Ɵj – αd/d Ɵj J(Ɵ)
to obtain the values for the best fit equation
hƟ(x) = Ɵ2xn+ Ɵ1xn-1+ …+  Ɵ0