Linear Regression Machine Learning Algorithm from scratch

Introduction:

Linear Regression from Scratch without sklearn
Linear Regression from Scratch without sklearn

General Terms:

Import Libraries:

import numpy as np
import pandas as pd

Load Data:

df = pd.read_csv('Linear-Regression-Data.csv')
df.head()
Dataset for Linear Regression From Scratch

Implementation:

mean_yoe = sum(df['YearsExperience']) / float(len(df['YearsExperience']))
mean_salary = sum(df['Salary']) / float(len(df['Salary']))
The calculating mean of a pandas data frame column
def variance(values, mean):
return sum([(val-mean)**2 for val in values])
def covariance(yearsexperience, mean_yoe, salary , mean_salary):
covariance = 0.0
for r in range(len(yearsexperience)):
covariance = covariance + (yearsexperience[r] - mean_yoe) * (salary[r] - mean_salary)
return covariance
variance_yoe, variance_salary = variance(df['YearsExperience'], mean_yoe), variance(df['Salary'], mean_salary)
variance_yoe , variance_salary
covariance_yoe_salary = covariance(df['YearsExperience'],mean_yoe,df['Salary'],mean_salary)
covariance_yoe_salary
Calculating the variance and covariance of pandas data columns

Linear fitment:

m = covariance_yoe_salary/ variance_yoe
c = mean_salary - m * mean_yoe
Finding the coefficients for Linear Regression
Prediction using Linear Regression Model

Endnotes:

--

--

--

Data Scientist & Machine Learning Evangelist. I like to mess with data. dhiraj10099@gmail.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Introducing: Battle Racers Weekly Duels

A complete Guide for exploiting Smart car’s CAN — bus

Launching The First Hacker WG in Berlin

How HackerLoft can look like

Mac Os X Roomba App

Point & click to move and working with Nav mesh for smart AI in Unity

ReInventing static sites with AWS

Deprek8ion: Staying ahead of K8s API deprecations

HTO: This is $MANA, but in SPACE (Metaverse)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dhiraj K

Dhiraj K

Data Scientist & Machine Learning Evangelist. I like to mess with data. dhiraj10099@gmail.com

More from Medium

Linear Regression — GRE Admission Predictor

DIY Gradient Descending Linear Regression

Complete guide of Linear Regression built from scratch

Beginner Machine Learning: 2) Multiple Linear Regression in Python