从零单排入门机器学习:线性回归(linear regression)实践篇
2023-09-11 14:20:43 时间
线性回归(linear regression)实践篇
之前一段时间在coursera看了Andrew ng的机器学习的课程,感觉还不错,算是入门了。
这次打算以该课程的作业为主线,对机器学习基本知识做一下总结。小弟才学疏浅,如有错误。敬请指导。
问题原描写叙述:
you will implement linear regression with one variable to predict prots for a food truck. Suppose you are the CEO of a restaurant franchise and are considering dierent cities for opening a new outlet. The chain already has trucks in various cities and you have data for prots and populations from the cities.
简单来说,就是依据一个城市的人口数量,来预測一辆快餐车能获得的利益。
数据集大概是这样子的:
一行数据为一个样本。第一列表示人口,第二列表示利益。
首先。先把数据可视化。
%% ======================= Part 2: Plotting ======================= fprintf('Plotting Data ...\n') data = load('ex1data1.txt'); X = data(:, 1); y = data(:, 2); m = length(y); % number of training examples % Plot Data % Note: You have to complete the code in plotData.m plotData(X, y); fprintf('Program paused. Press enter to continue.\n'); pause;
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 window plot(x, y, 'rx', 'MarkerSize', 10); % Plot the data ylabel('Profit in $10,000s'); % Set the y label xlabel('Population of City in 10,000s'); % Set the x label % ============================================================ end
计算cost function
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 values m = length(y); % number of training examples % You need to return the following variables correctly % ====================== YOUR CODE HERE ====================== % Instructions: Compute the cost of a particular choice of theta % You should set J to the cost. H = X*theta; diff = H - y; %J = sum(diff.^2)/(2*m); J = sum(diff.*diff)/(2*m); % ========================================================================= end
为了方便理解上面代码,看看各变量大概长什么样子的。
梯度下降法计算參数theta
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 values m = length(y); % number of training examples J_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. % H = X*theta-y; theta(1) = theta(1) - sum(H.* X(:,1))*alpha/m;%感觉这样写挺搓的 theta(2) = theta(2) - sum(H.* X(:,2))*alpha/m; %theta = theta - alpha * (X' * (X * theta - y)) / m; % ============================================================ % Save the cost J in every iteration J_history(iter) = computeCost(X, y, theta); end end
难以理解的是theta = theta - alpha * (X' * (X * theta - y)) / m; 这样的向量化算法。
先看看theta本质是怎么计算的
再看看各变量长什么样子的
算出theta之后,就能够画出拟合直线了。
注:本文作者linger,如有转载。请标明转载于http://blog.csdn.net/lingerlanlan。
本文链接:http://blog.csdn.net/lingerlanlan/article/details/32162559
相关文章
- 神经网络与机器学习 笔记—基本知识点(上)
- (《机器学习》完整版系列)第3章 线性模型——3.3 线性判别分析LDA(将平面上点投影到线上)
- (《机器学习》完整版系列)第14章 概率图模型——14.9 变分推断的详细推导(找一个“数学性质好的分布”来近似代替)
- 偏差(Bias)和方差(Variance)——机器学习中的模型选择zz
- 【FPGA教程案例94】机器学习1——基于FPGA的SVM支持向量机二分类系统实现之理论和MATLAB仿真
- 机器学习/人工智能的笔试面试题目——NLP相关面试
- 机器学习面试题——朴素贝叶斯
- 机器学习:入门方法与学习路径 (附资料)
- 《机器学习与R语言(原书第2版)》一 第2章 数据的管理和理解
- 机器学习常用数学符号及读法大全
- 《Python机器学习——预测分析核心算法》——1.5 构建预测模型的流程
- 机器学习入门学习资源
- 《Scala机器学习》一一1.1 Scala入门
- 《Scala机器学习》一一1.4 基本抽样、分层抽样和一致抽样
- Meson:支持Netflix的机器学习工作流
- 机器学习入门--MNIST(一)
- 整个机器学习入门框架以及机器学习预测股票市场例子-笔记海纳百川版
- Python 基础 之 jupyter notebook 中机器学习的简单入门书写数字识别 demo 操作学习
- 从入门机器学习的零单排:OctaveMatlab经常使用绘图知识
- 【机器学习】:特征工程
- 【机器学习】:Xgboost使用optuna进行调试参数
- <机器学习实战>读书笔记--朴素贝叶斯
- 机器学习的数学基础-(二、线性代数)