Calculate the regression coefficient of simple regression analysis with python

Calculation of regression coefficient for simple regression analysis

I made a code to calculate the regression coefficient of simple regression analysis, so please use it if you like!

Library import

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

Dataset example (test.csv)

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

Data reading


dataset = pd.read_csv('test.csv')

Column retrieval

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

Average calculation


# 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

Deviation calculation


# 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 the deviation between X and 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 the product of the deviations of X and y


# Sum of Multiply Deviation X and y
SMD_Xy = sum(MD_Xy)

Deviation square

# 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 deviation


# Sum of Squares of Calcuate Deviation
SSX = sum(Sq_Devi_X)

SSy = sum(Sq_Devi_y)

Calculation of regression coefficient


# Calculate Regression paramator
betaOne = SMD_Xy / SSX
betaZero = Mean_y - betaOne * Mean_X

Recommended Posts

Calculate the regression coefficient of simple regression analysis with python
Machine learning with python (2) Simple regression analysis
Simple regression analysis in Python
Explanation of the concept of regression analysis using python Part 2
Explanation of the concept of regression analysis using Python Part 1
Explanation of the concept of regression analysis using Python Extra 1
Logistic regression analysis Self-made with python
Calculate the square root of 2 in millions of digits with python
Introduction to Simple Regression Analysis with Python (Comparison of 6 Libraries of Numerical Calculation/Computer Algebra System)
Check the existence of the file with python
Tips: [Python] Calculate the average value of the specified area with bedgraph
From the introduction of JUMAN ++ to morphological analysis of Japanese with Python
Calculate the shortest route of a graph with Dijkstra's algorithm and Python
Calculate the probability of being a squid coin with Bayes' theorem [python]
Prepare the execution environment of Python3 with Docker
2016 The University of Tokyo Mathematics Solved with Python
[Note] Export the html of the site with python.
Check the date of the flag duty with Python
Automating simple tasks with Python Table of contents
Convert the character code of the file with Python3
Static analysis of Python code with GitLab CI
[Python] Determine the type of iris with SVM
Easy Lasso regression analysis with Python (no theory)
Data analysis with python 2
the zen of Python
Basics of regression analysis
Voice analysis with python
Regression analysis with NumPy
Data analysis with Python
Regression analysis in Python
Extract the table of image files with OneDrive & Python
2. Multivariate analysis spelled out in Python 1-1. Simple regression analysis (scikit-learn)
[Python] [Word] [python-docx] Simple analysis of diff data using python
Learn Nim with Python (from the beginning of the year).
[Python] Calculate the average value of the pixel value RGB of the object
[OpenCV / Python] I tried image analysis of cells with OpenCV
Visualize the range of interpolation and extrapolation with python
A python implementation of the Bayesian linear regression class
Python Scikit-learn Linear Regression Analysis Nonlinear Simple Regression Analysis Machine Learning
Challenge principal component analysis of text data with Python
Planar skeleton analysis with Python (4) Handling of forced displacement
Summary of the basic flow of machine learning with Python
2. Multivariate analysis spelled out in Python 1-2. Simple regression analysis (algorithm)
Get the operation status of JR West with Python
Extract the band information of raster data with python
Calculate the product of matrices with a character expression?
[Python] Morphological analysis with MeCab
Towards the retirement of Python2
[Co-occurrence analysis] Easy co-occurrence analysis with Python! [Python]
Multiple regression analysis with Keras
Sentiment analysis with Python (word2vec)
About the ease of Python
Static analysis of Python programs
Planar skeleton analysis with Python
Simple FPS measurement of python
Japanese morphological analysis with Python
Calculate the number of changes
[Python] Linear regression with scikit-learn
Call the API with python3.
About the features of Python
Muscle jerk analysis with Python