UrbanPro
true

Learn Machine Learning from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Linear Regression Without Any Libraries

S
Saumya Rajen Shah
19/08/2017 0 0
I am here to help you understand and implement Linear Regression from scratch without any libraries. Here, I will implement this code in Python, but you can implement the algorithm in any other programming language of your choice just by basically developing 4-5 simple functions.
 
So now, first of all, what exactly is Linear Regression?
As discussed in my blog on Machine Learning, Linear Regression is used to identify linear relationships between the input features x(i) and the output labels of the training set y(i) and thus form a function F(x(i),θ), which would help in predicting future values.
 
This function, is called hypothesis and is usually denoted by h(x(i),θ). Note that, x(lowercase) is used to denote a single training example as a whole, where as we use X (i,j) is used to point the jth feature for the ith training example. But confusing?? Let's simplify it!!

 



As shown, to show the whole feature set for a single example, we use x(1). We can also point to the first feature in third example in training set as X(3,1) or x1(3).

For simplicity of understanding, let's assume, there is only one feature in our dataset of 10 examples. Let's plot it on a graph.

So, let's say, the equation of the line we are supposed to fit the curve is h(x(i),θ)= θ0 + x1θ1.
Or for simplicity, let's say we have one more feature x0 which is always equal to 1 so that we can rewrite hθ (x(i))= θ0 x01 x1.
Or for multiple features, we can write it as h(x(i),θ)= θ0 x0+ θ1 x1+ θ2 x2+ θ3x3…… θn xn for n number of features.
Therefore, in short, hθ(x(i))= i=1n∑  (θi*xi).
 
Now, the code for our hypothesis will be as follows:
def HypoThe(Theta,xi):    if(len(Theta)==len(xi)):        sum=0        for i in range(len(xi)):            sum+=(Theta[i]*xi[i])        return sum    else:        return False
 
Now, since we have defined a mathematical model for prediction, the next problem we face will be finding the rest set of parameters Theta(θ).
 
To find Theta, let's break it into basic steps:
1. Assume a pair of Theta.
2. Compute the prediction error.
3. Assume again so that it minimizes the Prediction Error.
 
Sounds complicated? Well it's quite simple actually. Suppose we have selected some particular values for parameter θ. Now, we will devise a new cost function, which basically calculates the prediction error for θ.
 
J(θ)= i=1m( hθ(x(i))-y(i))2/ (2*m)
 
What is means is that, we predict the output using the hθ(x(i)) function, and subtract it from actual label y(i), and then square it and add it for all training examples(1….m). We average it out using (2*m). Why did we use 2*m instead of m, we'll find it out soon. Let’s just say, it's for the simplicity, and also because we are dealing with a mean-squared computed error function which we are supposed to minimize.
 

Although, cost function is useful only for debugging purposes, and not in actual implementation, we'll still code it out.
 
def RegCostFunc(Theta,X,Y):    sum1=0    for i in range(len(X)):        sum1+=((HypoThe(Theta, X[i])-Y[i])**2)    J=sum1    return J/(2*len(X))


Now, what we are supposed to do next is find those value for Theta for which J(θ) reaches it minimum. If you are familiar with calculus, what we're supposed to do is to find a the minima for the J(θ) function. To solve this optimization problem, we'll be using the gradient descent algorithm.
 
So what exactly does it do?
Let's write down the algorithm first.
repeat until convergence: {
 
for all values of θ­ simultaneously update,
 θj= θj - α * (? J(θ)/ ?θj)
}
 
Let's understand this equation first.
(? J(θ)/ ?θj) means that take partial derivative of J(θ) w.r.t θj while rest of the θ are constant.
Therefore
? J(θ)/ ?θj= ? (  i=1m∑( hθ(x(i))-y(i))2/ (2*m) )  / ?θj.
 
If you solve this using calculus, we find that,
? J(θ)/ ?θj=  i=1m∑( hθ(x(i))-y(i))2 * xj(i)/ m
 
This basically means, while keeping the rest constant, decrease the value of θj in the direction of the slope of the function J(θ).
 
α is the learning rate by which, we reduce the value of the θj. If α is too large, the function will never converge actually.
 
Note that, do not update θj continuously, but update the whole θ simultaneously.
i.e. after one complete loop.
 
Rewriting the Algorithm, we get.
repeat until convergence: {
 
for all values of θ­ simultaneously update,
 θj= θj - α * ( i=1m∑( hθ(x(i))-y(i))2 * xj(i)/ m )
}
 
 
Let's code this now,
def GradTerm(X,Y,Theta,i):    sum1=0    for j in range(len(X)):        sum1+=((HypoThe(Theta,X[j])-Y[j])*X[j][i])    return sum1

Note that, here, I have divided the Gradient Descent Algorithm from the Gradient Term above, for the simplicity of the understanding of our code.
 
def GradDesc(Theta,alpha,Xfeature,Ylabels):    Theta_=[]    for i in range(0,len(Theta)):        Theta_.append(Theta(i)-(alpha* GradTerm(Xfeature,Ylabels,Theta,i)/len(Xfeature)))    return Theta_
 
 
Now we are done with everything required. So how to Run all these functions for proper functioning?
 
Basically, here is how we'll proceed:
 
1. We'll call linear regression and provide it with a Features and Labels.
2. We'll add feature x0 = 1 to every example in data set.
3. We'll initialize Theta=[0]*len(Features[0]). (i.e.  number of parameters should be equal to number of features )
4. We'll select a particular value of alpha and no. of  iterations, and run gradient descent algorithm to minimize the parameters θ.
Make sure to keep alpha as small as possible in the order of 0.000001 or less and the number of iterations to be around 100,000-1,000,000 for better results.
 
def LinearRegression(Xfeature,Ylabels,alpha,iterations):    if len(Xfeature)!=len(Ylabels):        print("Missing Data");        return False    else:           for i in range(len(Xfeature)):            Xfeature[i].insert(0,1)        Theta=[0]*len(Xfeature[0])        for i in range(iterations):            print("\nIteration Number ",i)            print(Theta)            Theta=GradDesc(Theta, alpha, Xfeature, Ylabels)        print(Theta)        return Theta            


Now, I know you can easily implement it using scikit-learn library, but then, it's just a black-box module for machine learning. You'll never understand what is happening inside of it. However, if you have studied machine learning in detail, once, you'll have better understanding in using the sci-kit library and tweak the values accordingly for better results.

<
0 Dislike
Follow 3

Please Enter a comment

Submit

Other Lessons for You

Tuning Parameters Of Decision Tree Models
Implementations of the decision tree algorithm usually provide a collection of parameters for tuning how the tree is built. The defaults in Rattle often provide a basically good tree. They are certainly...

Decision Tree or Linear Model For Solving A Business Problem
When do we use linear models and when do we use tree based classification models? This is common question often been asked in data science job interview. Here are some points to remember: We can use any...

Linear Regression and its types
Linear Regression A Linear regression is a Regression Analysis technique which is used for modeling the predictions on the continuous data. A Linear Regression can be modelled using 1. A Simple Regression...

Learn the secret of mastering machine learning fast.
There are many ways to master machine learning but let me give you the secret of doing it fast. The general technique for mastering machine learning is learning Python or R first. This is not the right...

An Introduction to Probability and Theory of Counting
In this lesson, we introduce the concept of probability with high school mathematics as a prerequisite. Before we start, I want to make you familiar with some standard terms in probability theory. The...
X

Looking for Machine Learning Classes?

The best tutors for Machine Learning Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Machine Learning with the Best Tutors

The best Tutors for Machine Learning Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more