본문 바로가기
Autonomous Vehicle

Points Polynomial Line Fitting (Python Code)

by kim.jeff 2021. 11. 15.

 

 

x, y fitting , 1차 다항식

import numpy as np

x = [1., 2., 3., 4., 5.]
y = [0.8, 2.2, 3.1, 3.9 5.1]

x = np.array(x)
y = np.array(y)
order = 1

yfit = np.polyfit(x,y,order)

 

 

 

x,y 를 z 에 맞춤, 2차 다항식

import warnings
from astropy.modeling import models, fitting

x = [1., 1., 1., 2., 2., 2., 3., 3., 3.]
y = [1., 2., 3., 1., 2., 3., 1., 2., 3.]
z = [-0.05, 0.45, -0.1, 0.51, 1.05, 0.48, 0.0, 0.5, 0.01]

order = 2

p_init = models.Polynomial2D(degree=order)
fit_p = fitting.LevMarLSQFitter()
with warnings.catch_warnings():
    warnings.simplefilter('ignore')
    model = fit_p(p_init, x, y, z)

xmin = min(x)
xmax = max(x)
ymin = min(y)
ymax = max(y)
dx = 0.05
dy = 0.05
xtmp = np.arange(xmin-dx, xmax+dx, dx)
ytmp = np.arange(ymin-dy, ymax+dy, dy)
xfit, yfit = np.meshgrid(xtmp,ytmp)
zfit = model(xfit,yfit)
coeff = model.parameters

 

 

출처 : https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=seul4ever1&logNo=221650777702 

 

python polynomial fitting (1D & 2D)

파이썬에서 numpy의 polyfit을 이용하여 간단히 polynomial fitting을 할 수 있다. 이때 order는 차수이며,...

blog.naver.com