本文用Numpy生成测试数据,并且用线性回归来求解预测函数。通过Matplotlib来可视化数据和代价函数,这样我们可以对代价函数有更直观的理解。
也叫Normal Function
import numpy as np
import math
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
"""Prepare Data"""
x = 5+ 5*np.random.randn(100)
y = 3*x + 4 + 3*np.random.randn(100)
'''Show the data'''
plt.title("Sample Distribution")
plt.plot(np.arange(-20,20), 3*np.arange(-20,20)+4, "r")
plt.plot(x,y,"bo")
plt.show()
"""Show Cost Function"""
wa=np.arange(-7,14)
cost=np.zeros(21)
for i in range(-7,18):
cost[i]=((wa[i]*x+4-y)**2).sum()/len(x)
plt.title("Cost Function for LSM for w only")
plt.plot(wa, cost)
plt.show()
wix = np.arange(-7,13,0.1);
wiy = np.arange(-6,14,0.1);
Wi,Bi = np.meshgrid(wix,wiy)
cost2d = np.zeros([200,200])
for i in rang