1 代价函数实现(cost function)

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 % Initialize some useful values
m = length(y); % number of training examples % You need to return the following variables correctly
J = 0; % ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta
% You should set J to the cost. predictions = X * theta;
sqrErrors = (predictions-y) .^ 2; J = 1/(2*m) * sum(sqrErrors); % ========================================================================= end

  1.1 详细解释

转化成了向量(矩阵)形式,如果用其他的语言,用循环应该可以实现

predictions = X * theta;        % 这里的大X是矩阵

  

sqrErrors = (predictions-y) .^ 2;

  

2 梯度下降

function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
%GRADIENTDESCENT Performs gradient descent to learn theta
% theta = GRADIENTDESENT(X, y, theta, alpha, num_iters) updates theta by
% taking num_iters gradient steps with learning rate alpha % Initialize some useful values
m = length(y); % number of training examples
J_history = zeros(num_iters, 1); for iter = 1:num_iters % ====================== YOUR CODE HERE ======================
% Instructions: Perform a single gradient step on the parameter vector
% theta.
%
% Hint: While debugging, it can be useful to print out the values
% of the cost function (computeCost) and gradient here.
%
theta_temp = theta;
for j = 1:size(X, 2)
theta_temp(j) = theta(j)-alpha*(1/m)*(X*theta - y)' * X(:, j);
end
theta = theta_temp;
% ============================================================ % Save the cost J in every iteration
J_history(iter) = computeCost(X, y, theta); end end

  2.1 解释

J_history = zeros(num_iters, 1);

theta_temp = theta;

  把theta存起来。保证同时更新

for j = 1:size(X, 2)
theta_temp(j) = theta(j)-alpha*(1/m)*(X*theta - y)' * X(:, j);
end

  更新theta    

(X*theta - y)' 是转置

(X*theta - y)' * X(:, j);

  这步是求和,相当于sum

J_history(iter) = computeCost(X, y, theta);

记录代价函数

因为随着迭代次数的增加,代价函数收敛。theta也就确定了。

代价函数的是降低,同时theta也在变化

到后面代价函数的值已经不变化了。到收敛了

线性回归代码实现(matlab)的更多相关文章

  1. 线性二次型调节器LQR/LQC算法解析及求解器代码(matlab)

    参考链接:http://120.52.51.14/stanford.edu/class/ee363/lectures/dlqr.pdf 本文参考讲义中的第20页PPT,根据Hamilton-Jacob ...

  2. Spark MLlib线性回归代码实现及结果展示

    线性回归(Linear Regression)是利用称为线性回归方程的最小平方函数对一个或多个自变量和因变量之间关系进行建模的一种回归分析. 这种函数是一个或多个称为回归系数的模型参数的线性组合.只有 ...

  3. 机器学习笔记(一)—— 线性回归问题与Matlab求解

    给你多组数据集,例如给你很多房子的面积.房子距离市中心的距离.房子的价格,然后再给你一组面积. 距离,让你预测房价.这类问题称为回归问题. 回归问题(Regression) 是给定多个自变量.一个因变 ...

  4. 简单的线性回归问题-TensorFlow+MATLAB·

    首先我们要试验的是 人体脂肪fat和年龄age以及体重weight之间的关系,我们的目标就是得到一个最优化的平面来表示三者之间的关系: TensorFlow的程序如下: import tensorfl ...

  5. 数学类网站、代码(Matlab & Python & R)

    0. math & code COME ON CODE ON | A blog about programming and more programming. 1. 中文 统计学Computa ...

  6. MATLAB Coder从MATLAB生成C/C++代码步骤

    MATLAB Coder可以从MATLAB代码生成独立的.可读性强.可移植的C/C++代码. 使用MATLAB Coder产生代码的3个步骤: 准备用于产生代码的MATLAB算法: 检查MATLAB代 ...

  7. 转 举例说明使用MATLAB Coder从MATLAB生成C/C++代码步骤

    MATLAB Coder可以从MATLAB代码生成独立的.可读性强.可移植的C/C++代码. http://www.mathworks.cn/products/matlab-coder/ 使用MATL ...

  8. 借助全新 MATLAB® 适配器代码示例读取英特尔® 实感™ 摄像头数据流

    下载源代码请访问原文地址:借助全新 MATLAB® 适配器代码示例读取英特尔® 实感™ 摄像头数据流 简介 该可下载代码示例简要介绍了如何使用英特尔® 实感™ SDK 和 MATLAB 的图像采集工具 ...

  9. 使用MATLAB对图像处理的几种方法(下)

     试验报告 一.试验原理: 图像点处理是图像处理系列的基础,主要用于让我们熟悉Matlab图像处理的编程环境.灰度线性变换和灰度拉伸是对像素灰度值的变换操作,直方图是对像素灰度值的统计,直方图均衡是对 ...

随机推荐

  1. DELPHI 数据库操作

    DELPHI 把数据库中的数据转换成XML格式 function ReplaceString(AString: string): string; begin Result := StringRepla ...

  2. NOIp2018集训test-9-8(pm) (联考一day2)

    把T1题读错了,想了一个多小时发现不可做.然后打了t2,常数不优秀.然后去打t3,lct,结果打挂爆0了. 然后今天就爆炸了. 如果这是noip我今年就可以直接回去学常规了.学常规多好,多开心. 今天 ...

  3. flutter WebView flutter js 互调

    通过webview 进行flutter 和js 的互调,页面可以放在其他网站 也可以放在本地,我这里放在本地进行测试. 1.引入webview_flutter: ^0.3.5+2 这个是官网推荐用的, ...

  4. 在fedora下面安装ftp服务器

    Fedora版本:Fedora 12 1. 安装vsftp #yum install vsftpd 一路yes,最后提示安装成功. 2. 配置vsftpd.conf. #vi /etc/vsftpd/ ...

  5. Linux服务器上监控网络带宽的18个常用命令nload, iftop,iptraf-ng, nethogs, vnstat. nagios,运用Ntop监控网络流量

    Linux服务器上监控网络带宽的18个常用命令 本文介绍了一些可以用来监控网络使用情况的Linux命令行工具.这些工具可以监控通过网络接口传输的数据,并测量目前哪些数据所传输的速度.入站流量和出站流量 ...

  6. CodeForces 1166D Cute Sequences

    题目链接:http://codeforces.com/problemset/problem/1166/D 题目大意 给定序列的第一个元素 a 和最后一个元素 b 还有一个限制 m,请构造一个序列,序列 ...

  7. 20140312 Excel表格画折现图次坐标轴

    1.struct 是8字节对齐 2.char * a, sizeof(a)=4; char a[]="abcdefg";sizeof(a)=8; 3.内存对其齐

  8. 简单的spring--mvc整合

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  9. CSS3新属性之---flex box布局实例

    flex box布局实例 flex的强大之处在于不管什么布局,几行命令即可实现 /*本节模板div元素(代表骰子的一个面)是Flex容器,span元素(代表一个点)是Flex项目.如果有多个项目,就要 ...

  10. USACO2007 捕牛记 /// queue+桶 oj1503

    题目大意: John和牛都站在一条直线上,开始时John位于坐标点N上( 0 ≤ N ≤ 100,000 ),牛位于坐标点K上( 0 ≤ K ≤ 100,000 ). John有两种行动方式:步行和瞬 ...