转行程序员1 机器学习 线性回归 Linear Regression 纯属敦促自己学习
来源:互联网 发布:人工智能相关的电影 编辑:程序博客网 时间:2024/03/28 21:21
主要学习了Andrew Ng的公开课 machine learning 之Linear Regression,其exercise网址如下
http://openclassroom.stanford.edu/MainFolder/DocumentPage.php?course=MachineLearning&doc=exercises/ex2/ex2.html
编写的代码如下:
%%%%%%%%%%%%%%%%%%%%%%%%Linear regression
clear all; close all; clc
x = load('ex2x.dat');
y = load('ex2y.dat');
figure % open a new figure window
plot(x, y, 'o');
ylabel('Height in meters')
xlabel('Age in years')
m = length(y); % store the number of training examples 样本数量
x = [ones(m, 1), x]; % Add a column of ones to x
theta=zeros(2,1);
alpha=0.07;% learning rate
for inter=1:1500 % 迭代次数
h_theta=x*theta;
theta=theta-alpha/m*x'*(h_theta-y);
end
hold on % Plot new data without clearing old plot
plot(x(:,2), x*theta, '-') % remember that x is now a matrix with 2 columns
% and the second column contains the time info
legend('Training data', 'Linear regression')
%%%%%%%%%%%%%%%%%%%%%%%% Understanding J(theta)
J_vals = zeros(100, 100); % initialize Jvals to 100x100 matrix of 0's
theta0_vals = linspace(-3, 3, 100);
theta1_vals = linspace(-1, 1, 100);
for i = 1:length(theta0_vals)
for j = 1:length(theta1_vals)
t = [theta0_vals(i); theta1_vals(j)];
J_vals(i,j) = 1/2/m*sum((theta0_vals(i)+theta1_vals(j)*x(:,2)-y(:)).^2);
end
end
% Plot the surface plot
% Because of the way meshgrids work in the surf command, we need to
% transpose J_vals before calling surf, or else the axes will be flipped
J_vals = J_vals';
figure;
surf(theta0_vals, theta1_vals, J_vals)
xlabel('\theta_0'); ylabel('\theta_1')
% Plot the cost function with 15 contours spaced logarithmically
% between 0.01 and 100
figure;
contour(theta0_vals, theta1_vals, J_vals, logspace(-2, 2, 15))
xlabel('\theta_0'); ylabel('\theta_1')
- 转行程序员1 机器学习 线性回归 Linear Regression 纯属敦促自己学习
- 转行程序员2 机器学习 线性回归 Linear Regression II 纯属敦促自己学习
- 转行程序员3 机器学习 Logistic Regression 纯属敦促自己学习
- 转行程序员4 机器学习 Regularization 纯属敦促自己学习
- 机器学习1 线性回归 Linear Regression
- 机器学习:线性回归(Linear Regression)
- 【机器学习】线性回归Linear Regression
- 机器学习(1)---线性回归(Linear Regression)
- Andrew机器学习笔记1:线性回归 linear regression
- 机器学习1——线性回归Linear Regression
- 斯坦福大学机器学习——线性回归(Linear Regression)
- 机器学习手记[9]--线性回归linear regression
- 机器学习(一)------- 线性回归(Linear regression )
- 机器学习之线性回归(Linear Regression)
- [笔记]机器学习(Machine Learning) - 01.线性回归(Linear Regression)
- 机器学习二-线性回归(linear regression)
- 机器学习算法之线性回归(Linear Regression)
- PYTHON机器学习实战——线性回归 Linear Regression
- About Scrum
- 验证短信集成
- 杨氏矩阵的增删改查
- HBase 常用Shell命令
- Android 4.4 Kitkat Phone工作流程浅析(三)__MO(去电)流程分析
- 转行程序员1 机器学习 线性回归 Linear Regression 纯属敦促自己学习
- 线程安全知识
- 什么牌子的视频会议系统好?
- multer中间件上传文件
- PHP/Java Bridge:PHP extend继承Java的interface,Java的interface直接在PHP实现
- 基于angularjs的瀑布流插件angular-waterfull.js
- Android Service与Activity之间通信使用Messenger方式
- Unity3d 和 3dmax轴向问题以及快速解决方案
- 虚函数调用