import numpy as np
from matplotlib import pyplot as plt
import sklearn.datasets
x,y = sklearn.datasets.make_regression(n_features=1,noise=5,random_state=2020)
plt.scatter(x,y)
plt.show()
a = np.linspace(1,2,5).reshape(-1,1)
b = np.array([350,380,410,430,480])
x_1 = np.r_[x,a]
y_1 = np.r_[y,b]
plt.scatter(x_1,y_1)
plt.show()
class normal():
def __init__(self):
pass
def fit(self,x,y):
m=x.shape[0]
X = np.concatenate((np.ones((m,1)),x),axis=1)
xMat=np.mat(X)
yMat =np.mat(y.reshape(-1,1))
xTx=xMat.T*xMat
ws=xTx.I*xMat.T*yMat
return ws
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
clf1 =normal()
w1 = clf1.fit(x,y)
y_pred = x * w1[1] + w1[0]
w2 = clf1.fit(x_1,y_1)
y_1_pred = x_1 * w2[1] + w2[0]
print('原始样本拟合参数:\n',w1)
print('\n')
print('新样本拟合参数:\n',w2)
ax1= plt.subplot()
ax1.scatter(x_1,y_1,label='样本分布')
ax1.plot(x,y_pred,c='y',label='原始样本拟合')
ax1.plot(x_1,y_1_pred,c='r',label='新样本拟合')
ax1.legend(prop = {'size':15})
plt.show()