Mastering Simple Linear Regression with Python

Linear Regression serves as a cornerstone in the realm of machine learning, specifically for predictive modeling. In this comprehensive blog post, we will not only introduce the concept of Simple Linear Regression but also provide an in-depth walkthrough of its implementation using Python. We'll leverage powerful libraries such as NumPy for numerical operations, Matplotlib for data visualization, Pandas for data manipulation, and scikit-learn for machine learning algorithms.


Understanding Simple Linear Regression

Definition:

Simple Linear Regression is a statistical method that models the relationship between a dependent variable (target) and a single independent variable (feature). The objective is to find the best-fitting linear relationship to predict the dependent variable based on the independent variable.

Formula:

The simple linear regression equation is given by:

Y = mX + b

Where:

- Y is the dependent variable (target),

- X is the independent variable (feature),

- m is the slope or coefficient,

- b is the intercept.


Implementation in Python


Importing the Libraries

The initial step involves importing the necessary libraries:

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


Importing the Dataset

Load the dataset using Pandas:

dataset = pd.read_csv('/content/Salary_Data.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values

Here,  X represents the independent variable (Years of Experience), and y represents the dependent variable (Salary).


Splitting the Dataset

Evaluate the model's performance by splitting the dataset into training and testing sets:

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)


Training the Simple Linear Regression Model

Train the model using scikit-learn's `LinearRegression` class:

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


Predicting and Visualizing Results

Make predictions and visualize the results:

y_pred = regressor.predict(X_test)


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


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


Making Predictions and Obtaining the Regression Equation

Make a single prediction and reveal the coefficients of the regression equation:

# Making a single prediction (e.g., salary of an employee with 12 years of experience)
print(regressor.predict([[12]]))


Getting the final linear regression equation with the values of the coefficients

print(regressor.coef_)
print(regressor.intercept_)


The model predicts that the salary of an employee with 12 years of experience is $138967.5. The final linear regression equation is expressed as Salary = 9312.57 * YearsExperience + 26780.09.


Conclusion

Mastering Simple Linear Regression empowers you to understand and model relationships between variables. With Python and the mentioned libraries, the process becomes not only accessible but also insightful, allowing you to make informed predictions based on data.

AJ Blogs

Hello everyone, My name Arth and I like to write about what I learn. Follow My Website - https://sites.google.com/view/aj-blogs/home

Post a Comment

Previous Post Next Post
Best Programming Books

Facebook

AJ Facebook
Checkout Our Facebook Page
AJ Blogs
Checkout Our Instagram Page