Why?

Why this technology is relevant and what the scope.

Computers transformed the way we interact with things in our daily life. We have a software in almost everything we use, from toaster to our cars. We are witnessing an age where it is being taken to the next level by integrating AI to our daily life things and thereby solving a vast majority of problems we face in medical science to space exploration. AI is the next big thing.

AI will create 133 million jobs by 2025.

Take-Away Skills

After following this resource what you will learn.

Syllabus

Summary of what learn to get started.

The content

Details about the technology and learning materials.

What is Linear Regression ?

Linear regression is a statistical approach for modelling relationship between a dependent variable with a given set of independent variables.

https://media.geeksforgeeks.org/wp-content/uploads/python-linear-regression.png

<aside> 💡 In Linear Regression, we try to find a linear function that predicts the response value(y) as accurately as possible as a function of the feature or independent variable(x).

</aside>

A scatter plot of above dataset looks like,

https://media.geeksforgeeks.org/wp-content/uploads/python-linear-regression-1.png

Regression Line

Now, the task is to find a line which fits best in above scatter plot so that we can predict the response for any new feature values. (i.e a value of x not present in dataset)

Here, we are going to use the sklearn ****to do that.

Numpy and Matplotlib

<aside> 💡 These are helper packages used for doing machine learning

</aside>

Numpy

<aside> 💡 Numpy provides arrays and tools for working with this arrays in python.

</aside>

We import numpy in python like this

import numpy as np

and use it like this

array = np.zeros([5, 5])
print(array)
"""
array([[ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.]])
"""
# we just created an array of shape 5x5 full of zeros!

Matplotlib

<aside> 💡 Matplotlib help us to create plots from data

</aside>

We import matplotlib like this

import matplotlib.pyplot as plt

Then create data

# Prepare the data
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
y = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

And plot like this,

# Plot the data
plt.plot(x, y, label='linear')

# Add a legend
plt.legend()

# Show the plot
plt.show()

The plot looks like this.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/349f8206-f0f6-4234-bea4-ef8a0adbdb6d/Screenshot_2019-10-11_at_2.51.39_PM.png

Installing the Libraries

To install python check out this resource

Python installation

To install numpy try this command in terminal

pip install numpy

To install matplotlib

pip install matplotlib

To install sklearn

pip install sklearn

Building a Linear Regression model using Numpy and sklearn

Sklearn

<aside> 💡 Sklearn or sci-kit learn is a tool for machine learning and data analysis

</aside>

We are going to build a linear regression model using sklearn for the above dataset

Steps

  1. Importing sklearn

    from sklearn.linear_model import LinearRegression
    
  2. Preparing dataset

    # Prepare the data
    x = [[0], [1], [2], [3], [4], [5], [6], [7], [8], [9]]
    y = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    
  3. Creating Linear Regression model object

    model = LinearRegression()
    
  4. Training the model

    model.fit(x, y)
    

    Yes it is that simple!!!!!

  5. Making prediction

    output = model.predict([[10]])
    print(output)
    # array([11.])
    
  6. Plotting : Assignment

    Actual y data and predicted y data.

    y_actual = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    y_predict = model.predict(x)
    

    Try writing code for plotting both actual (x, y) data and predicted (x, y) data.

<aside> 👩 Level: Advanced

</aside>

<aside> 💼 Career Path: ‣

</aside>

Top links

Machine Learning Mastery