资源简介
多线性回归的MatLab例子(带有数据与代码,可运行显示图形)
代码片段和文件信息
% Exercise 3 -- Multivariate Linear Regression
clear all; close all; clc
x = load(‘ex3x.dat‘);
y = load(‘ex3y.dat‘);
m = length(y);
% Add intercept term to x
x = [ones(m 1) x];
% Save a copy of the unscaled features for later
x_unscaled = x;
% Scale features and set them to zero mean
mu = mean(x);
sigma = std(x);
x(:2) = (x(:2) - mu(2))./ sigma(2);
x(:3) = (x(:3) - mu(3))./ sigma(3);
% Prepare for plotting
figure;
% plot each alpha‘s data points in a different style
% braces indicate a cell not just a regular array.
plotstyle = {‘b‘ ‘r‘ ‘g‘ ‘k‘ ‘b--‘ ‘r--‘};
% Gradient Descent
alpha = [0.01 0.03 0.1 0.3 1 1.3];
MAX_ITR = 100;
% this will contain my final values of theta
% after I‘ve found the best learning rate
theta_grad_descent = zeros(size(x(1:)));
for i = 1:length(alpha)
theta = zeros(size(x(1:)))‘; % initialize fitting parameters
J = zeros(MAX_ITR 1);
for num_iterations = 1:MAX_ITR
% Calculate the J term
J(num_iterations) = (0.5/m) .* (x * theta - y)‘ * (x * theta - y);
% The gradient
grad = (1/m) .* x‘ * ((x * theta) - y);
% Here is the actual update
theta = theta - alpha(i) .* grad;
end
% Now plot the first 50 J terms
plot(0:49 J(1:50) char(plotstyle(i)) ‘LineWidth‘ 2)
hold on
% After some trial and error I find alpha=1
% is the best learning rate and converges
% before the 100th iteration
%
% so I save the theta for alpha=1 as the result of
% gradient descent
if (alpha(i) == 1)
theta_grad_descent = theta;
end
end
legend(‘0.01‘‘0.03‘‘0.1‘ ‘0.3‘ ‘1‘ ‘1.3‘)
xlabel(‘Number of iterations‘)
ylabel(‘Cost J‘)
% force Matlab to display more than 4 decimal places
% formatting persists for rest of this session
format long
% Display gradient descent‘s result
theta_grad_descent
% Estimate the price of a 1650 sq-ft 3 br house
price_grad_desc = dot(theta_grad_descent [1 (1650 - mu(2))/sigma(2)...
(3 - mu(3))/sigma(3)])
% Calculate the parameters from the normal equation
theta_normal = (x_unscaled‘ * x_unscaled)\x_unscaled‘ * y
%Estimate the house price again
price_normal = dot(theta_normal [1 1650 3])
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 2248 2018-03-26 13:51 NG-Exercise3 Multivariate Linear Regression\ex3.m
文件 1551 2010-10-14 23:26 NG-Exercise3 Multivariate Linear Regression\ex3x.dat
文件 799 2010-10-14 23:26 NG-Exercise3 Multivariate Linear Regression\ex3y.dat
相关资源
- 串行级联cpm系统MATLAB仿真
- matlab_OFDM调制解调(来自剑桥大学)
- Matlab路面裂缝识别69319
- 高灵敏度GPS接收机MATLAB仿真,附捕获
- 基于MATLAB的质点弹道计算与外弹道优
- 阵列天线的matlab仿真
- MATLAB 经典程序源代码大全
- MATLAB小波软阈值去噪代码33473
- 天线阵的波束形成在MATLAB仿真程序及
- 非线性SVM算法-matlab实现
- 《MATLAB 智能算法超级学习手册》-程序
- 组合导航matlab程序
- 读取txt文件内容matlab代码实现
- Matlab实现基于相关的模板匹配程序
- matlab优化工具箱讲解
- 基于MATLAB的快速傅里叶变换
- 光纤传输中的分布傅立叶算法matlab实
- 基于matlab的图像处理源程序
- matlab 椭圆拟合程序
- 算术编码解码matlab源代码
- optical_flow 光流法 matlab 实现程序
- 引导图像滤波器 Matlab实现
- 分形几何中一些经典图形的Matlab画法
- OFDM系统MATLAB仿真代码
- SVM工具箱(matlab中运行)
- 图像小波变换MatLab源代码
- LU分解的MATLAB实现
- 冈萨雷斯数字图像处理matlab版(第三
- 替代数据法的matlab程序
- 用matlab实现的多站定位系统性能仿真
川公网安备 51152502000135号
评论
共有 条评论