1. 程式人生 > >最小二乘法擬合

最小二乘法擬合

import numpy as np


#P145 例題2
x=np.array([-1,-0.75,-0.5,-0.25,0,0.25,0.5,0.75,1])
y=np.array([-0.2209,0.3295,0.8826,1.4392,2.0003,2.5645,3.1334,3.7601,4.2836])

#習題六 2
x=np.array([1,3,4,5,6,7,8,9,10])
y=np.array([10,5,4,2,1,1,2,3,4])

#習題六 4
x=np.array([19,25,31,38,44])
y=np.array([19.0,32.3,49.0,73.3,97.8])

print
(len(x)) #print(np.sum(x)) print(np.sum(x**2)) #print(np.sum(x**3)) print(np.sum(x**4)) print(np.sum(y)) #print(np.sum(x*y)) print(np.sum((x**2)*y)) # a=([len(x),np.sum(x),np.sum(x**2)], # [np.sum(x),np.sum(x**2),np.sum(x**3)], # [np.sum(x**2),np.sum(x**3),np.sum(x**4)]) # b=(np.sum(y),np.sum(x*y),np.sum((x**2)*y))
a=([len(x),np.sum(x**2)], [np.sum(x**2),np.sum(x**4)]) b=(np.sum(y),np.sum((x**2)*y)) x=np.linalg.solve(a,b) print(x)