Octave Tutorial - Basic operations

摘要: 本文是吴恩达 (Andrew Ng)老师《机器学习》课程,第六章《Octave教程》中第38课时《基本操作》的视频原文字幕。为本人在视频学习过程中记录下来并加以修正,使其更加简洁,方便阅读,以便日后查阅使用。现分享给大家。如有错误,欢迎大家批评指正,在此表示诚挚地感谢!同时希望对大家的学习能有所帮助

You now know a bunch about machine learning. In this video/article, I like to teach you a programming language, Octave, in which you'll be able to very quickly implement the learning algorithm we've seen already, and the learning algorithms we'll see later in this course. In the past, I've tried to teach machine learning using a large variety of different programming languages, including C++, Java, Python NumPy, R, and also Octave, and what I found is that students were able to learn the most productively, learn the most quickly and prototype your algorithms most quickly, using a relatively high level language like Octave. In fact, what I often see in Silicon Valley is that even if you want to build a large scale deployment of a learning algorithm, what people often do is prototype and the language is Octave, which is a great prototype language. So you can sort of get your learning algorithms working quickly. And then only if you need a very large scale deployment of it, only then spend your time re-implementing the algorithm to C++, Java or some of the languages like that. Because all the lessons we've learned is that programming time or developing time, that is your time. The machine learning's time is incredibly valuable. And if you can get your learning algorithms to work more quickly in Octave, then overall you have a huge time savings by first developing the algorithms in Octave, and then implementing and maybe C++, Java, only after we have the ideas working. The most common prototyping language I see people use for machine learning are Octave, MATLAB, Python, NumPy, and R. Octave is nice since it's free open sourced. And MATLAB works well too, but it is expensive to many people. But if you have access to a copy of MATLAB, you can also use MATLAB with this class. If you know Python NumPy, or if you know R, I do see some people use it. But, what I see is that people usually end up developing somewhat more slowly, and you know, these languages, because the Python, NumPy syntax is just slightly clunkier than the Octave syntax. And so because of that, and because we are releasing start code in Octave, I strongly recommend that you not try to do the following exercises in this class in NumPy and R, but that I do recommend that you instead do the programming exercises for this class in Octave instead. What I'm going to do in this video/article is go through a list of commands very quickly, and the goal is to quickly show you the range of commands and the range of things you can do in Octave. The course website will have a transcript of everything I do, so after watching this video, you can refer to the transcript posted on the course website when you want to find a command. Concretely, what I recommend you do is first watch the tutorial videos. And after watching to the end, then install Octave on your computer. And finally, go to the course website, download the transcript of the things you see in the session, and type in whatever commands seem interesting to you into Octave, so running on your own computer, you can see it work for yourself. And with that let's get started.

>> 5+6
ans =  11
>> 3-2
ans =  1
>> 5 * 8
ans =  40
>> 1 / 2
ans =  0.50000
>> 2^6
ans =  64
>>

Here's my Windows desktop, and I'm going to start up Octave. And I'm now in Octave. And that's my Octave prompt. Let me first show you the elementary operations you can do in Octave. So you type in 5+6, that gives you the answer 11. 3-2, 5*8, 1/2, 2^6 is 64. So those are the elementary math operations.

>> 1 == 2 %false
ans = 0
>> 1 ~= 2
ans = 1
>> 1 && 0   % AND
ans = 0
>> 1 || 0   % OR
ans = 1
>> xor(1,0)
ans = 1
>> PS1('>> ')

You can also do logical operations. So 1 == 2, this evaluates to false. The percent command here means a comment. So 1 == 2, evaluates to false, which is represented by zero. One not equals to two, this is true. So that returns 1. Note that a not equal sign is this tilde equals symbol. And not bang equals, which is what some other programming language use. Let's see logical operations one and zero. Use a double ampersand sign, to denote the logical AND, and that evaluates false. One or zero is the OR operation, and that evaluates to true. And I can XOR one and zero, and that evaluates to 1. This thing over on the left, this Octave-3.2.4.exe:11 this is the default Octave prompt. It shows what the version in Octave and so on. If you don't want that prompt, there's a somewhat cryptic command, PS1, quote, greater greater than and so on, that you can use to change the prompt. And I guess this quote a string in the middle, quote, greater than, greater than, space, that's what I prefer my Octave prompt to look like. So if I hit enter, now my Octave prompt has changed to the greater than, greater than sign, which you know, looks quite a bit better.

