线性回归代码实现(matlab)
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)的更多相关文章
- 线性二次型调节器LQR/LQC算法解析及求解器代码(matlab)
参考链接:http://120.52.51.14/stanford.edu/class/ee363/lectures/dlqr.pdf 本文参考讲义中的第20页PPT,根据Hamilton-Jacob ...
- Spark MLlib线性回归代码实现及结果展示
线性回归(Linear Regression)是利用称为线性回归方程的最小平方函数对一个或多个自变量和因变量之间关系进行建模的一种回归分析. 这种函数是一个或多个称为回归系数的模型参数的线性组合.只有 ...
- 机器学习笔记(一)—— 线性回归问题与Matlab求解
给你多组数据集,例如给你很多房子的面积.房子距离市中心的距离.房子的价格,然后再给你一组面积. 距离,让你预测房价.这类问题称为回归问题. 回归问题(Regression) 是给定多个自变量.一个因变 ...
- 简单的线性回归问题-TensorFlow+MATLAB·
首先我们要试验的是 人体脂肪fat和年龄age以及体重weight之间的关系,我们的目标就是得到一个最优化的平面来表示三者之间的关系: TensorFlow的程序如下: import tensorfl ...
- 数学类网站、代码(Matlab & Python & R)
0. math & code COME ON CODE ON | A blog about programming and more programming. 1. 中文 统计学Computa ...
- MATLAB Coder从MATLAB生成C/C++代码步骤
MATLAB Coder可以从MATLAB代码生成独立的.可读性强.可移植的C/C++代码. 使用MATLAB Coder产生代码的3个步骤: 准备用于产生代码的MATLAB算法: 检查MATLAB代 ...
- 转 举例说明使用MATLAB Coder从MATLAB生成C/C++代码步骤
MATLAB Coder可以从MATLAB代码生成独立的.可读性强.可移植的C/C++代码. http://www.mathworks.cn/products/matlab-coder/ 使用MATL ...
- 借助全新 MATLAB® 适配器代码示例读取英特尔® 实感™ 摄像头数据流
下载源代码请访问原文地址:借助全新 MATLAB® 适配器代码示例读取英特尔® 实感™ 摄像头数据流 简介 该可下载代码示例简要介绍了如何使用英特尔® 实感™ SDK 和 MATLAB 的图像采集工具 ...
- 使用MATLAB对图像处理的几种方法(下)
试验报告 一.试验原理: 图像点处理是图像处理系列的基础,主要用于让我们熟悉Matlab图像处理的编程环境.灰度线性变换和灰度拉伸是对像素灰度值的变换操作,直方图是对像素灰度值的统计,直方图均衡是对 ...
随机推荐
- 反演+分块套分块——bzoj2154
题解都在论文里了 #include<bits/stdc++.h> using namespace std; #define maxn 10000005 #define ll long lo ...
- fasttext源码剖析
fasttext源码剖析 目的:记录结合多方资料以及个人理解的剖析代码: https://heleifz.github.io/14732610572844.html http://www.cnbl ...
- mysql删除字段为null的数据
delete FROM main_bussiness_cost1 where date is null; 不能用 date = null:
- Mybatis使用Dao代码方式CURD
Mybatis 使用Dao代码方式进行增.删.改.查. 1.Maven的pom.xml <project xmlns="http://maven.apache.org/POM/4.0. ...
- 再次封装ajax函数,统一入口
根据API写网页的时候,每个页面都需要ajax请求,每次都写一大堆请求,配置什么的太麻烦,于是打算封装一个ajax函数,统一调用: 开始时是使用return返回ajax,如下: function cr ...
- item字母问题
解决方法:复写toString方法 @Override public String toString() { return this.getBookTypeName(); } 将对象的toString ...
- 2019-6-23-win10-uwp-开发-CSDN-访问量统计-源代码
title author date CreateTime categories win10 uwp 开发 CSDN 访问量统计 源代码 lindexi 2019-6-23 11:2:1 +0800 2 ...
- mac下xampp+vscode进行php程序调试
最近折腾公司的官网,是 php 做的,搭建调试环境做个记录,我用的是 mac 机. 1.下载最新的xampp,我的版本是XAMPP for OS X 5.6.31: 2.找到 php.ini,/App ...
- Oracle18C安装后首次创建数据库并用sql developer 创建连接和用户
注意: SQL Developer 不能用于创建Oracle数据库,只能用来连接已经创建的数据库,数据库的建立要通过Database Configuration Assistant(DBCA)来完成. ...
- [JZOJ3235] 数字八
题目 题目大意 给你一个二维的图,其中.代表完好,*代表有缺陷. 现在要在图上刻一个数字\(8\),满足: 由两个矩形组成. 每个矩形中必须有空隙在内部,也就是说,至少为\(3*3\)的矩形. 上矩形 ...