Machine Learning:Regression with multi variables
来源:互联网 发布:警惕网络陷阱教学设计 编辑:程序博客网 时间:2024/06/18 04:04
学习NG的Machine Learning教程,先关推导及代码。由于在matleb或Octave中需要矩阵或向量,经常搞混淆,因此自己推导,并把向量的形式写出来了,主要包括cost function及gradient descent
见下图。
图中可见公式推导,及向量化表达形式的cost function(J).
图中为参数更新的向量化表达方式(其中有一处写错了,不想改了。。。)
图中为feature scaling的推导,及向量化表示
下面regression with multi variable的代码
%loading datadata = load('ex1data2.txt');X = data(:, 1:2); %X : m*2y = data(:, 3); % y : m*1m = length(y)%Scale features and set them to zero meanfprintf('Normalizing Features ...\n');[X mu sigma] = featureNormalize(X);% Add intercept term to XX = [ones(m, 1) X];fprintf('Running gradient descent ...\n');% Choose some alpha valuealpha = 0.05;num_iters = 400;% Init Theta and Run Gradient Descent theta = zeros(3, 1); % theta:3*1[theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters);% Plot the convergence graphfigure;plot(1:numel(J_history), J_history, '-b', 'LineWidth', 2); xlabel('Number of iterations');ylabel('Cost J');% Display gradient descent's resultfprintf('Theta computed from gradient descent: \n');fprintf(' %f \n', theta);fprintf('\n');
其中featureNormalize代码如下
function [X_norm, mu, sigma] = featureNormalize(X)X_norm = X; % X: m*2mu = zeros(size(X,2),1); sigma = zeros(size(X, 2),1); mu = mean(X)'; % m*1 ,对每列求meansigma = std(X)'; %m*1, 对每列求stdX_norm = (X .- mu') ./ sigma';end
其中gradientDescentMulti的代码如下
function [theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters)m = length(y); % number of training examplesJ_history = zeros(num_iters, 1);for iter = 1:num_iters error = X*theta - y; % m*1 theta = theta - alpha/m*(X'*error); J_history(iter) = computeCostMulti(X, y, theta);endend
其中computeCostMulti的代码如下
function J = computeCostMulti(X, y, theta)m = length(y); % number of training examplesJ = 0;error = X*theta - y; %m*1J = 1/(2*m)*sum(error .^ 2);end
为方便查找,下附公式原图
阅读全文
1 0
- Machine Learning:Regression with multi variables
- Machine Learning:Linear Regression With Multiple Variables
- Machine Learning - Linear Regression with Multiple Variables
- 【Stanford Machine Learning】Lecture 2--Linear Regression with Multiple Variables
- Machine Learning by Andrew Ng---Linear Regression with multiple variables
- Machine Learning week 2 quiz: Linear Regression with Multiple Variables
- Machine Learning -- Linear Regression with Multiple Variables(Andrew Ng)
- Machine Learning -- Linear Regression with Multiple Variables(Andrew Ng)
- Machine Learning Week 2 Linear Regression with multiple variables in Matlab or Octave
- [Machine Learning (Andrew NG courses)]IV.Linear Regression with Multiple Variables
- Machine Learning - IV. Linear Regression with Multiple Variables多变量线性规划 (Week 2)
- Coursera-Machine Learning 笔记(二)Linear Regression with multiple variables
- Coursera Machine Learning 第二周 quiz Linear Regression with Multiple Variables 习题答案
- Coursera Machine Learning 第二周 quiz Linear Regression with Multiple Variables 习题答案 标签: Andrew NgCour
- Machine Learning - Logistic Regression - Multi-class Classification
- Machine Learning:Linear Regression With One Variable
- Machine Learning - Linear Regression with One Variable
- Machine learning : Regression with one variable
- 简单理解src和ng-src
- Attention-based Extraction of Structured Information from Street View Imagery
- MPAndroidChart 画柱状图,线等统计图。
- shell read处理用户输入示例讲解
- 开发环境、生产环境、测试环境的基本理解和区别
- Machine Learning:Regression with multi variables
- PAT:1005. 继续(3n+1)猜想 (25)
- hibernate设置默认值
- 【怎样写代码】偷窥高手 -- 反射技术(四):深入窥视属性
- easyui表格数据折叠的使用经验
- day_12_信号处理、进程间通信
- mongodb 查询条件
- 【JavaScript】一些基本的知识
- WUST 1873 潜水员 (二维背包) 【模板】