>> a = 3
a =  3
>> a = 3;  %semicolon supressing output
>> b = 'hi';
>> b
b = hi
>> c = (3>=1);
>> c
c = 1
>> a=pi;
>> a
a =  3.1416
>> disp(sprintf('2 decimals: %0.2f', a))
2 decimals: 3.14
>> disp(sprintf('6 decimals: %0.6f', a))
6 decimals: 3.141593
>> a
a =  3.1416
>> format long
>> a
a =  3.141592653589793
>> format short
>> a
a =  3.1416
>>

Next let's talk about Octave variables. I can take a variable a and assign it to 3, and hit enter. And now, a is equal to 3. You want to assign a variable, but you don't want to print out the result. If you put a semicolon, the semicolon suppresses the print output. So to do that, enter, it doesn't print anything. Whereas a equals 3, print it out. Where a equals 3 semicolon doesn't print anything. I can do string assignment. b equals 'hi'. Now if I just enter b, it prints out the variable b. So b is the string 'hi'. c equals 3 greater than equal 1. So now, c evaluates to true. If you want to print out or display a variable, here is how you go about it. Let me set a=pi. And if I want to print a, I can just type a like so, and it printed out. For more complex printing, there is also the disp command, which stands for display. Display a just prints out a like so. You can also display strings, so,  disp(sprintf('2 decimal: %0.2f', a)), like so. And this will print out the string '2 decimals: 3.14'. This is kind of an old style C syntax. For those of you that have programmed C before, this is essentially the syntax you use to print screen. So the sprintf generates a string, that is these "2 decimals: 3.14" string. This percent 0.2 means substitute a into here, showing the two digits after the decimal points. And disp takes the string that's generated by the sprintf command, and this actually displays the string. And to show you another example, sprintf('6 decimals: %0.6f', a). And this should print pi with six decimal places. Finally, I was displaying, a like so, looks likes this. There are useful shortcuts, you type format long. It causes strings to by default, be displayed to a lot more decimal places. And format short is a command that restores the default of just printing a small number of digits. Okay, that's how you work with variables.

>> A = [1 2; 3 4; 5 6]
A =

   1   2
   3   4
   5   6

>> a = [1 2;
3 4;
5 6]
a =

   1   2
   3   4
   5   6

>> v = [1 2 3]
v =

   1   2   3

>> v = [1; 2; 3]
v =

   1
   2
   3

>> v = 1:0.1:2
v =

    1.0000    1.1000    1.2000    1.3000    1.4000    1.5000    1.6000    1.7000    1.8000    1.9000    2.0000

>> v = 1:6
v =

   1   2   3   4   5   6

>> ones(2,3)
ans =

   1   1   1
   1   1   1

>> C = 2*ones(2,3)
C =

   2   2   2
   2   2   2

>> C = [2 2 2; 2 2 2]
C =

   2   2   2
   2   2   2

>> w = ones(1,3)
w =

   1   1   1

>> w = zeros(1,3)
w =

   0   0   0

>> w = rand(1,3)
w =

   0.37859   0.31112   0.28616

>> rand(3,3)
ans =

   0.70473   0.70130   0.22457
   0.60853   0.65776   0.34016
   0.98352   0.64556   0.39988

>> w = randn(1,3)
w =

   0.70988  -1.45497  -1.49873

>>

