cost function:

, where 

我们的目的是找到最优的两个参数theta 0,  theta 1, 来使得cost function 值最小。

方法:Gradient Descent(又叫Batch Gradient Descent)

问题描述:Suppose you are the CEO of a restaurant franchise and are considering different cities for opening a new outlet. The chain already has trucks in various cities and you have data for profits and populations from the cities. You would like to use this data to help you select which city to expand to next. The file ex1data1.txt contains the dataset for our linear regression problem. The first column is the population of a city and the second column is the profit of a food truck in that city. A negative value for profit indicates a loss.

MATLAB代码实现:

clear ; close all; clcdata = load('ex1data1.txt');
X = data(:, 1); y = data(:, 2);
m = length(y);  % number of training examples% Plot Data
plot(X, y, 'rx', 'MarkerSize', 10); % Plot the data
ylabel('Profit in $10,000s'); % Set the y axis label
xlabel('Population of City in 10,000s'); % Set the x axis label%% =================== Part 1: Cost and Gradient descent ===================X = [ones(m, 1), data(:,1)]; % Add a column of ones to x
theta = zeros(2, 1); % initialize fitting parameters% Some gradient descent settings
iterations = 1500;
alpha = 0.01;for i=1:iterationstheta_s=theta;theta(1) = theta(1) - alpha / m * sum(X * theta_s - y);       theta(2) = theta(2) - alpha / m * sum((X * theta_s - y) .* X(:,2));
end% Plot the linear fit
hold on; % keep previous plot visible
plot(X(:,2), X*theta, '-')
legend('Training data', 'Linear regression')
hold off % don't overlay any more plots on this figure% Predict values for population sizes of 35,000 and 70,000
predict1 = [1, 3.5] *theta;
predict2 = [1, 7] * theta;%% ============= Part 2: Visualizing J(theta_0, theta_1) =============% Grid over which we will calculate J
theta0_vals = linspace(-10, 10, 100);
theta1_vals = linspace(-1, 4, 100);% initialize J_vals to a matrix of 0's
J_vals = zeros(length(theta0_vals), length(theta1_vals));% Fill out J_vals
for i = 1:length(theta0_vals)for j = 1:length(theta1_vals)t = [theta0_vals(i); theta1_vals(j)];J_vals(i,j) = computeCost(X, y, t);end
end% 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';
% Surface plot
figure;
surf(theta0_vals, theta1_vals, J_vals)
xlabel('\theta_0'); ylabel('\theta_1');% Contour plot
figure;
% Plot J_vals as 15 contours spaced logarithmically between 0.01 and 100
contour(theta0_vals, theta1_vals, J_vals, logspace(-2, 3, 20))
xlabel('\theta_0'); ylabel('\theta_1');
hold on;
plot(theta(1), theta(2), 'rx', 'MarkerSize', 10, 'LineWidth', 2);

运行结果:predict1=0.4520;predict2=4.5342

吴恩达机器学习: 单变量线性回归 理论与实践相关推荐

  1. 吴恩达机器学习--单变量线性回归【学习笔记】

    说明:本文是本人用于记录学习吴恩达机器学习的学习笔记,如有不对之处请多多包涵. 作者:爱做梦的90后 一.模型的描述: 下面的这张图片是对于课程中一些符号的基本的定义: 吴老师列举的最简单的单变量线性 ...

  2. 机器学习入门(吴恩达)——单变量线性回归

    2.1模型表示 从线性回归算法开始学起,从一个预测住房价格的例子开始,需要一个数据集(包含住房价格),根据不同房子尺寸的大小所出售的价格,画出数据集如下图 要想知道多大的房子可以出售什么价格,就需要构 ...

  3. 吴恩达机器学习课后作业——线性回归(Python实现)

    1.写在前面 吴恩达机器学习的课后作业及数据可以在coursera平台上进行下载,只要注册一下就可以添加课程了.所以这里就不写题目和数据了,有需要的小伙伴自行去下载就可以了. 作业及数据下载网址:吴恩 ...

  4. 吴恩达机器学习课后作业——线性回归

    一.单变量线性回归 一.作业内容 假设你是一家特许经营餐厅的首席执行官,正在考虑在不同的城市开一家新店,该连锁店已经在各个城市开设了餐车,文件ex1data1.txt中包含你这些城市的利润和人口数据. ...

  5. 吴恩达机器学习之多变量线性回归实现部分

    C++实现梯度下降法 "linear_regression.h" //多变量线性回归模型 struct elem_var2 {double y;double* x; //用数组传入 ...

  6. 吴恩达机器学习作业 1线性回归

    一.单变量线性回归 数据可视化 import numpy as np import pandas as pd import matplotlib.pyplot as plt #print(np.eye ...

  7. 从六大概念总结吴恩达机器学习书籍:如何做好工程项目实践?

    选自towardsdatascience 作者:Niklas Donges 机器之心编译 在 ML 工程实践中,很多时候都会走一些弯路,可能是模型选错了,也可能是某个超参数一直不正确.那么我们该如何根 ...

  8. 吴恩达机器学习之逻辑回归理论部分

    一.特征函数 对应分类问题,我们先针对二分类问题进行讨论,对应计算机而言,分类即将数据按其特征值不同分为不同的集合,仅对应二分类问题,我们只需考虑分为:正类和负类,为此我们引入特征函数. y=1 - ...

  9. pytorch实现吴恩达机器学习课后作业——线性回归

    线性回归 题目和数据 题目:使用ex1data1.txt中给出的两个变量,分别设为x,y,来预测卡车利润(y)的收益值. 在数据集,第一列表示城市人数(x),第二列该城市的卡车利润(y) 数据集: 6 ...

最新文章

  1. POJ 1414 Life Line(搜索)
  2. 他研究了5000家AI公司,说人工智能应用该这么做!
  3. CouchDB 简单HTTP接口使用说明
  4. php 实现 html转js
  5. 你的电池再充几次电就报废?机器学习帮你预测电池寿命
  6. Leetcode 剑指 Offer 11. 旋转数组的最小数字 (每日一题 20210916)
  7. Scrapy-xpath用法以及实例
  8. 光模块价格由带宽还是距离决定_100G QSFP28 CWDM4光模块介绍及应用
  9. ListView的Columns自适应内部文字
  10. 零信任策略下云上安全信息与事件管理实践
  11. Linux -Ubuntu安装 Tomcat
  12. ubuntu 绑定网卡
  13. unity---------------------关于BuildAssetBundles的使用(打包)
  14. ug区域轮廓铣没有重叠距离_UG加工基本操作
  15. 华三中各种路由协议的缺省优先级
  16. Spring MVC如何配置OpenSessionInViewInterceptor并结合Hibernate使用
  17. cxf整合spring发布webservices服务端
  18. 从零开始开发HybridApp
  19. 计算机设置新用户名和密码怎么设置路由器,怎么修改无线路由器密码和用户名【图】...
  20. DASCTF2022 7月赋能赛 crypto wp(DASCTF2022.07赋能赛Pwn easyheap)

热门文章

  1. Excel完成随机抽奖的小游戏
  2. iOS - 抖音效果
  3. 如何提高抖音短视频流量?
  4. 光学系统常用光学参数的测量
  5. ps去除人像脸上的油光
  6. 小样本学习的k-way n-shot
  7. 鸿蒙渊更新公告,《天下3》更新公告(版本2.0.703)
  8. Scala的Map方法
  9. python运算符计算集合差集的是_集合的运算(交集、并集和差集)
  10. 欧亚经济联盟 EAC认证-俄罗斯、白俄罗斯、哈萨克斯坦、吉尔吉斯斯坦和亚美尼亚