[PYTHON] Regression analysis with NumPy

Regression analysis was mentioned earlier in The topic of linear regression.

Regression analysis with NumPy

Now, let's actually write the code and perform regression analysis. np.polyfit and np.polyval /reference/generated/numpy.polyval.html#numpy.polyval) can be used to perform regression analysis of two variables with an n-th order equation.

For details, it is better to refer to the document from the above link, but it is as follows.

import numpy as np
import matplotlib.pyplot as plt

x = np.array([0.0, 1.0, 2.0, 3.0,  4.0,  5.0])
y = np.array([0.0, 0.8, 0.9, 0.1, -0.8, -1.0])
z = np.polyfit(x, y, 3)

p = np.poly1d(z)
p30 = np.poly1d(np.polyfit(x, y, 30))
xp = np.linspace(-2, 6, 100)

plt.plot(x, y, '.', xp, p(xp), '-', xp, p30(xp), '*')
plt.ylim(-2,2)
plt.show()
plt.savefig('image.png')

image.png

At this time In the case of a linear equation, p [0]: slope and p [1]: intercept. For N-th order equation, p [0] * t ** (N-1) + p [1] * t ** (N-2) + ... + p [N-2] * t + p [N-1] ] is.

Regarding the least squares method I mentioned before, the data strings {(x_1, y_1), (x_2, y_2), ... (x_n,, Determine the model that minimizes the residual sum of squares for y_n)}. At this time, it is assumed that the variance of the error distribution of the observed values is constant.

Multiple regression analysis

Regression analysis with m independent variables is [Multiple Regression Analysis](http://en.wikipedia.org/wiki/%E9%87%8D%E5%9B%9E%E5%B8%B0%E5% 88% 86% E6% 9E% 90).

z = ax + by + c

I want to find the formula.

x = [9.83, -9.97, -3.91, -3.94, -13.67, -14.04, 4.81, 7.65, 5.50, -3.34]
y = [-5.50, -13.53, -1.23, 6.07, 1.94, 2.79, -5.43, 15.57, 7.26, 1.34]
z = [635.99, 163.78, 86.94, 245.35, 1132.88, 1239.55, 214.01, 67.94, -1.48, 104.18]

import numpy as np
from scipy import linalg as LA

N = len(x)
G = np.array([x, y, np.ones(N)]).T
result = LA.solve(G.T.dot(G), G.T.dot(z))

print(result)

This [ -30.02043308 3.55275314 322.33397214] Can be obtained

z = -30.0x + 3.55y + 322

Is obtained, and this is the solution.

reference

2013 Mathematics Study Group Materials for Programmers http://nineties.github.io/math-seminar/

Recommended Posts

Regression analysis with NumPy
Kernel regression with Numpy only
Multiple regression analysis with Keras
Implementing logistic regression with NumPy
Logistic regression analysis Self-made with python
Poisson regression analysis
Regression analysis method
Machine learning with python (2) Simple regression analysis
Data analysis with python 2
Basket analysis with Spark (1)
Linear regression with statsmodels
Moving average with numpy
Dependency analysis with CaboCha
Voice analysis with python
Regression with linear model
Getting Started with Numpy
Basics of regression analysis
Learn with Cheminformatics NumPy
Hamming code with numpy
Voice analysis with python
Dynamic analysis with Valgrind
Try regression with TensorFlow
Data analysis with Python
Easy Lasso regression analysis with Python (no theory)
Extend NumPy with Rust
Regression analysis in Python
Calculate the regression coefficient of simple regression analysis with python
I wrote GP with numpy
[Python] Morphological analysis with MeCab
CNN implementation with just numpy
Artificial data generation with numpy
[Python] Calculation method with numpy
Try matrix operation with NumPy
Ridge regression with Pyspark's Mllib
Diffusion equation animation with NumPy
Sentiment analysis with Python (word2vec)
Debt repayment simulation with numpy
Implemented SMO with Python + NumPy
Stick strings together with Numpy
Texture analysis learned with pyradiomics
Planar skeleton analysis with Python
Japanese morphological analysis with Python
Linear regression method using Numpy
Handle numpy arrays with f2py
Simple regression analysis in Python
[Python] Linear regression with scikit-learn
Use OpenBLAS with numpy, scipy
Muscle jerk analysis with Python
Python3 | Getting Started with numpy
[PowerShell] Morphological analysis with SudachiPy
Text sentiment analysis with ML-Ask
Robust linear regression with scikit-learn
Perform least squares fitting with numpy.
First simple regression analysis in Python
3D skeleton structure analysis with Python
Impedance analysis (EIS) with python [impedance.py]
Machine learning algorithm (multiple regression analysis)
Draw a beautiful circle with numpy
Text mining with Python ① Morphological analysis
Machine learning algorithm (simple regression analysis)
Linear regression with Student's t distribution