numpy で2変数の回帰分析

  • np.polyfit(x, y, n)
    • n次式で2変数の回帰分析
  • np.polyval(p, t):
    • pで表される多項式に t を代入し、値を計算して返す
    • p[0]*t**(N-1) + p[1]*t**(N-2) + ... + p[N-2]*t + p[N-1]
#!/usr/bin/env python
#coding: utf-8

import matplotlib.pyplot as plt
import numpy as np


def read():
    labels = (("car_price", "gas_price", "weight", "co2", "power", "duration"))
    
    ret = []
    ret.append((8895,33,2560,97,113,12))
    ret.append((7402,33,2345,114,90,8))
    ret.append((6319,37,1845,81,63,12))
    ret.append((6635,32,2260,91,92,13))
    ret.append((6599,32,2440,113,103,13))
    ret.append((8672,26,2285,97,82,12))
    ret.append((7399,33,2275,97,90,13))
    ret.append((7254,28,2350,98,74,8))
    ret.append((9599,25,2295,109,90,13))
    ret.append((8748,29,2390,97,102,13))
    ret.append((6488,35,2075,89,78,13))
    ret.append((9995,26,2330,109,100,9))
    ret.append((11545,20,3320,305,170,8))
    ret.append((9745,27,2885,153,100,8))
    ret.append((12164,19,3310,302,225,8))
    ret.append((11470,30,2695,133,110,9))
    ret.append((9410,33,2170,97,108,13))
    ret.append((13945,27,2710,125,140,13))
    ret.append((13249,24,2775,146,140,10))
    ret.append((10565,23,2640,151,110,8 ))
    ret.append((10320,26,2655,133,95,9  ))
    ret.append((10945,25,3065,181,141,12))
    ret.append((9483,24,2750,141,98,9   ))
    ret.append((12145,26,2920,132,125,13))
    ret.append((12459,24,2780,133,110,12))
    ret.append((10989,25,2745,122,102,13))
    ret.append((17879,21,3110,181,142,12))
    ret.append((11650,21,2920,146,138,13))
    ret.append((9995,23,2645,151,110,9  ))
    ret.append((11499,23,2935,135,130,13))
    ret.append((11588,27,2920,122,115,13))
    ret.append((18450,23,2985,141,114,10))
    ret.append((24760,20,3265,163,160,13))
    ret.append((13150,21,2880,151,110,10))
    ret.append((12495,22,2975,153,150,9 ))
    ret.append((16342,22,3450,202,147,10))
    ret.append((15350,22,3145,180,150,9 ))
    ret.append((13195,22,3190,182,140,10))
    ret.append((14980,23,3610,232,140,8 ))
    ret.append((23300,21,3480,180,158,13))
    ret.append((17899,22,3200,180,160,13))
    ret.append((13150,21,2765,151,110,9 ))
    ret.append((21498,23,3480,180,190,10))
    ret.append((16145,23,3325,231,165,10))
    ret.append((14525,18,3855,305,170,8 ))
    ret.append((17257,20,3850,302,150,10))
    ret.append((15395,18,3735,202,150,10))
    ret.append((12267,18,3665,182,145,10))
    ret.append((14944,19,3735,181,150,13))
    
    return labels, np.array(ret)


def work(x, y, xLabel, yLabel):
    fig = plt.figure()
    ax = fig.add_subplot(1,1,1)

    poly = np.polyfit(x, y, 1) # 1次式で回帰分析
    yy = np.polyval(poly, x)   # poly[0]: 傾き,  poly[1]: 切片
    ax.plot(x,yy)

    poly = np.polyfit(x, y, 2) # 2次式で回帰分析
    xx = np.arange(x.min(),x.max(),1000)
    yy = np.polyval(poly,xx)   # poly[0] * t^2 + poly[1] * t^1 + poly[0]
    ax.plot(xx,yy)
    
    ax.plot(x,y,linestyle="None",marker="o")
    ax.set_xlabel(xLabel)
    ax.set_ylabel(yLabel)

    fig.savefig("polyfit.png",bbox_inches="tight")


if __name__ == "__main__":
    labels, X = read()
    work(X[:,0], X[:,4], labels[0], labels[4])