梯度下降法在多元线性中的应用
注:本文基于已知梯度下降法的运行原理的基础上并基于吴恩达第一次的作业进行对梯度下降法的编程
①数据说明
②关于损失函数法(Loss Function)的编程
③关于梯度下降法的编程(采用矩阵以及向量的形式)
④关于数据的预处理
⑤多元线性回归的实现
①数据说明
本文采用吴恩达第一次作业集即ex1data2的数据进行介绍,数据如下:
链接:https://pan.baidu.com/s/14VEdskXsWi5p8k2s4Houlw
提取码:d2j3
②关于损失函数
损失函数的定义:在回归模型中,常常采用均方误差作为CostFunction(LossFunction):
python代码实现如下:
def Ccost(X,y,theta):##计算cost function函数
J=np.power(((X*theta.T)-y),2)##注意X要与theta.T满足矩阵乘法的要求
return np.sum(J) / (2 * len(X))
③关于梯度下降法
一元梯度下降法的公式:
更一般将其推广到多元的情况下的梯度下降法公式为:
python实现如下:
def gradientdescent(X,y,theta,alpha,cishu):##梯度下降法的实施过程
temp = np.matrix(np.zeros(theta.shape))##创建一个临时存放参数的地方
cost = np.zeros(cishu)##代价函数矩阵
parameters = int(theta.ravel().shape[1])
for i in range(cishu):
error = (X * theta.T) - y
for j in range(parameters):
term = np.multiply(error, X[:,j])##凑出想要减去的步长
temp[0,j] = theta[0,j] - ((alpha / len(X)) * np.sum(term))##梯度公式
theta = temp##更新参数
cost[i] = Ccost(X, y, theta)##调用上方编辑的函数
return theta,cost##返回参数theta以及在不同参数下的LossFunction的值
④数据的预处理(标准化)
在机器学习领域中数据标准化又称数据的特征缩放:
标准化公式为:
⑤多元线性回归实现
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn import preprocessing
data = pd.read_csv("ex1data2.txt", header=None, names=['area','number', 'Profit'])
data.head()
##————————————————————————————特征缩放(缩小量纲带来的影响)采取标准化进行缩小量纲
data = (data - data.mean()) / data.std()
print(data)
##_______________________________
def Ccost(X,y,theta):##计算cost function函数
J=np.power(((X*theta.T)-y),2)##注意X要与theta.T满足矩阵乘法的要求
return np.sum(J) / (2 * len(X))
##——————————————————————————梯度下降
def gradientdescent(X,y,theta,alpha,cishu):##梯度下降法的实施过程
temp = np.matrix(np.zeros(theta.shape))##创建一个临时存放参数的地方
cost = np.zeros(cishu)##代价函数矩阵
parameters = int(theta.ravel().shape[1])
for i in range(cishu):
error = (X * theta.T) - y
for j in range(parameters):
term = np.multiply(error, X[:,j])##凑出想要减去的步长
temp[0,j] = theta[0,j] - ((alpha / len(X)) * np.sum(term))##梯度公式
theta = temp##更新参数
cost[i] = Ccost(X, y, theta)
return theta,cost##返回参数theta以及在不同参数下的LossFunction的值
##————————————————————————准备数据
data.insert(0, 'x0', 1)
print(data.head())##准备数据环节结束
##——————————————————————进行梯度下降法的参数以及数据准备
cols = data.shape[1]
print(cols)
X = data.iloc[:,0:cols-1]
y = data.iloc[:,cols-1:cols]
cishu=1000
alpha=0.01##设置学习率
##_________________________矩阵化(梯度下降法的程序编程是定义在矩阵乘法的基础上)
X = np.matrix(X.values)
y = np.matrix(y.values)
theta = np.matrix(np.array([0,0,0]))##注意theta也需要矩阵化
##————————————————————
g,cost=gradientdescent(X, y, theta, alpha, cishu)
print(g)##输出梯度下降法下的参数
print(Ccost(X,y,g))##输出该参数下的LossFunction的值