机器学习-线性回归穷举法

线性回归(w和b)-穷举法

1. 导包,numpy和画图matplotlib

import numpy as np
import matplotlib.pyplot as plt

2. 创造数据

简单起见,创造三个样本。

data_x = [1.0, 2.0, 3.0]
data_y = [6.0, 9.0, 12.0]

3. 建立线性回归模型

def forward(x):
    return x * w + b

4. 创建损失函数

def loss(x, y):
    y_pred = forward(x)
    return (y_pred - y) * (y_pred - y)

5. 训练

选取了0-6范围的w和b值

w_list = []
loss_list = []
b_list = []
for w in np.arange(0.0, 6.1, 0.1):
    for b in np.arange(0.0, 6.1, 0.1):
        loss_sum = 0
        for x_train, y_train in zip(data_x, data_y):
            print('w = ', w)
            y_pred_test = forward(x_train)
            print('y = ', y_pred_test)
            loss1 = loss(x_train, y_train)
            loss_sum += loss1
        print('MSE = ', loss_sum / 3)
        loss_list.append(loss_sum)

    w_list.append(w)

6. 画图

import matplotlib.pyplot as plt
fig = plt.figure()
from mpl_toolkits.mplot3d import Axes3D

ax = Axes3D(fig)
x = w_list
y = np.arange(0.0, 6.1, 0.1)
z = np.reshape(loss_list, (x.__len__(), y.__len__()))
x, y = np.meshgrid(x, y)
surf = ax.plot_surface(x, y, z, cmap = plt.get_cmap('rainbow'))
plt.xlabel('w')
plt.ylabel('b')
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
  1. 结果
    机器学习-线性回归穷举法_第1张图片

你可能感兴趣的:(实验报告,python,机器学习,开发语言)