Cost Function的原理及实现(Python, matlab)
来源:互联网 发布:node schedule 每秒 编辑:程序博客网 时间:2024/06/11 10:00
成本函数(Cost Function)
Parameters:
以下为MATLAB实现方式:
function J = computeCost(X, y, theta) %COMPUTECOST Compute cost for linear regression % J = COMPUTECOST(X, y, theta) computes the cost of using theta as the parameter for linear regression to fit the data points in X and y m = length(y); % number of training examples J = 0; J = sum((X * theta - y) .^2) / 2 / m;end
以下为Python实现方式:
def computeCost(X, y, theta): """Cost Functions Parameters ---------- X : np.ndarray, like (49 * 2) y : np.ndarray, like (49 * 1) theta : np.ndarray, like (2 * 1) Returns ------- J : float, cost """ y = np.transpose(y) J = sum((np.dot(X, theta) - y.reshape(len(y),1)) **2) / 2.0 / len(y) # np.dot(A, B) 矩阵乘积,A * B 矩阵点乘 # pow(A, 2) 多次乘积,**n 点次方 return J
注意:np中点乘和矩阵乘积,点次方与多次乘积的区别,与MATLAB不同。
梯度下降法(Gradient Descent)
以下为MATLAB实现:
function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters) %GRADIENTDESCENT Performs gradient descent to learn theta % theta = GRADIENTDESCENT(X, y, theta, alpha, num_iters) updates theta by % taking num_iters gradient steps with learning rate alpha m = length(y); % number of training examples J_history = zeros(num_iters, 1); for iter = 1:num_iters theta = theta - X' * (X * theta - y) * (alpha / m); J_history(iter) = computeCost(X, y, theta); endend
以下为Python实现:
def gradientDescent(X, y, theta, alpha, num_iters): """Gradient Descent for (Multivariate) Linear Regression Parameters ---------- X : np.ndarray, like (49 * 2) y : np.ndarray, like (49 * 1) theta : np.ndarray, like (49 * 1) alpha : learning rate num_iters : number of iter Returns ------- tuple(J_history, theta) J_history : np.ndarray, like (num_iters, 1) theta : theta of convergence, like (2 * 1) """ J_history = np.zeros((num_iters, 1)) for n_iter in range(num_iters): theta = theta - np.dot(X.T, np.dot(X, theta) - y.reshape(len(y),1)) *alpha / len(y) J_history[n_iter, 0] = computeCost(X, y, theta) return J_history, theta
线性回归中的梯度下降公式
同理可得:
在(多元)线性回归中,由于在代码中直接使用矩阵进行运算,因此代码同上。
阅读全文
0 0
- Cost Function的原理及实现(Python, matlab)
- MATLAB实现cost function计算和gradience descent计算
- 神经网络的cost function
- PCA的原理及MATLAB实现
- Decision Tree的matlab实现及原理
- FFT的原理及matlab实现
- softmax原理及Matlab实现
- 白化原理及Matlab实现
- CNN原理及matlab实现
- Python的简单爬虫原理及实现
- 分水岭算法的原理、改进及matlab实现
- m序列生成器的原理与MATLAB及FPGA实现
- 卷积编码的原理与MATLAB及FPGA实现
- 标记符控制的分水岭算法原理及matlab实现
- MDS(多维尺度法)的原理及MATLAB实现
- MLP-多层感知机的原理及Matlab实现
- 区域生长算法原理及MATLAB实现
- LDA算法原理及matlab实现
- js对象和Object关系、以及js数据类型杂谈
- 小白成长日记(12)--(C语言)头插法建立单链表(输入输出)
- js脚本爬取网页数据案例解析
- 一篇文让你秒懂CDN
- ubuntu ssh以及samba服务器搭建
- Cost Function的原理及实现(Python, matlab)
- 阿里云上安装python 3.5
- Gson解析
- CentOS7下安装Python3及Pip3并保留Python2
- vptr与vtbl笔记----C++学习之路
- struts2的常见错误
- 三大IT定律
- Oracle 如何创建表空间
- [NOIP] [模拟] NOIP2017Day1 时间复杂度