batch gradient decent(线性回归批量梯度下降)
θj:=θj−α∂∂θjJ(θ)(1)
θ
j
:=
θ
j
−
α
∂
∂
θ
j
J
(
θ
)
(
1
)
1.吴恩达机器学习作业
2.代码参考黄海广博士
3.线性回归是监督学习中的回归问题,带labels。
4.第一次使用Numpy,仅做记录
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
def computeCost(X, y, theta):#代价函数,输入均为矩阵np.matrix(下同)
inner = np.power(((X * theta.T) - y), 2)
return np.sum(inner) / (2 * len(X))#代价函数定义
def gradientDescent(X,y,theta,alpha,iters): #输入的均为向量
parmeters_num = int(theta.ravel().shape[1]) #变量theta的个数
theta_record=np.matrix(np.zeros(parmeters_num))#用来记录采用梯度下降算法后后theta的数值
cost = np.zeros(iters)#记录在迭代过程中的代价函数
for i in range(iters):
temp = (X * theta.T)-y #见下
for j in range(parmeters_num):
Partial_value = np.multiply(temp, X[:,j])#np.multiply是让向量的对应位置相乘
theta_record[0,j] = theta[0,j] - alpha * np.sum(Partial_value) / (len(X))#即为公式(1)
theta = theta_record
cost[i] = computeCost.computeCost(X,y,theta)#迭代过程中的代价函数
return theta, cost
- 由于是线性回归,则有: hθ(x(i))=θ0+θ1x(i)1+...+θnx(i)n h θ ( x ( i ) ) = θ 0 + θ 1 x 1 ( i ) + . . . + θ n x n ( i ) (2)
- 代价函数为 J(θ)=12m∑mi=1(hθ(x(i))−y(i))2 J ( θ ) = 1 2 m ∑ i = 1 m ( h θ ( x ( i ) ) − y ( i ) ) 2
- 将 hθ(x) h θ ( x ) 带入假设函数 J(θ) J ( θ ) 中,则得到下面的逻辑
我们也可以使用scikit-learn的线性回归函数,采用scikit-learn的线性回归算法
- 类似sklearn.neighbors中的KNeighborsClassifier方法,对线性回归,不用设定k数值,直接用fit来训练模型:
from sklearn import linear_model
model = linear_model.LinearRegression()
model.fit(X, y) #X代表训练集数据,y代表这个训练集的标签
path = '***' #导入的csv文件路径
data = pd.read_csv(path, header=None, names=['Population', 'Profit'])
data.head()
data.insert(0, 'Ones', 1)#添加一行数据,对应为theta_0的数据
cols = data.shape[1]
X = data.iloc[:,0:cols-1]#X是所有行,去掉最后一列
y = data.iloc[:,cols-1:cols]#X是所有行,最后一列
X = np.matrix(X.values)
y = np.matrix(y.values)
- scikit-learn的预测表现
x = np.array(X[:, 1].A1)#X[:, 1].A1等同于np.asarray.ravel()---将数据降维,返回一个array
f = model.predict(X).flatten()#使用predict()得到预测值,和KNN一样,predict操作前要先进行fit操作
#作图
fig, ax = plt.subplots(figsize=(12,8))
ax.plot(x, f, 'r', label='Prediction')
ax.scatter(data.Population, data.Profit, label='Traning Data')
ax.legend(loc=2)
ax.set_xlabel('Population')
ax.set_ylabel('Profit')
ax.set_title('Predicted Profit vs. Population Size')
plt.show()