热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

吴恩达机器学习Exercise1的部分Octavecommand

在吴恩达视频中的第35节有讲师关于如何提交答案的过程介绍,以下的代码部分没有提交过,正确性还有待考量。主要记录的是生成gradientdiscent函

在吴恩达视频中的第35节有讲师关于如何提交答案的过程介绍,以下的代码部分没有提交过,正确性还有待考量。

主要记录的是生成gradient discent函数的过程,这次我用Octave生成的gradient descent函数和原数据汇集图的对比图:

看起来感觉挺对应的

(英语小白)


the first part

Now there are some data and are asked to be visiualized ,and these data are saved in ex1data.txt.The basic steps has been given,so it's easy the get the plot.

As the exercise asks to complete plotData.m to draw the plot,here are the code in plotData.m:

function plotData(x, y)
plot(x,y,'rx','MarkerSize',10);
ylabel('Profit in $10,000');
xlabel('Poputation in 10,000');

For the first command,both x and y are vector,saving the dataset of profit and polulation respectively.The second command,the plot function has two propertise,x and y, so the result is a scatter plot with red('rx') cross(叉号).The rest two command are just label.

Now the function is finished,and by typing the command in Octave小黑框 and calling this function,the plot can be visualized.

here are the commands:

>>cd E:\Octave\exercise;
>>data = load('ex1data1.txt');

The first command is to find the path of dataset,and then downlod them.So now 'data' is a matrix with two colume.

Define two vector X and y ,x is the first colume of 'data',y is the another,here are the commands:

>>X=data(:,1);
>>y=data(:,2);

we can see the x vector :

>>X
X =6.11015.52778.51867.00325.85988.38297.47648.57816.48625.05465.710714.16405.73408.40845.64075.37946.36545.13016.42967.07086.189120.27005.49016.32615.564918.945012.8280

well it's clear to see what the vector X saved

now call the function dataPlot,and then we can see the plot

>>cd 'E:\Octave\exercise';
>>plotData(X,y);

and here is the plot:

写的啰嗦一点方便以后忘了回看*——*


the second part

The vedio has taught the cost function a lot,the cost function:

you know that h(x) is a linear regression,with two unkonwn parameters θ1,θ2:

Let's just hypothesis that we know theta(θ) completely

now define a metrix theta ={0;0},so we have all the factors to define cost function:x,θ,y,m(m is a real number,meaning the number of training examples,that is the length of y)

well before figure the cost funxtion,it's necessary to notice the way to figure the h(x):a transfer mertix 'θ' times a metrix x,which is also the way where Octave figure the h(x).Thus,as we know now that theta is a two rows one columns mertix,while x is a m rows one column metrix.This means,x need to make some change--it's ok to add a colume to x,and all the result will be right,the new x likes this:

    1.0000    6.1101
    1.0000    5.5277
    1.0000    8.5186
    1.0000    7.0032
    1.0000    5.8598
    1.0000    8.3829.........

so now it's can time to theta correctly

Following the exercise,now we complete the computeCost.m,here are the code:

function J = computeCost(X, y, theta)
m = length(y);
predictions=X*theta;
sqrErros=(predictions-y).^2;
J=sum(sqrErros)/(2*m);

the predictions is the predict result of y,by using the theta that we predict (0,0).

So now it can figure out the J function.

well the last step is to type the command and call the computeCost function in Octave小黑框,here are the commands:

>>m=length(y);
>>x=[ones(m,1),data(:,1)];
>>theta=zeros(2,1);
>>cd 'E:\Octave\exercise'
>>J=computeCost(x,y,theta)
J = 32.073

the second command is to rebuild x

and the result J is super big,it can be changed bu changing the theta.


the third part

now it's time to figure the gradient descent,here is the formula:

There are two θ that needed to be figured until getting  the most proper number.

(in the vedio,the teacher has told about the simply way to figure all the θ a time,that is to regard θ as a vetor and multiply metrix to get the answer.However,I didn't use this complexed way because there are two θ only .But this is not common in reality@_@)

As the formula shows,θj is a real number,and the ultimate result of α...xj would be a real number too,so it's better to calculate the sum of 'h(x)-y)xj' firstly.

the h(x):

As it mentioned before,h(x)=theta(metrix) times x(metrix),the answer will be a m row one column vector,and then minus y vector.

At this moment,x is still a m row two column metrix(the first row is one[maybe there are some grammer mistakes]).To explain this,x is:

     x1:          x2:

    1.0000    6.1101
    1.0000    5.5277
    1.0000    8.5186
    1.0000    7.0032
    1.0000    5.8598
    1.0000    8.3829.........

So,when it calculates θ1,we use x1,and when it calculates θ2,we use x2:

θ1=θ1-α* (1/m) * sum {the transfer metrix of (theta*x-y) * x1}

θ2=θ2-α* (1/m) * sum {the transfer metrix of (theta*x-y) * x2}

α is 0.01,the exercise has given

When I try to complete the gradientDescentMulti.m,I found that I have  no idea to seperate X by using Octave command in the function,I could only get x1 and x2 in Octave 小黑框 .It's a sad thing,maybe I can acquire this skill in later learning

So I have to input parameters x,x1 and x2 in function...:

