Simple Machine Learning Model in Python in 5 lines of code

Raman Sah
Towards Data Science
3 min readMar 6, 2017

--

In this blog, we will train a Linear Regression Model and expect to perform correct on a fresh input.

The basic idea of any machine learning model is that it is exposed to a large number of inputs and also supplied the output applicable for them. On analysing more and more data, it tries to figure out the relationship between input and the result.

Consider a very primitive example when you have to decide whether to wear a jacket or not depending on the weather. You have access to the training data as we call it -

+---------------------+---------------+
| Outside Temperature | Wear a Jacket |
+---------------------+---------------+
| 30°C | No |
| 25°C | No |
| 20°C | No |
| 15°C | Yes |
| 10°C | Yes |
+---------------------+---------------+

Somehow, your mind finds a connection between the input (temperature) and the output (decision to wear a jacket).

So, if the temperature is 12°C, you would still wear a jacket although you were never told the outcome for that particular temperature.

Now, lets move on to a slightly better algebraic problem which the computer will solve for us.

Before we begin, don’t forget to install scikit-learn, it provides easy to use functions and predefined models which saves a lot of time

pip install scikit-learn

Sample Training Set

Here, X is the input and y is the output.

Given the training set you could easily guess that the output (y) is nothing but (x1 + 2*x2 + 3*x3).

How To Generate Training Set

The ML Model -Linear Regression

Working with linear regression model is simple. Create a model, train it and then use it :)

Train The Model

We have the training set ready, so create a Linear Regression Model and pass it the training data.

Test Data

X = [[10, 20, 30]]

The outcome should be 10 + 20*2 + 30*3 = 140. Let’s see what we got…

Outcome : [ 140.]
Coefficients : [ 1. 2. 3.]

Did you notice what just happened? The model had access to the training data, through which it calculated the weights to assign to the inputs to arrive at the desired output. On giving test data, it successfully managed to get the right answer!

I also experiment a lot and tinker with code. Feel free to fork ML Prototype where I have tried to expose the functions of scikit-learn through API. And don’t forget to clap if you find this article interesting.

--

--