02 lnear model
问题:AttributeError: module ‘matplotlib’ has no attribute ‘plot’
原因:导入包时候是这样写的
import matplotlib as plt
应该改成
import matplotlib.pyplot as plt
课件代码:函数forward()中,有一个变量w。这个变量最终的值是从for循环中传入的。for循环中,使用了np.arrange。
range和np.arrange的区别
https://blog.csdn.net/yuanxiang01/article/details/78702123
range步长不能是小数,np.arrange可以是
# -*- codeing = utf-8 -*-
# @Time :2021/4/12 11:05
# @Author:sueong
# @File:test.py
# @Software:PyCharm
import numpy as np
import matplotlib.pyplot as plt
x_data=[1.0,2.0,3.0]#准备dataset
y_data=[2.0,4.0,6.0]
def forward(x):#前置函数 就是模型
return x*w
def loss(x,y):#loss
y_pre=forward(x)
return (y_pre-y)*(y_pre-y)
'''
>>>a = [1,2,3]
>>> b = [4,5,6]
>>> c = [4,5,6,7,8]
>>> zipped = zip(a,b) # 打包为元组的列表
[(1, 4), (2, 5), (3, 6)]
>>> zip(a,c) # 元素个数与最短的列表一致
[(1, 4), (2, 5), (3, 6)]
>>> zip(*zipped) # 与 zip 相反,*zipped 可理解为解压,返回二维矩阵式
[(1, 2, 3), (4, 5, 6)]
zip 可以把y=f(x) x和y对应起来是一组值
'''
w_list=[]
mse=[]
for w in np.arange(0.0,4.1,0.1):
print('w=',w)
l_sum=0
for x_val,y_val in zip(x_data,y_data):
y_pre_val=forward(x_val)
loss_val=loss(x_val,y_val)
l_sum+=loss_val
print('\t',x_val,y_val,y_pre_val,loss_val)
print('MSE=',l_sum/3)
w_list.append(w)
mse.append(l_sum/3)
plt.plot(w_list, mse)
plt.ylabel('Loss')
plt.xlabel('w')
plt.show()
'''
F:\anaconda\envs\pytorch\python.exe F:/pythonProject1/pytorch_liu/test.py
w= 0.0
1.0 2.0 0.0 4.0
2.0 4.0 0.0 16.0
3.0 6.0 0.0 36.0
MSE= 18.666666666666668
w= 0.1
1.0 2.0 0.1 3.61
2.0 4.0 0.2 14.44
3.0 6.0 0.30000000000000004 32.49
MSE= 16.846666666666668
w= 0.2
1.0 2.0 0.2 3.24
2.0 4.0 0.4 12.96
3.0 6.0 0.6000000000000001 29.160000000000004
MSE= 15.120000000000003
w= 0.30000000000000004
1.0 2.0 0.30000000000000004 2.8899999999999997
2.0 4.0 0.6000000000000001 11.559999999999999
3.0 6.0 0.9000000000000001 26.009999999999998
MSE= 13.486666666666665
w= 0.4
1.0 2.0 0.4 2.5600000000000005
2.0 4.0 0.8 10.240000000000002
3.0 6.0 1.2000000000000002 23.04
MSE= 11.946666666666667
w= 0.5
1.0 2.0 0.5 2.25
2.0 4.0 1.0 9.0
3.0 6.0 1.5 20.25
MSE= 10.5
w= 0.6000000000000001
1.0 2.0 0.6000000000000001 1.9599999999999997
2.0 4.0 1.2000000000000002 7.839999999999999
3.0 6.0 1.8000000000000003 17.639999999999993
MSE= 9.146666666666663
w= 0.7000000000000001
1.0 2.0 0.7000000000000001 1.6899999999999995
2.0 4.0 1.4000000000000001 6.759999999999998
3.0 6.0 2.1 15.209999999999999
MSE= 7.886666666666666
w= 0.8
1.0 2.0 0.8 1.44
2.0 4.0 1.6 5.76
3.0 6.0 2.4000000000000004 12.959999999999997
MSE= 6.719999999999999
w= 0.9
1.0 2.0 0.9 1.2100000000000002
2.0 4.0 1.8 4.840000000000001
3.0 6.0 2.7 10.889999999999999
MSE= 5.646666666666666
w= 1.0
1.0 2.0 1.0 1.0
2.0 4.0 2.0 4.0
3.0 6.0 3.0 9.0
MSE= 4.666666666666667
w= 1.1
1.0 2.0 1.1 0.8099999999999998
2.0 4.0 2.2 3.2399999999999993
3.0 6.0 3.3000000000000003 7.289999999999998
MSE= 3.779999999999999
w= 1.2000000000000002
1.0 2.0 1.2000000000000002 0.6399999999999997
2.0 4.0 2.4000000000000004 2.5599999999999987
3.0 6.0 3.6000000000000005 5.759999999999997
MSE= 2.986666666666665
w= 1.3
1.0 2.0 1.3 0.48999999999999994
2.0 4.0 2.6 1.9599999999999997
3.0 6.0 3.9000000000000004 4.409999999999998
MSE= 2.2866666666666657
w= 1.4000000000000001
1.0 2.0 1.4000000000000001 0.3599999999999998
2.0 4.0 2.8000000000000003 1.4399999999999993
3.0 6.0 4.2 3.2399999999999993
MSE= 1.6799999999999995
w= 1.5
1.0 2.0 1.5 0.25
2.0 4.0 3.0 1.0
3.0 6.0 4.5 2.25
MSE= 1.1666666666666667
w= 1.6
1.0 2.0 1.6 0.15999999999999992
2.0 4.0 3.2 0.6399999999999997
3.0 6.0 4.800000000000001 1.4399999999999984
MSE= 0.746666666666666
w= 1.7000000000000002
1.0 2.0 1.7000000000000002 0.0899999999999999
2.0 4.0 3.4000000000000004 0.3599999999999996
3.0 6.0 5.1000000000000005 0.809999999999999
MSE= 0.4199999999999995
w= 1.8
1.0 2.0 1.8 0.03999999999999998
2.0 4.0 3.6 0.15999999999999992
3.0 6.0 5.4 0.3599999999999996
MSE= 0.1866666666666665
w= 1.9000000000000001
1.0 2.0 1.9000000000000001 0.009999999999999974
2.0 4.0 3.8000000000000003 0.0399999999999999
3.0 6.0 5.7 0.0899999999999999
MSE= 0.046666666666666586
w= 2.0
1.0 2.0 2.0 0.0
2.0 4.0 4.0 0.0
3.0 6.0 6.0 0.0
MSE= 0.0
w= 2.1
1.0 2.0 2.1 0.010000000000000018
2.0 4.0 4.2 0.04000000000000007
3.0 6.0 6.300000000000001 0.09000000000000043
MSE= 0.046666666666666835
w= 2.2
1.0 2.0 2.2 0.04000000000000007
2.0 4.0 4.4 0.16000000000000028
3.0 6.0 6.6000000000000005 0.36000000000000065
MSE= 0.18666666666666698
w= 2.3000000000000003
1.0 2.0 2.3000000000000003 0.09000000000000016
2.0 4.0 4.6000000000000005 0.36000000000000065
3.0 6.0 6.9 0.8100000000000006
MSE= 0.42000000000000054
w= 2.4000000000000004
1.0 2.0 2.4000000000000004 0.16000000000000028
2.0 4.0 4.800000000000001 0.6400000000000011
3.0 6.0 7.200000000000001 1.4400000000000026
MSE= 0.7466666666666679
w= 2.5
1.0 2.0 2.5 0.25
2.0 4.0 5.0 1.0
3.0 6.0 7.5 2.25
MSE= 1.1666666666666667
w= 2.6
1.0 2.0 2.6 0.3600000000000001
2.0 4.0 5.2 1.4400000000000004
3.0 6.0 7.800000000000001 3.2400000000000024
MSE= 1.6800000000000008
w= 2.7
1.0 2.0 2.7 0.49000000000000027
2.0 4.0 5.4 1.960000000000001
3.0 6.0 8.100000000000001 4.410000000000006
MSE= 2.2866666666666693
w= 2.8000000000000003
1.0 2.0 2.8000000000000003 0.6400000000000005
2.0 4.0 5.6000000000000005 2.560000000000002
3.0 6.0 8.4 5.760000000000002
MSE= 2.986666666666668
w= 2.9000000000000004
1.0 2.0 2.9000000000000004 0.8100000000000006
2.0 4.0 5.800000000000001 3.2400000000000024
3.0 6.0 8.700000000000001 7.290000000000005
MSE= 3.780000000000003
w= 3.0
1.0 2.0 3.0 1.0
2.0 4.0 6.0 4.0
3.0 6.0 9.0 9.0
MSE= 4.666666666666667
w= 3.1
1.0 2.0 3.1 1.2100000000000002
2.0 4.0 6.2 4.840000000000001
3.0 6.0 9.3 10.890000000000004
MSE= 5.646666666666668
w= 3.2
1.0 2.0 3.2 1.4400000000000004
2.0 4.0 6.4 5.760000000000002
3.0 6.0 9.600000000000001 12.96000000000001
MSE= 6.720000000000003
w= 3.3000000000000003
1.0 2.0 3.3000000000000003 1.6900000000000006
2.0 4.0 6.6000000000000005 6.7600000000000025
3.0 6.0 9.9 15.210000000000003
MSE= 7.886666666666668
w= 3.4000000000000004
1.0 2.0 3.4000000000000004 1.960000000000001
2.0 4.0 6.800000000000001 7.840000000000004
3.0 6.0 10.200000000000001 17.640000000000008
MSE= 9.14666666666667
w= 3.5
1.0 2.0 3.5 2.25
2.0 4.0 7.0 9.0
3.0 6.0 10.5 20.25
MSE= 10.5
w= 3.6
1.0 2.0 3.6 2.5600000000000005
2.0 4.0 7.2 10.240000000000002
3.0 6.0 10.8 23.040000000000006
MSE= 11.94666666666667
w= 3.7
1.0 2.0 3.7 2.8900000000000006
2.0 4.0 7.4 11.560000000000002
3.0 6.0 11.100000000000001 26.010000000000016
MSE= 13.486666666666673
w= 3.8000000000000003
1.0 2.0 3.8000000000000003 3.240000000000001
2.0 4.0 7.6000000000000005 12.960000000000004
3.0 6.0 11.4 29.160000000000004
MSE= 15.120000000000005
w= 3.9000000000000004
1.0 2.0 3.9000000000000004 3.610000000000001
2.0 4.0 7.800000000000001 14.440000000000005
3.0 6.0 11.700000000000001 32.49000000000001
MSE= 16.84666666666667
w= 4.0
1.0 2.0 4.0 4.0
2.0 4.0 8.0 16.0
3.0 6.0 12.0 36.0
MSE= 18.666666666666668
Process finished with exit code 0
'''
作业:
3d画图
https://blog.csdn.net/u014636245/article/details/82799573
numpy.linspace使用详解:
https://blog.csdn.net/u014636245/article/details/82799573
https://blog.csdn.net/guduruyu/article/details/78050268
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
在指定的间隔内返回均匀间隔的数字。
返回num均匀分布的样本,在[start, stop]。
这个区间的端点可以任意的被排除在外。
import numpy as np
import matplotlib.pyplot as plt
x = np.array([0, 1, 2])
y = np.array([0, 1])
X, Y = np.meshgrid(x, y)
print(X)
print(Y)
plt.plot(X, Y,
color='red', # 全部点设置为红色
marker='.', # 点的形状为圆点
linestyle='') # 线型为空,也即点与点之间不用线连接
plt.grid(True)
plt.show()
'''
# 从输出的结果来看,两种方法生成的坐标矩阵一毛一样。
[[0 1 2]
[0 1 2]]
[[0 0 0]
[1 1 1]]
'''
3D表面形状的绘制例子
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Make data
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = 10 * np.outer(np.cos(u), np.sin(v))
y = 10 * np.outer(np.sin(u), np.sin(v))
z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))
# Plot the surface
ax.plot_surface(x, y, z, color='b')
plt.show()
# -*- codeing = utf-8 -*-
# @Time :2021/4/12 11:05
# @Author:sueong
# @File:test.py
# @Software:PyCharm
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
plt.show()
x_data=[1.0,2.0,3.0]#准备dataset
y_data=[5.0,8.0,11.0]
def forward(x):#前置函数 就是模型
return x*w+b
def loss(x,y):#loss
y_pre=forward(x)
return (y_pre-y)*(y_pre-y)
w=np.arange(0.0,4.1,0.1)
b=np.arange(0.0,4.1,0.1)
[w,b]=np.meshgrid(w,b)
mse=[]
l_sum=0
for x_val,y_val in zip(x_data,y_data):
y_pre_val=forward(x_val)
loss_val=loss(x_val,y_val)
l_sum+=loss_val
print('\t',x_val,y_val,y_pre_val,loss_val)
print('MSE=',l_sum/3)
mse.append(l_sum/3)
fig=plt.figure()
ax=Axes3D(fig)
ax.plot_surface(w,b,l_sum/3)
plt.show()
np.meshgrid()的用法
#coding:utf-8
import numpy as np
# 坐标向量
a = np.array([1,2,3])
# 坐标向量
b = np.array([7,8])
# 从坐标向量中返回坐标矩阵
# 返回list,有两个元素,第一个元素是X轴的取值,第二个元素是Y轴的取值
res = np.meshgrid(a,b)
#返回结果: [array([ [1,2,3] [1,2,3] ]), array([ [7,7,7] [8,8,8] ])]