I made a code to calculate the regression coefficient of simple regression analysis, so please use it if you like!
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
Column 1 | Column 2 |
---|---|
2.2 | 71 |
4.1 | 81 |
5.5 | 86 |
1.9 | 72 |
3.4 | 77 |
2.6 | 73 |
4.2 | 80 |
3.7 | 81 |
4.9 | 85 |
3.2 | 74 |
dataset = pd.read_csv('test.csv')
Extract columns with X as the explanatory variable and y as the objective variable.
X = dataset.iloc[:, :-1].values #Indepand variable
y = dataset.iloc[:, 1].values #Depand variable
# Calculate Mean
Sum_X = sum(X)
N_X = len(X)
Mean_X = Sum_X / N_X
Sum_y = sum(y)
N_y = len(y)
Mean_y = Sum_y / N_y
# Calcuate Deviation
Devi_X = []
for Row_X in X:
Devi_X.append(Row_X - Mean_X)
Devi_y = []
for Row_y in y:
Devi_y.append(Row_y - Mean_y)
# Multiply Deviation X and y
counter = 0
MD_Xy = []
while counter < len(Devi_X):
MD_Xy_Value = Devi_X[counter] * Devi_y[counter]
MD_Xy.append(MD_Xy_Value)
counter += 1
# Sum of Multiply Deviation X and y
SMD_Xy = sum(MD_Xy)
# Squares of Calcuate Deviation
Sq_Devi_X = []
for DX in Devi_X:
Sq_Devi_X.append(DX * DX)
Sq_Devi_y = []
for Dy in Devi_y:
Sq_Devi_y.append(Dy * Dy)
# Sum of Squares of Calcuate Deviation
SSX = sum(Sq_Devi_X)
SSy = sum(Sq_Devi_y)
# Calculate Regression paramator
betaOne = SMD_Xy / SSX
betaZero = Mean_y - betaOne * Mean_X