线性回归代码实现(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图像处理的编程环境.灰度线性变换和灰度拉伸是对像素灰度值的变换操作,直方图是对像素灰度值的统计,直方图均衡是对 ...
随机推荐
- Android中的广播Broadcast详解
今天来看一下Android中的广播机制,我们知道广播Broadcast是Android中的四大组件之一,可见他的重要性了,当然它的用途也很大的,比如一些系统的广播:电量低.开机.锁屏等一些操作都会发送 ...
- 2018-2019-2-20175323 java实验三敏捷开发与XP实践
代码规范 安装alibaba插件 首先使用code栏里面的reformat code使代码的格式更加规范 再用编码规约扫描,alibaba把问题分为block/critical/major三个等级,出 ...
- params拦截器
1. params拦截器首先给action中的相关参数赋值,如id 2. prepare拦截器执行prepare方法,prepare方法中会根据参数,如id,去调用业务逻辑,设置model对象 ...
- LogInfoHelper
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- Spring源码由浅入深系列三 refresh
spring中的refresh是一个相当重要的方法.它完成IOC的第一个阶段,将xml中的bean转化为beanDefinition.详细说明如上图所示. 在上图中,创建obtainFreshBean ...
- HDU 2167 状压dp方格取数
题意:给出一个数表,规定取出一个数后周围的八个数都不可取,求可获得的最大数字和 思路:状态压缩dp,每一行的取数方法为状态,显然,由于取数规则的限制,可取的状态并不是 1<<size_co ...
- 【2018ACM/ICPC网络赛】沈阳赛区
这次网络赛没有打.生病了去医院了..尴尬.晚上回来才看了题补简单题. K Supreme Number 题目链接:https://nanti.jisuanke.com/t/31452 题意:输入一个 ...
- Error resolving template,template might not exist or might not be accessible by any of the configured Template Resolvers
template might not exist or might not be accessible by any of the configured Template Resolvers at o ...
- ES6 学习 -- 箭头函数(=>)
(1).只有一个参数且只有一句表达式语句的,函数表达式的花括号可以不写let test = a => a; // 只有一个参数a,这里的表达式相当于 "return a" ( ...
- BBS论坛 自定义form组件
二.自定义form组件 from django import forms from django.forms import widgets from app01 import models # 定制f ...