刘二第线性模型 课后作业

改写

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# 数据集
x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]

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


def loss(x, y):
    """
    返回的是预测值与真实值得平方差
    :param x: 特征
    :param y: 预测值
    :return: 误差
    """
    y_pred = forward(x)
    return (y_pred - y) ** 2


# 创建列表用于存放w值;损失值;偏执值
# w_list = []
# mes_list = []
# b_list = []
W = np.arange(0.0, 4.1, 0.1)
B = np.arange(0.0, 4.1, 0.1)
[w, b] = np.meshgrid(W, B)
# 前面的w竖向展开,后面的b横向展开
# 此时w=[[0.  0.1 0.2 ... 3.8 3.9 4. ]
#  [0.  0.1 0.2 ... 3.8 3.9 4. ]
#  [0.  0.1 0.2 ... 3.8 3.9 4. ]
#  ...
#  [0.  0.1 0.2 ... 3.8 3.9 4. ]
#  [0.  0.1 0.2 ... 3.8 3.9 4. ]
#  [0.  0.1 0.2 ... 3.8 3.9 4. ]]
# b=[[0.  0.  0.  ... 0.  0.  0. ]
#  [0.1 0.1 0.1 ... 0.1 0.1 0.1]
#  [0.2 0.2 0.2 ... 0.2 0.2 0.2]
#  ...
#  [3.8 3.8 3.8 ... 3.8 3.8 3.8]
#  [3.9 3.9 3.9 ... 3.9 3.9 3.9]
#  [4.  4.  4.  ... 4.  4.  4. ]]


l_sum = 0
for x_val, y_val in zip(x_data, y_data):
     #zip()函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这#些元组组成的列表。                                               
    y_pred_val = forward(x_val)

#     此时的W和B
#     w=[[0.  0.1 0.2 ... 3.8 3.9 4. ]
#  [0.  0.1 0.2 ... 3.8 3.9 4. ]
#  [0.  0.1 0.2 ... 3.8 3.9 4. ]
#  ...
#  [0.  0.1 0.2 ... 3.8 3.9 4. ]
#  [0.  0.1 0.2 ... 3.8 3.9 4. ]
#  [0.  0.1 0.2 ... 3.8 3.9 4. ]]
# b= [[0.  0.  0.  ... 0.  0.  0. ]
#  [0.1 0.1 0.1 ... 0.1 0.1 0.1]
#  [0.2 0.2 0.2 ... 0.2 0.2 0.2]
#  ...
#  [3.8 3.8 3.8 ... 3.8 3.8 3.8]
#  [3.9 3.9 3.9 ... 3.9 3.9 3.9]
#  [4.  4.  4.  ... 4.  4.  4. ]]
# 而经过此计算就是把X的值矩阵对应位置的w和b带入计算,
# 就像是[[w=0.b=0,  w=0.1b=0,w= 0.2b=0 ... w=3.8b=0, w=3.9b=0, w=4.b=0]
#           ...............
#           ...............]
# # 就相当于w和b的矩阵对应叠加。
# 而每一个循环中的y_pred_val,以第一个循环中的为例,
# y_pred_val= [[ 0.   0.2  0.4 ...  7.6  7.8  8. ]
#  [ 0.1  0.3  0.5 ...  7.7  7.9  8.1]
#  [ 0.2  0.4  0.6 ...  7.8  8.   8.2]
#  ...
#  [ 3.8  4.   4.2 ... 11.4 11.6 11.8]
#  [ 3.9  4.1  4.3 ... 11.5 11.7 11.9]
#  [ 4.   4.2  4.4 ... 11.6 11.8 12. ]]
# 每一个数字都是将x带入上面对应叠加的w和b,也同样是一个矩阵
# # 就像是[[w=0.b=0,  w=0.1b=0,w= 0.2b=0 ... w=3.8b=0, w=3.9b=0, w=4.b=0]]
# [[1*0+0,1*0.1+0,1*0+0.2.....]
# ...............
#           ...............]
# loss_val的值就是对y_pred_val矩阵中每个位置的数求损失值(在这个程序中就是差值的平方)再放回原来的位置,
# 形成本次循环的损失矩阵,例如
#     [[4.    3.61  3.24...  3.24  3.61  4.]
# [3.61  3.24  2.89...  3.61  4.    4.41]
# [3.24  2.89  2.56...  4.    4.41  4.84]
# ...
# [3.24  3.61  4.... 31.36 32.49 33.64]
# [3.61  4.    4.41... 32.49 33.64 34.81]
# [4.    4.41  4.84... 33.64 34.81 36.]]
#     loss_val = [[1.600e+01 1.444e+01 1.296e+01... 1.296e+01 1.444e+01 1.600e+01]
# [1.521e+01 1.369e+01 1.225e+01... 1.369e+01 1.521e+01 1.681e+01]
# [1.444e+01 1.296e+01 1.156e+01... 1.444e+01 1.600e+01 1.764e+01]
# ...
# [4.000e-02 0.000e+00 4.000e-02... 5.476e+01 5.776e+01 6.084e+01]
# [1.000e-02 1.000e-02 9.000e-02... 5.625e+01 5.929e+01 6.241e+01]
# [0.000e+00 4.000e-02 1.600e-01... 5.776e+01 6.084e+01 6.400e+01]]
# loss_val= [[ 36.    32.49  29.16 ...  29.16  32.49  36.  ]
#  [ 34.81  31.36  28.09 ...  30.25  33.64  37.21]
#  [ 33.64  30.25  27.04 ...  31.36  34.81  38.44]
#  ...
#  [  4.84   3.61   2.56 ...  84.64  90.25  96.04]
#  [  4.41   3.24   2.25 ...  86.49  92.16  98.01]
#  [  4.     2.89   1.96 ...  88.36  94.09 100.  ]]
# 最后的l_sum +就是将三个损失矩阵对应位置相加,
# 例如
# 第一个数就是三个损失值之和4+16+36=56,只不过画图象的时候需要将该数除3来求平均值
# l_sum= [[ 56.    50.54  45.36 ...  45.36  50.54  56.  ]
#  [ 53.63  48.29  43.23 ...  47.55  52.85  58.43]
#  [ 51.32  46.1   41.16 ...  49.8   55.22  60.92]
#  ...
#  [  8.12   7.22   6.6  ... 170.76 180.5  190.52]
#  [  8.03   7.25   6.75 ... 175.23 185.09 195.23]
#  [  8.     7.34   6.96 ... 179.76 189.74 200.  ]]
    print('y_pred_val',y_pred_val)
    loss_val = loss(x_val, y_val)
    print('loss_val=',loss_val)
    l_sum += loss_val
    # print(x_val, y_val, loss_val)
    # b_list.append(b)
# print('MSE:', l_sum / 3)
# w_list.append(w)
# mes_list.append(l_sum / 3)
print('mes=',mes_list)
print('大W',W)
print('大B',B)
print('w=',w)
print('b=',b)
print('l_sum=',l_sum)

# 画图
fig = plt.figure()
ax = Axes3D(fig)
ax = fig.add_subplot(111, projection='3d')
surf = ax.plot_surface(w, b, l_sum / 3)
plt.show()

你可能感兴趣的:(python,算法,开发语言)