最小二乘
import numpy as np
import numpy.matlib
import matplotlib.pyplot as plt
data=np.array(([19,25,31,38,44],[19.0,32.3,49.0,73.3,97.8]))
x1=data[0]
y1=data[1]
print("x1的值:",end=" ")
print(x1)
print("y1的值:",end=" ")
print(y1)
plt.plot(x1,y1,'o')
min1=np.min(x1)
max1=np.max(x1)
min2=np.min(y1)
max2=np.max(y1)
plt.xlim(min1-1,max1+1)
plt.ylim(min2-1,max2+1)
plt.show()
col=data.shape[1]
row=data.shape[0]
print("data的行数:",end=" ")
print(data.shape[0])
print("data的列数:",end=" ")
print(data.shape[1])
X1=x1**2
X1=X1.reshape(5,1)
X2=x1.reshape(5,1)
X3=np.ones((col,1))
X=np.hstack((X1,X2))
X=np.hstack((X,X3))
print("X的值:")
print(X)
x=X.T
print("X的转置的值:")
print(x)
X=np.mat(X)
a1=X.I
print("X的逆矩阵的值:")
print(a1,)
solve=np.dot(a1,y1)
solve=solve.reshape(3,1)
print("a,b,c的值:")
print(solve)
y2=np.dot(X,solve)
print("在x对应的方程中的值y2:")
print(y2)
print("y2与y1之间的差值:")
print(y2-y1.reshape(5,1))
plt.plot(x1,y1,'o')
plt.plot(x1,y2)
plt.xlim(min1-1,max1+1)
plt.ylim(min2-1,max2+1)
plt.show()