Now let's look at vectors and matrices. Let's say I want to assign A to the matrix. Let me show you an example: 1, 2, semicolon, 3, 4, semicolon, 5, 6. This generates a three by two matrix A, whose first row is 1, 2. Second row is 3, 4. Third row is 5, 6. What the semicolon does is essentially say, go to the next row of the matrix. There are other ways to type this in. Type A equals 1,2 semicolon, 3,4 semicolon, 5, 6 like so. And that's another equivalent way of assigning A to be the values of this three by two matrix. Similarly, you can assign vectors. V equals 1, 2, 3. This is actually a row vector, or this is a 1x3 vector. Right this is a fat wide vector. If I want to assign this to a column vector, what I would do is instead do v equals 1;2;3. And this will give me a 3 by 1, instead of 1 by 3 vector. So this will be a column vector. Here's some more useful notation. Set V equals 1:0.1:2. What this does is it sets V to the bunch of elements that starts from 1, and increment steps of 0.1 until you get up to 2. So if I do this, V is going to be this, you know, row vector. This is what one by eleven matrix really. That's 1, 1.1, 1.2, 1.3 and so on, until we get up to 2. Now, and I can also set V equals 1:6, and that sets V to be these numbers, 1 through 6. Now, here are some other ways to generate matrices. Ones(2,3) is a command that generates a matrix that is a 2 by 3 matrix that is the matrix of all ones. So if I set c equals 2*ones(2,3), this generates a 2 by 3 matrix that is all two's. You can think of that as a shorter way of writing this, c=[2 2 2; 2 2 2] which would also give you the same result. Let's say w=ones(1,3), so this is going to be a row vector, or a row of 3 one's. And similarly you can also say w=zeros(1,3), and this generates a matrix, a 1 by 3 matrix of all zeros. Just a couple more ways to generate matrices. If I do w=rand(1,3), this gives me a 1 by 3 matrix of all random numbers. If I do rand(3,3), this gives me a 3x3 matrix of all random numbers drawn from the uniform distribution between 0 and 1. So every time I do this, I get a different set of random numbers drawn uniformly between 0 and 1. For those of you that know what a Gaussian random variable is, or for those of you that know what a normal random variable is, you can also set w=randn(1,3), and so these are going to be three values drawn from a Gaussian distribution with mean zero and variance or standard deviation equal to one.

And you can set more complex things like w=-6+sqrt(10)*(randn(1,10000)). I'm going to put a semicolon at the end, because I don't really want this printed out. This is going to be a vector with ten thousand elements. And if I now print a histogram of w with a hist command, I can now.

And I can plot a histogram with more buckets, with more bins, with say, 50 bins. And this is my histogram of a Gaussian with mean minus 6. Because I have a minus 6 there plus square root 10 times this. So the variance of this Gaussian random variable is 10, and the standard deviation is square root of 10, which is about 3.1.

>> eye(4)
ans =

Diagonal Matrix

   1   0   0   0
   0   1   0   0
   0   0   1   0
   0   0   0   1

>> I = eye(4)
I =

Diagonal Matrix

   1   0   0   0
   0   1   0   0
   0   0   1   0
   0   0   0   1

>> I = eye(6)
I =

Diagonal Matrix

   1   0   0   0   0   0
   0   1   0   0   0   0
   0   0   1   0   0   0
   0   0   0   1   0   0
   0   0   0   0   1   0
   0   0   0   0   0   1

>>

Finally, one special command for generator matrix, which is the eye command. So eye stands for this is maybe a pun on the word identity. Let's say we have eye(4). This is the 4x4 identity matrix. So I=eye(4). This gives me a 4x4 identity matrix. And I=eye(6), that gives a 6x6 identity matrix.

Lastly, to wrap up this video, there's one more useful command, which is the help command. So you can type help eye, and this brings up the help function for the identity matrix. You can also type help rand. Brings up documentation for the rand or the random number generation function. Or even help help, which shows you help on help function.

So, those are the basic operations in Octave. And with this you should be able to generate a few matrices, multiply, add things, and use the basic operations in Octave. In the next video / article, I'd like to start talking about more sophisticated commands and how to move data around, and start to process data in Octave.

<end>

发布了41 篇原创文章 · 获赞 12 · 访问量 1306

猜你喜欢

转载自blog.csdn.net/edward_wang1/article/details/104046016
今日推荐