Explanation of the concept of regression analysis using Python Extra 1

Least squares understood by animation

Explanation of the concept of regression analysis using Python Part 1 uses the least squares method to draw the optimal straight line for the data. I explained that the parameters are set to minimize the difference (error) of. Here, as an extra edition, I tried to draw a graph by animating how each parameter changes. If you try this way, you will get an image.

The "sum of square errors" in the title of the graph is the sum of squares of the errors, so the best position is where this is the smallest.

The inclination changes

First, let's see how the inclination changes.

regression_anim.gif

I am using the matplotlib.animation.FuncAnimation function to output animation with matplotlib. Let's go out of the function that draws the graph and take the argument to change the value by animation. Here we are creating an animate function. This animate function is called in the FuncAnimation function, and the values set in nframe from 0 to frames are set in order as arguments and called.

import numpy as np
import matplotlib.pyplot as plt
from moviepy.editor import *
from matplotlib import animation as ani

data= np.loadtxt('cars.csv',delimiter=',',skiprows=1)
data[:,1] = map(lambda x: x * 1.61, data[:,1])    #km from mph/Convert to h
data[:,2] = map(lambda y: y * 0.3048, data[:,2])  #Convert from ft to m

def animate(nframe):
    plt.clf()        # clear graph canvas
    slope = 0.746606334842 * (float(nframe)/50) *2   #The slope changes as the argument nframe changes
    intercept = - 5.41583710407
    x = np.linspace(0,50,50)
    y = slope * x + intercept
    plt.ylim(-10,80)
    plt.xlim(0,50)
    plt.xlabel("speed(km/h)")
    plt.ylabel("distance(m)")
    plt.scatter(data[:,1],data[:,2])
    # draw errors
    se = 0
    i = 0
    for d in data:
        plt.plot([d[1],d[1]],[d[2],d[1]*slope+intercept],"k")
        se += (y[i] - d[2]) ** 2
        i += 1
    plt.title("Stopping Distances of Cars (slope=%.3f, sum of square errors=%5d)" % (slope, se))
    # based line: y = 0.74x -5
    plt.plot(x,y)


fig = plt.figure(figsize=(10,6))

anim = ani.FuncAnimation(fig, animate, frames=50, blit=True)

anim.save('regression_anim.mp4', fps=13)

clip = VideoFileClip("regression_anim.mp4")
clip.write_gif("regression_anim.gif")


How the intercept changes

Unlike before, the intercept moves.

regression_anim_i.gif


def animate(nframe):
    plt.clf()        # clear graph canvas
    slope = 0.746606334842 
    intercept = -5.41583710407 + (float(nframe-25)/50) * 50   #The intercept changes as the nframe of the argument changes
    x = np.linspace(0,50,50)
    y = slope * x + intercept
    plt.ylim(-30,80)
    plt.xlim(0,50)
    plt.xlabel("speed(km/h)")
    plt.ylabel("distance(m)")
    plt.scatter(data[:,1],data[:,2])
    # draw errors
    se = 0
    i = 0
    for d in data:
        plt.plot([d[1],d[1]],[d[2],d[1]*slope+intercept],"k")
        se += (y[i] - d[2]) ** 2
        i += 1
    plt.title("Stopping Distances of Cars (slope=%.3f, sum of square errors=%5d)" % (slope, se))
    # based line: y = 0.74x -5
    plt.plot(x,y)


fig = plt.figure(figsize=(10,6))

anim = ani.FuncAnimation(fig, animate, frames=50, blit=True)

anim.save('regression_anim_i.mp4', fps=13)

clip = VideoFileClip("regression_anim_i.mp4")
clip.write_gif("regression_anim_i.gif")

Recommended Posts

Explanation of the concept of regression analysis using Python Extra 1
Explanation of the concept of regression analysis using python Part 2
Explanation of the concept of regression analysis using Python Part 1
Calculate the regression coefficient of simple regression analysis with python
Review the concept and terminology of regression
Time variation analysis of black holes using python
Find the geometric mean of n! Using Python
Data analysis using Python 0
the zen of Python
Basics of regression analysis
Regression analysis in Python
Predicting the future of Numazu's population transition by time-series regression analysis using Prophet
[Python] [Word] [python-docx] Simple analysis of diff data using python
Cut a part of the string using a Python slice
A python implementation of the Bayesian linear regression class
The pain of gRPC using Python. November 2019. (Personal memo)
Study from the beginning of Python Hour8: Using packages
Towards the retirement of Python2
About the ease of Python
Static analysis of Python programs
python: Basics of using scikit-learn ①
Simple regression analysis in Python
About the features of Python
Data analysis using python pandas
The Power of Pandas: Python
3. Natural language processing with Python 5-1. Concept of sentiment analysis [AFINN-111]
View using the python module of Nifty Cloud mobile backend
[Python] LASSO regression with equation constraints using the multiplier method
The story of Python and the story of NaN
Japanese Natural Language Processing Using Python3 (4) Sentiment Analysis by Logistic Regression
First simple regression analysis in Python
[Python] The stumbling block of import
First Python 3 ~ The beginning of repetition ~
[Python] I wrote the route of the typhoon on the map using folium
Python: Application of supervised learning (regression)
[Python] PCA scratch in the example of "Introduction to multivariate analysis"
Existence from the viewpoint of Python
[Python] LINE notification of the latest information using Twitter automatic search
pyenv-change the python version of virtualenv
[Python] I thoroughly explained the theory and implementation of logistic regression
Change the Python version of Homebrew
[In-Database Python Analysis Tutorial with SQL Server 2017] Step 6: Using the model
[Python] Understanding the potential_field_planning of Python Robotics
Evaluate the performance of a simple regression model using LeaveOneOut cross-validation
Review of the basics of Python (FizzBuzz)
Implementation of desktop notifications using Python
Get and set the value of the dropdown menu using Python and Selenium
Extract the targz file using python
Try using the Python Cmd module
From the introduction of JUMAN ++ to morphological analysis of Japanese with Python
Recommendation of data analysis using MessagePack
Logistic regression analysis Self-made with python
About the basics list of Python basics
Supplement to the explanation of vscode
Recommendation tutorial using association analysis (concept)
Learn the basics of Python ① Beginners
I tried logistic regression analysis for the first time using Titanic data
[AtCoder explanation] Control the A, B, C problems of ABC182 with Python!
Installation method using the pip command of the Python package (library) Mac environment
[AtCoder explanation] Control the A, B, C problems of ABC186 with Python!
Display the result of video analysis using Cloud Video Intelligence API from Colaboratory.