# Simple Linear Regression Model using Python

The very first step in ML is the Simple Linear Regression model, so let’s get started.

Before learning about simple linear regression, let’s understand what is Regression and then what is Linear regression.

## Regression:

In simple words, our problem is a regression problem when the output variable is **real **or **continues **value, such as ‘salary’, ‘price’ or ‘weight’.

**Linear Regression:**

Linear regression is a **linear **approach to modeling the relationship between **independent **and **dependent **variables.

## Simple Linear Regression:

SLR is a linear approach to modeling the relationship between a ** single **independent and dependent variable.

Now you know what SLR is, let’s code it using python language.

I will divide it into six steps,

- Importing the Libraries
- Importing data
- Splitting the data into training and testing sets.
- Training SLR model
- Predicting the result
- Visualizing the result

I will provide a link for the code in the end...

# 1. Importing the Libraries:

As I mentioned earlier we will train our model in python, and python is full of libraries. We will use NumPy, matplotlib, Scikit-learn, and pandas.

- Numpy- Numpy provides an array object that is 50x faster than traditional python lists.
- Matplotlib- Matplot is a plotting library that helps us to visualize our results.
- Scikit-learn- Sklearn is a machine learning library for python, it features various algorithms like support vector machine, simple linear regression, etc.
- Pandas- Pandas are used for data analysis.

`# importing the libraries`

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

# 2. Importing Data:

Now that we have imported libraries, our next step is to import our data. To import the data we will use the pandas' library.

We will use data that contains User rating and price. You can download the data using the following link: https://github.com/RAHUL-KAD/Machine-Learning/blob/main/Regrassion/Data/UserRating_Price.csv

`# importing data`

dataset = pd.read_csv('file_name.csv')

After importing data we will split our variables into independent and dependent variables. X will contain an independent variable and y will contain a dependent variable.

`# spliting into independent and dependent variable`

X = dataset.iloc[:, :-1].values

y = dataset.iloc[:, -1:].values

# 3. Splitting the data into training and testing sets:

Splitting our data into training and testing is an important part of data evaluation. We will split most of the data into a training set and a smaller portion into a testing set. we split our data so we could perform some testing or prediction on our testing data and if the result is satisfactory then we use training data. We use this method to minimize the effect of data discrepancies.

To split the data we will use the scikit learn library, from scikit learn we will import train_test_split class

`# importing the scikit learn library`

from sklearn.model_selection import train_test_split

Now we will initialize four variables X_train, X_test, y_train, and y_test. They will store our splitting data.

`# Spliting the data`

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

train_test_split() is object of class train_test_split, parameter** test_size** splits our data into 80% of training data and 20% of testing data.

# 4. Training a simple linear regression model:

We will train our model on the training data set for that, we will use the LinearRegression class from the scikit learn library.

`# Training the model`

from sklearn.linear_model import LinearRegression

LR = LinearRegression()

LR.fit(X_train, y_train)

LinearRegression() is an object of the class LinearRegression and using a fit() method we fit our data into a linear regression class that trains our model.

# 5. Predicting the result:

Now our model is ready to predict the test result. We will use the predict() method to predict the result.

`# prediting the test`

LR.predict(X_test)

After running the code cell you will see a 2D array containing the predicted price of the user's rating.

You can enter your own rating and get a predicted price.

Note: You have to use 2D array to give a user input cause our X and y matrixs are 2D arrays.

`# predicting the result using users input`

LR.predict([[4.5],[4.9]])

## Now your model is ready to use in the real world.

# 6. Visualizing the result:

It’s time to visualize our result, we will use the Matplotlib library.

**Visualizing the training result:**

`# Scattering our data`

plt.scatter(X_train, y_train, color = 'red')

# ploting the linear line

plt.plot(X_train, LR.predict(X_train), color = 'blue')

# giving the title

plt.title('User rating VS Price - training result')

# Xlabel

plt.xlabel('User rating')

# ylabel

plt.ylabel('Price')

# Showing the plot

plt.show()

**Visualizing the testing result:**

`# Scattering our data`

plt.scatter(X_test, y_test, color = 'red')

# ploting the linear line

plt.plot(X_train, LR.predict(X_train), color = 'blue')

# giving the title

plt.title('User rating VS Price - testing result')

# Xlabel

plt.xlabel('User rating')

# ylabel

plt.ylabel('Price')

# Showing the plot

plt.show()

## Code link:

Now use another dataset and train your own model, best of luck…