import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
path = 'ex1data2.txt'
data2 = pd.read_csv(path,names=['Size','Bedroom','Price'])
#特征归一化(解决特征分布范围不接近的问题)
#data2 = (data2 - data2.min()) / (data2.max() - data2.min())
#data2.head()
data2 = (data2 - data2.mean()) / data2.std()
data2.head()
Size | Bedroom | Price | |
---|---|---|---|
0 | 0.130010 | -0.223675 | 0.475747 |
1 | -0.504190 | -0.223675 | -0.084074 |
2 | 0.502476 | -0.223675 | 0.228626 |
3 | -0.735723 | -1.537767 | -0.867025 |
4 | 1.257476 | 1.090417 | 1.595389 |
为什么要特征归一化及特征归一化常用方法 参考资料:
https://www.cnblogs.com/ooon/p/4947347.html
https://www.cnblogs.com/sddai/p/6250094.html
#计算代价函数
def compute_cost(x,y,theta):
inner = np.power(np.dot(x,theta.T) - y, 2)#power求次方,dot矩阵相乘
return sum(inner)/(2*len(x)) #???
#梯度下降算法
def gradientDescent(x, y, theta, alpha, epoch = 1000):
temp = np.array(np.zeros(theta.shape)) #初始化参数矩阵
#parameters = int(theta.shape[1]) #参数数量
cost = np.zeros(epoch) #初始化一个ndarray,包含每次更新后的代价
m = x.shape[0] #样本数目
for i in range(epoch):
temp = theta - (alpha/m)*(x.dot(theta.T)-y).T.dot(x)
theta = temp #同步更新theta
cost[i] = compute_cost(x,y,theta)
return theta,cost
alpha = 0.27
epoch = 100
# add ones column
# set X (training data) and y (target variable)
data2.insert(0, 'Ones', 1)
cols = data2.shape[1]
X2 = data2.iloc[:,0:cols-1]
y2 = data2.iloc[:,cols-1:cols]
# convert to matrices and initialize theta
X2 = np.matrix(X2.values)
y2 = np.matrix(y2.values)
theta2 = np.matrix(np.array([0,0,0]))
# perform linear regression on the data set
g2, cost2 = gradientDescent(X2, y2, theta2, alpha, epoch)
# get the cost (error) of the model
compute_cost(X2, y2, g2), g2
fig, ax = plt.subplots(figsize=(12,8))
ax.plot(np.arange(epoch), cost2, 'r')
ax.set_xlabel('Iterations')
ax.set_ylabel('Cost')
ax.set_title('Error vs. Training Epoch')
plt.show()
参考学习博客:
https://blog.csdn.net/Mrs_Ivory/article/details/91449797
数据:
2104,3,399900
1600,3,329900
2400,3,369000
1416,2,232000
3000,4,539900
1985,4,299900
1534,3,314900
1427,3,198999
1380,3,212000
1494,3,242500
1940,4,239999
2000,3,347000
1890,3,329999
4478,5,699900
1268,3,259900
2300,4,449900
1320,2,299900
1236,3,199900
2609,4,499998
3031,4,599000
1767,3,252900
1888,2,255000
1604,3,242900
1962,4,259900
3890,3,573900
1100,3,249900
1458,3,464500
2526,3,469000
2200,3,475000
2637,3,299900
1839,2,349900
1000,1,169900
2040,4,314900
3137,3,579900
1811,4,285900
1437,3,249900
1239,3,229900
2132,4,345000
4215,4,549000
2162,4,287000
1664,2,368500
2238,3,329900
2567,4,314000
1200,3,299000
852,2,179900
1852,4,299900
1203,3,239500