# Simple Linear Regression

Simple linear regression is a regression technique in which the independent variable has a linear relationship with the dependent variable. The straight line in the diagram is the best fit line. The main goal of the simple linear regression is to consider the given data points and plot the best fit line to fit the model in the best way possible.

The best fit line can be based on the linear equation given below.

y=b0+b1x

## Cost function

# Linear Regression Use Cases

- Sales Forecasting
- Risk Analysis
- Housing Applications To Predict the prices and other factors
- Finance Applications To Predict Stock prices, investment evaluation, etc.

Basic idea behind this simple linear regression is the relationship between the dependent and independent variables.

In real life we can use it to predict the exam results using studying hours.

# Implementation of Linear Regression

- Importing the libraries

import numpy as np

import matplotlib.pyplot as plt

import pandas as pd

2. Importing the dataset

dataset = pd.read_csv(‘Salary_Data.csv’)

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

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

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)

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)

5. Predicting the Test set results

y_pred = regressor.predict(X_test)

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()

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()

# Output

*~ Reference Edureka, Udemy & Image credits : internet*