# Simple Linear Regression In Machine Learning

If you are new to Machine Learning then you can follow this blog for a basic understanding of machine learning.

### What is Simple Linear Regression?

• Simple Linear Regression is a type of Regression algorithm that models the relationship between a dependent variable and a single independent variable. The relationship shown by a Simple Linear Regression model is linear or a sloped straight line, hence it is called Simple Linear Regression.
• Linear Regression is a machine learning algorithm based on supervised learning.  It performs a regression task.
• Linear regression performs the task to predict a dependent variable value (y) based on a given independent variable (x).

Equation :   y = b0 + b1 * x

Here,  y = Dependent Variable (DV) ,  x = Independent Variable (IV) , bo = Constant, b1 = Coefficient

-> Based on salary and user experience we can predict the new hiring user salary, using a simple linear regression model.

-> Equation :   Salary = b0 + b1 * Experience

Here, b0 is Point where the line crosses the vertical axis

-> y-axis to consider salary, and the x-axis to consider experience.

### Let’s start to implement one example for predicting salary based on experience using simple linear regression.

Step 1)  Importing the libraries

```import numpy as np
import matplotlib.pyplot as plt
import pandas as pd```

Step 2 ) Importing the dataset

• Here, Salary_Data.csv is nothing but previous candidate’s salary and experience data, based on this file we can train our model and predict the salary for new candidates.
```dataset = pd.read_csv('Salary_Data.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values```

Step 3 ) Splitting the dataset into the Training set and Test set

```from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 1/3, random_state = 0)```

Step 4 ) Training the Simple Linear Regression model on the Training set

```from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, y_train)```

Step 5 ) Predicting the Test set results

`y_pred = regressor.predict(X_test)`

Step 6) Visualising the Training set results

```plt.scatter(X_train, y_train, color = 'red')
plt.plot(X_train, regressor.predict(X_train), color = 'blue')
plt.title('Salary vs Experience (Training set)')
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.show()```

Step 7) Visualising the Test set results

```plt.scatter(X_test, y_test, color = 'red')
plt.plot(X_train, regressor.predict(X_train), color = 'blue')
plt.title('Salary vs Experience (Test set)')
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.show()```

• Here, still close to the real salary is even for new observation, absolutely our predicted salary which is the blue line is very close to the real salary, so our simple linear regression model wonderful job at predicting new observation.