斯坦福机器学习课程 Exercise 习题三
2024-09-01 15:44:54
Exercise 3: Multivariate Linear Regression
预处理数据
Preprocessing the inputs will significantly increase gradient descent’s efficiency
Matlab代码
x=load('L:\\MachineLearning2016\\ex3x.dat');
y=load('L:\\MachineLearning2016\\ex3y.dat');
m = length(x(:,1));
x = [ones(m, 1), x];
sigma = std(x);
mu = mean(x);
x(:,2) = (x(:,2) - mu(2))./ sigma(2);
x(:,3) = (x(:,3) - mu(3))./ sigma(3);%%规整化输入数据
theta = zeros( size( x(1,:) ) )';
alpha= 0.18;
J = zeros(50, 1); %%这里只迭代50次
for num_iterations = 1:50
J(num_iterations) = (x*theta - y)' * (x * theta -y) /m/2; %% Calculate your cost function here %%
theta = theta -( (x*theta -y)' * x)' * alpha /m /2; %% Result of gradient descent update %%
end
% now plot J
% technically, the first J starts at the zero-eth iteration
% but Matlab/Octave doesn't have a zero index
figure;
plot(0:49, J(1:50), '-')
xlabel('Number of iterations')
ylabel('Cost J')
%Prediction
realx =[1,1650,3];
realx(2) = (realx(2) - mu(2))./ sigma(2);
realx(3) = (realx(3) - mu(3))./ sigma(3);
realx*theta
Normal equations
不对数据进行预处理
x=load('L:\\MachineLearning2016\\ex3x.dat');
y=load('L:\\MachineLearning2016\\ex3y.dat');
m = length(x(:,1));
x = [ones(m, 1), x];
theta = (x'*x) \(x'*y);
J3= (x*theta - y)' * (x * theta -y)/m/2;
realx =[1,1650,3];
realx*theta;
TIPS:Normal equations 好处是不用对数据进行规整化。缺点是矩阵运算比较占用计算机资源。
这里有个疑问,对要预测的数据的处理,[1,1650,3],规整化是否是直接使用样本数据的均值和标准差。
注:上面的 scale data 只迭代了50次,与Normal equations的结果有较大误差。我就懒得去验证了。
最新文章
- 前端学HTTP之客户端识别和cookie
- linux 程序管理与SElinux
- 【代码笔记】iOS-点击出现选择框
- HTML与CSS入门——第十二章 在网页中使用多媒体
- POJ 3449 Geometric Shapes
- Shiro第五篇【授权过滤、注解、JSP标签方式、与ehcache整合】
- Hessian源码分析--HessianProxy
- W3C------JS
- Javascript 严格模式(strict mode)详解
- jquery的$(selector).each(function(index,element))和$.each(dataresource,function(index,element))的区别
- nginx 重定向 说明
- Linux configure,make,make install
- Fundebug是这样备份数据的
- falsk 与 django 过滤器的使用与区别
- git从安装到使用
- Vue.js示例:树型视图; 模式组件;
- node(2)
- struts2第四天——拦截器和标签库
- nagios监控mysql
- 彻底弄懂JS原型与继承
热门文章
- 大数据处理中的Lambda架构和Kappa架构
- FTP无法登录问题-内有网盘福利
- 安装sublime text3 、转化为汉化版、安装SublimeREPL使得在交互条件下运行代码,设置快捷键
- gym102201E_Eat Economically
- 面试官:都说阻塞 I/O 模型将会使线程休眠,为什么 Java 线程状态却是 RUNNABLE?
- 学习WEBAPI第一天
- SharePoint 2013 Sandbox Solution
- JS 生成唯一值UUID
- 如何增强VR的vection/self-motion?
- JSON parse error: No suitable constructor found for type