function [theta, J_history] = gradientDescentMulti(X,x1,x2, y, theta, alpha, num_iters)
m = length(y); % number of training examples
J_history = zeros(num_iters, 1);for iter = 1:num_iterspredictions=X*theta;vector_1=(predictions-y).*x1;theta(1)=theta(1)-alpha/m*sum(vector_1);vector_2=(predictions-y).*x2;theta(2)=theta(2)-alpha/m*sum(vector_2);%figure the cost function and same them in J_historypredictions=X*theta;sqrErros_2=(predictions-y).^2;J_history(iter)=sum(sqrErros_2)/(2*m);
end

the meaning of  num_iters is the time of calculate the theta,again and again,until getting the most proper results.

J_history is a num_iters row vector,saving all of the result of cost function(J function)

Now you can define the theta by yourself,I define them with [1;5]

here are the commands in Octave小黑盒:

>>x1=X(:,1)
>>x2=X(:,2)
>>theta=[1;5];
>>cd 'E:\Octave\exercise';
>>[theta,J]=gradientDescentMulti(x,x1,x2,y,theta,alpha,1)
theta =-2.49851.5015J = 12.843

the first and second command is to seperate the x metrix

I try one time,and get the J=12.843.

then I try a few hundreds times,and get all the J=4.5661

>>[theta,J]=gradientDescentMulti(x,x1,x2,y,theta,alpha,100)
theta =-2.90621.0938J =4.56614.56614.56614.56614.5661

now I suppose the theta gets the good answer

in the end,I make the ultimate plot,commands:

>>a=[5:4:25]
a =5 9 13 17 21 25>>b=theta(1)+theta(2)*a %use the theta
b =2.5630 6.9384 11.3137 15.6890 20.0644 24.4397>>plot(a,b)
>>hold on;%keep two lines on one image
>>plotData(X,y);

plot:

 

(欢迎指错)

 


推荐阅读
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 本文由编程笔记#小编为大家整理,主要介绍了logistic回归(线性和非线性)相关的知识,包括线性logistic回归的代码和数据集的分布情况。希望对你有一定的参考价值。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • Android Studio Bumblebee | 2021.1.1(大黄蜂版本使用介绍)
    本文介绍了Android Studio Bumblebee | 2021.1.1(大黄蜂版本)的使用方法和相关知识,包括Gradle的介绍、设备管理器的配置、无线调试、新版本问题等内容。同时还提供了更新版本的下载地址和启动页面截图。 ... [详细]
  • 展开全部下面的代码是创建一个立方体Thisexamplescreatesanddisplaysasimplebox.#Thefirstlineloadstheinit_disp ... [详细]
  • 不同优化算法的比较分析及实验验证
    本文介绍了神经网络优化中常用的优化方法,包括学习率调整和梯度估计修正,并通过实验验证了不同优化算法的效果。实验结果表明,Adam算法在综合考虑学习率调整和梯度估计修正方面表现较好。该研究对于优化神经网络的训练过程具有指导意义。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 利用Visual Basic开发SAP接口程序初探的方法与原理
    本文介绍了利用Visual Basic开发SAP接口程序的方法与原理,以及SAP R/3系统的特点和二次开发平台ABAP的使用。通过程序接口自动读取SAP R/3的数据表或视图,在外部进行处理和利用水晶报表等工具生成符合中国人习惯的报表样式。具体介绍了RFC调用的原理和模型,并强调本文主要不讨论SAP R/3函数的开发,而是针对使用SAP的公司的非ABAP开发人员提供了初步的接口程序开发指导。 ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • 本文讨论了在openwrt-17.01版本中,mt7628设备上初始化启动时eth0的mac地址总是随机生成的问题。每次随机生成的eth0的mac地址都会写到/sys/class/net/eth0/address目录下,而openwrt-17.01原版的SDK会根据随机生成的eth0的mac地址再生成eth0.1、eth0.2等,生成后的mac地址会保存在/etc/config/network下。 ... [详细]
  • 本文介绍了机器学习手册中关于日期和时区操作的重要性以及其在实际应用中的作用。文章以一个故事为背景,描述了学童们面对老先生的教导时的反应,以及上官如在这个过程中的表现。同时,文章也提到了顾慎为对上官如的恨意以及他们之间的矛盾源于早年的结局。最后,文章强调了日期和时区操作在机器学习中的重要性,并指出了其在实际应用中的作用和意义。 ... [详细]
  • 如何搭建Java开发环境并开发WinCE项目
    本文介绍了如何搭建Java开发环境并开发WinCE项目,包括搭建开发环境的步骤和获取SDK的几种方式。同时还解答了一些关于WinCE开发的常见问题。通过阅读本文,您将了解如何使用Java进行嵌入式开发,并能够顺利开发WinCE应用程序。 ... [详细]
  • 本文详细介绍了如何使用MySQL来显示SQL语句的执行时间,并通过MySQL Query Profiler获取CPU和内存使用量以及系统锁和表锁的时间。同时介绍了效能分析的三种方法:瓶颈分析、工作负载分析和基于比率的分析。 ... [详细]
  • 本文记录了在vue cli 3.x中移除console的一些采坑经验,通过使用uglifyjs-webpack-plugin插件,在vue.config.js中进行相关配置,包括设置minimizer、UglifyJsPlugin和compress等参数,最终成功移除了console。同时,还包括了一些可能出现的报错情况和解决方法。 ... [详细]
author-avatar
罗丝012
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有