With that under my belt, I went back to Dr Ng’s lecture notes (references 1 & 2) and tried again. In concept, Dr Brownlee’s and Dr. Ng’s algorithms are very similar for the hypothesis function. However, they differ in how to get B0 and B1.
Dr. Brownlee’s Algorithm (reference 6)
y = B0 + B1 * x
B1 = sum((xi-mean(x)) * (yi-mean(y))) / sum((xi – mean(x))^2)
B0 = mean(y) – B1 * mean(x)
Dr. Ng’s Algorithm (reference 1 & 2)
y = B0 + B1 * x
B0 = 1/2 m sum((xi-yi)^2)
I will only talk about Dr. Ng’s algorithm here since the previous blog posts cover Dr. Brownlee’s algorithm. What confused me earlier with Dr. Ng’s algorithm is you must guess B1 value to find B0. In doing so, you keep the lowest B0 value and use it in the hypothesis function. Data set 2 is one from Dr. Ng’s lecture, so I did it first.
To make Dr. Ng’s algorithm work, I put together a couple of functions. The first is one that returns the largest x for a dataset.
The second function takes the largest x and returns an array of B1 values to be used in calculating the lowest B0.
When run, the output shows that the lowest B0 is 0 and its corresponding B1 value is 1.
Using B0 and B1, predictions can be made.
The other dataset that this algorithm works with is the one Dr. Brownlee used.
The dataset this algorithm does not work with is the housing price one.
I find Dr. Brownlee’s Algorithm a lot simpler and easier to use than Dr. Ng’s. It works with all three datasets. Dr. Ng’s requires a lot of guess work on how to find B0 based on guessing at values for B1. At least in this implementation, this doesn’t seem to work for the house price data set. I am sure it is something I am doing wrong and my next blog post will deal with finding the source of the issue.