Online linear regression in Python

Introduction

Online estimation of linear regression $ y = ax + b $ coefficients $ a, b $ in Python.

Formula

Least squares

--Reference: Regression analysis (1) -Covariance and correlation, linear simple regression

a = \frac{\overline{xy} - \bar{x}\bar{y}}{\overline{x^2} - \bar{x}^2}
, \hspace{2em} b = \bar{y} - a \bar{x}

Online estimation of mean value

\bar{x}_n = \alpha \bar{x}_{n-1} + (1-\alpha)x_n

Source code

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import scipy as sp
sp.seterr(divide='ignore', invalid='ignore')

alpha = 0.9
def mean(old, new):
    return new if sp.isnan(old) else alpha * old + ( 1.0 - alpha ) * new

def plot(fig):
    xyhist = sp.ones([100, 2]) * sp.nan
    mean_x  = sp.array([sp.nan])
    mean_y  = sp.array([sp.nan])
    mean_x2 = sp.array([sp.nan])
    mean_xy = sp.array([sp.nan])

    ax = fig.gca()
    ax.hold(True)
    ax.grid(True)
    ax.set_xlim([0, 1.0])
    ax.set_ylim([0, 1.0])

    xyscat = ax.scatter([],[], c='black', s=10, alpha=0.4)
    approx = ax.add_line(plt.Line2D([], [], color='r'))

    def inner(i):
        x = sp.random.rand()
        y = x + 0.05 * sp.random.normal()

        xyhist[:-1, :] = xyhist[1:, :]
        xyhist[-1, 0] = x
        xyhist[-1, 1] = y

        mean_x[:]  = mean( mean_x,  x )
        mean_y[:]  = mean( mean_y,  y )
        mean_xy[:] = mean( mean_xy, x *  y )
        mean_x2[:] = mean( mean_x2, x ** 2 )

        a = ( mean_xy - mean_x * mean_y ) / ( mean_x2 - mean_x ** 2 )
        b = mean_y - a * mean_x

        xyscat.set_offsets(xyhist)
        approx.set_data([0, 1], [b, a*1+b])
        ax.title.set_text('y = %.3fx %+.3f' % (a, b))
        plt.draw()

    return inner


fig = plt.figure()
ani = animation.FuncAnimation(fig, plot(fig), interval=300, frames=100)
ani.save('result.gif', writer='imagemagick')

result

result.gif

Recommended Posts

Online linear regression in Python
Online Linear Regression in Python (Robust Estimate)
Linear regression in Python (statmodels, scikit-learn, PyMC3)
Linear search in Python
Regression analysis in Python
Implemented in Python PRML Chapter 3 Bayesian Linear Regression
Multiple regression expressions in Python
Simple regression analysis in Python
[Python] Linear regression with scikit-learn
"Linear regression" and "Probabilistic version of linear regression" in Python "Bayesian linear regression"
Linear regression
Coursera Machine Learning Challenges in Python: ex1 (Linear Regression)
First simple regression analysis in Python
Quadtree in Python --2
Basic Linear Algebra Learned in Python (Part 1)
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
SendKeys in Python
Eigenvalues and eigenvectors: Linear algebra in Python <7>
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
I implemented Cousera's logistic regression in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Introduction to Vectors: Linear Algebra in Python <1>
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
EV3 x Python Machine Learning Part 2 Linear Regression
Identity matrix and inverse matrix: Linear algebra in Python <4>
Inner product and vector: Linear algebra in Python <2>
Matrix Calculations and Linear Equations: Linear Algebra in Python <3>
Sorted list in Python
Daily AtCoder # 36 in Python