代码模拟线性回归解决机器学习问题
from sklearn.datasets import load_boston
X,y = load_boston(return_X_y = True)
print(X.shape,y.shape)
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import torch.utils.data as Data
standardscaler = StandardScaler()
X = standardscaler.fit_transform(X)
train_x,test_x,train_y,test_y = train_test_split(X,y,test_size = 0.2)
print(train_x.shape,train_y.shape)
print(test_x.shape,test_y.shape)
(102, 13) (102,)
w = np.random.rand(1,13)
b = np.random.rand()
loss_all = []
for i in range(30):
pre_y = np.sum(w*train_x,axis=1 ) + b
loss = np.sum(pow(pre_y - train_y,2))/(2 * len(train_x))
loss_all.append(loss)
print('第{}次,损失是{}'.format(i+1,loss))
w = w - 0.1 * np.dot(train_x.T,pre_y - train_y)/len(train_x)
b = b - 0.1 * np.sum(pre_y - train_y)/len(train_x)
print("第{}次,系数是{}".format(i+1,w))
print("第{}次,常系数是{}".format(i+1,b))
"""
第30次,损失是13.401357529069488
第30次,系数是[[-0.84860307 0.56084003 -0.40875167 0.9102585 -0.4299894 3.79253055
0.18943605 -1.11629341 0.83174091 -0.66907291 -1.67139862 0.94988543
-3.01703923]]
第30次,常系数是21.628916738586867
"""
import matplotlib.pyplot as plt
plt.plot(loss_all,'r-')
plt.title("Train Loss")
plt.show()
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/eb63ae79bbcc9ed130be020b3932faa8.png#pic_center)
pre_y = np.sum(w * test_x,axis =1) + b
loss = np.sum(pow(test_y - pre_y,2))/len(test_x)
print("测试机上损失是:",loss)