多元线性回归
练习1还包括一个房屋价格数据集,其中有2个变量(房子的大小,卧室的数量)和目标(房子的价格)。 我们使用我们已经应用的技术来分析数据集:
导入数据集并查看:
path = 'ex1data2.txt'
data2 = pd.read_csv(path, header=None, names=['Size', 'Bedrooms', 'Price'])
data2.head()
把数据进行归一化:
data2 = (data2 - data2.mean()) / data2.std()
data2.head()
处理数据得到X,y与theta:
data2.insert(0,'one',1)
cols = data2.shape[1]
X2 = data2.iloc[:,0:cols-1].values
y2 = data2.iloc[:,cols-1:cols].values
theta = np.zeros((cols-1,1))
theta
损失函数的计算:
def computeCost(X2, y2, theta):
temp = np.dot(X2, theta) - y2
cost = np.sum(np.power(temp, 2))/(2*len(X2))
return cost
梯度下降法:
def gradientDescent(X2, y2, theta, alpha, iters, m=len(X2)):
J = []
for i in range(iters):
temp = theta - (alpha/m) * np.dot((np.dot(X2, theta)-y2).T, X2[:]).T
theta = temp
J.append(computeCost(X2, y2, theta))
return theta, J
迭代1000次所得的theta:
alpha = 0.01
iters = 1000
final_theta ,cost2 = gradientDescent(X2, y2, theta, alpha, iters,m =len(X2))
final_theta
array([[-9.77586806e-17],
[ 8.78503652e-01],
[-4.69166570e-02]])
计算最终损失值:
computeCost(X2,y2,final_theta)
0.1307033696077189
cost的可视化:
plt.figure()
list = [i for i in range(1000)]
plt.plot(list,cost2)
plt.xlabel('Iterations')
plt.ylabel('cost')
plt.title('Error vs. Training Epoch')
plt.show()
通过正规方程来解:
# 正规方程
def normalEqn(X, y):
theta = np.linalg.inv(X.T@X)@X.T@y#X.T@X等价于X.T.dot(X)
return theta
t2 = normalEqn(X2,y2)
t2
array([[-8.67361738e-17],
[ 8.84765988e-01],
[-5.31788197e-02]])
可以看的这两种方法所得的theta还是有所不同。
正规方程推导: