Andrew Ng coursera上的《机器学习》ex1
来源:互联网 发布:北京学java最好的学校 编辑:程序博客网 时间:2024/05/29 11:42
Andrew Ng coursera上的《机器学习》ex1
本系列文章是在coursera上学习Andrew Ng的《机器学习》之后,对练习题进行了一些总结。我是初学者,所以肯定存在很多错误,欢迎大家能够给我提意见。
按照课程所给的ex1的文档要求,ex1要求完成以下几个计算过程的代码编写:
- Function to run gradient descent
1. warmUpExercise.m
要求通过写Octave/MATLAB代码返回一个5阶的单位矩阵。
X = eye(5);
2. plotData.m
要求将二维的训练数据的x和y用图展示出来。
function plotData(x, y)%PLOTDATA Plots the data points x and y into a new figure % PLOTDATA(x,y) plots the data points and gives the figure axes labels of% population and profit.% ====================== YOUR CODE HERE ======================% Instructions: Plot the training data into a figure using the % "figure" and "plot" commands. Set the axes labels using% the "xlabel" and "ylabel" commands. Assume the % population and revenue data have been passed in% as the x and y arguments of this function.%% Hint: You can use the 'rx' option with plot to have the markers% appear as red crosses. Furthermore, you can make the% markers larger by using plot(..., 'rx', 'MarkerSize', 10);figure; % open a new figure windowplot(x,y,'rx','MarkerSize',10);ylabel('Profit in $10,000s');xlabel('Population of City in $10,000s');% ============================================================end
其中传入的参数X,Y分别用下面的代码求出:
data = load('ex1data1.txt');X = data(:, 1); y = data(:, 2);m = length(y); % number of training examples
3. computeCost.m
要求:计算出线性回归函数中对应于只有一个特征值(X是二维的)的情况进行计算。
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 valuesm = 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.J = sum((X*theta-y).^2)/(2*m);% =========================================================================end
其中传入的参数分别为:
X = [ones(m, 1), data(:,1)]; % Add a column of ones to xtheta = zeros(2, 1); % initialize fitting parameters% Some gradient descent settingsiterations = 1500;alpha = 0.01;
其中的X是一个m*2的矩阵,theta是一个2 1的矩阵,所以m个数据集的h(x)= X theta,与之前描述的h(x) = (theta^T ) * X 有一定的区别,需要注意。这些是通过数学的推导得到的结果。
4.gradientDescent.m
利用批量梯度下降来对参数进行最优化求解。梯度下降的公式如下:
theta = theta - α * sum(h(x)-y)*x /m
其中的sum可以用求和符号表示。在Octave中,可以转为矩阵来进行计算。
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 valuesm = length(y); % number of training examplesJ_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 = theta - alpha*X'*(X*theta-y)/m; % ============================================================ % Save the cost J in every iteration J_history(iter) = computeCost(X, y, theta);end
其中传入的参数:
X = [ones(m, 1), data(:,1)]; % Add a column of ones to xtheta = zeros(2, 1); % initialize fitting parameters% Some gradient descent settingsiterations = 1500;alpha = 0.01;
0 0
- Andrew Ng coursera上的《机器学习》ex1
- Coursera上的Andrew Ng《机器学习》学习笔记Week1
- Coursera上的Andrew Ng《机器学习》学习笔记Week2
- Andrew Ng coursera上的《机器学习》ex2
- Andrew Ng coursera上的《机器学习》ex3
- Andrew Ng coursera上的《机器学习》ex4
- Andrew Ng coursera上的《机器学习》ex5
- Andrew Ng coursera上的《机器学习》ex6
- Andrew Ng coursera上的《机器学习》ex7
- Andrew Ng coursera上的《机器学习》ex8
- Coursera上Andrew Ng机器学习课程总结(一)
- Coursera上Andrew Ng机器学习课程总结(二)
- Coursera 的机器学习 (Andrew Ng) 课程 视频百度云
- coursera斯坦福Andrew Ng的机器学习编程作业答案
- Coursera机器学习(Andrew Ng)笔记1
- Coursera机器学习(Andrew Ng)笔记:神经网络
- Andrew Ng机器学习笔记ex1 线性回归
- Coursera机器学习(Andrew Ng)笔记:大规模机器学习
- python对文件的简单操作
- iOS静态库与动态库
- 学习设计模式-方法工厂设计模式
- MaterialDesign 的一些规范
- 带进度条的webview
- Andrew Ng coursera上的《机器学习》ex1
- android 字符串拼接
- Mysql删除语句
- 微软医疗BI简介
- vim中大小写转换
- WebView详解(一)
- iOS 开发中你是否遇到这些经验问题
- Android 九宫格解锁Demo--Android 进阶之路
- 常用GIT bash 命令一览