brunch

You can make anything
by writing

C.S.Lewis

by 라인하트 Oct 14. 2020

앤드류 응의 머신러닝 정리 (5-3):옥타브 데이터계산

   온라인 강의 플랫폼 코세라의 창립자인 앤드류 응 (Andrew Ng) 교수는 인공지능 업계의 거장입니다. 그가 스탠퍼드 대학에서 머신 러닝 입문자에게 한 강의를 그대로 코세라 온라인 강의 (Coursera.org)에서 무료로 배울 수 있습니다. 이 강의는 머신러닝 입문자들의 필수코스입니다. 인공지능과 머신러닝을 혼자 공부하면서 자연스럽게 만나게 되는 강의입니다. 


Octave / Matlab Tutorial 

옥타브 / 매트랩 튜토리얼 


Computing on Data (데이터 연산)



  Now that you know how to load and save data in Octave, put your data into matrices and so on. In this video, I'd like to show you how to do computational operations on data. And later on, we'll be using these source of computational operations to implement our learning algorithms.


   옥타브 프로그램에서 데이터를 로드하고 저장하는 법과 데이터를 행렬에 넣는 방법을 배웠습니다. 이번 강의에서 데이터를 계산하는 방법을 설명합니다. 나중에 우리가 만들 학습 알고리즘을 구현할 때 필요한 것들입니다.  



   Let's get started. Here's my Octave window. Let me just quickly initialize some variables to use for our example. So set A to be a three by two matrix, and set B to a three by two matrix, and let's set C to a two by two matrix like so. Now let's say I want to multiply two of my matrices. So let's say I want to compute A*C, I just type A*C, so it's a three by two matrix times a two by two matrix, this gives me this three by two matrix. You can also do element wise operations and do A.* B and what this will do is it'll take each element of A and multiply it by the corresponding elements B, so that's A, that's B, that's A .* B. So for example, the first element gives 1 times 11, which gives 11. The second element gives 2 time 12 Which gives 24, and so on. So this is element-wise multiplication of two matrices. And in general, the period tends to, is usually used to denote element-wise operations in Octave. So here's a matrix A, and if I do A .^ 2, this gives me the element wise squaring of A. So 1 squared is 1, 2 squared is 4, and so on.


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

   B = [11,12; 13,14; 15,16];

   C = [1,1; 2,2];


   A * C        % 행렬 곱셈 3 X 2 차원 행렬 A와 2 X 2 차원 행렬 C의 곱셈은 3 X 2 행렬 생성

    A .* B       % 행렬 A의 성분과 대응하는 행렬 B의 성분끼리 곱셈 

    A .^2         % 행렬 A의 성분의 값을 제곱 


   시작합니다. 여기 옥타브 창이 있습니다. 예제에서 사용할 변수들을 간단하게 초기화합니다. A는 3 X 2 행렬이고, B도 3 X 2 행렬로, C는 2 X 2 행렬입니다. 이제 두 개의 행렬 A와 C를 곱합니다. A는 3 X 2 행렬이고 B는 2 X 2 행렬이므로 'A * C' 명령어로 만들어지는 행렬은 3 X 2 행렬입니다. 행렬의 곱셈을 각 성분 단위에서 할 수 있습니다. 'A .* B' 명령어는 행렬 A의 성분과 대응하는 행렬 B의 성분끼리 곱하는 것입니다. 이것이 행렬 A의 값이고, 이것이 행렬 B의 값이고, 그리고 이것이 'A .*B'의 연산 결과입니다. 가령 첫 번째 성분은 1 * 11 = 11 이 되고, 두 번째 성분은 2 * 12  = 24 가 됩니다. 이런 식으로 동작합니다. 이것이 두 행렬 간의 성분 단위 곱셈입니다. 일반적으로 옥타브에서 점(.)은 성분 단위의 연산을 의미합니다. 'A.^2' 명령어는 행렬 A의 각각 성분의 값을 제곱합니다. 1의 제곱은 1, 2의 제곱은 4, 등등입니다.


    

   Let's set v as a vector. Let's set v as one, two, three as a column vector. You can also do one dot over v to do the element-wise reciprocal of v, so this gives me one over one, one over two, and one over three, and this is where I do the matrices, so one dot over a gives me the element wise inverse of a. And once again, the period here gives us a clue that this an element-wise operation. We can also do things like log(v), this is a element-wise logarithm of the v E to the V is base E exponentiation of these elements, so this is E, this is E squared EQ, because this was V, and I can also do abs V to take the element-wise absolute value of V. So here, V was our positive, abs, minus one, two minus 3, the element-wise absolute value gives me back these non-negative values. And negative v gives me the minus of v. This is the same as negative one times v, but usually you just write negative v instead of -1*v. 


   v = [1;2;3]

   1 ./ v          % 벡터 v의 각 성분의 역수로 만든 벡터를 생성

  log(v)          % 벡터 v의 각 성분의 로그 값을 계산 

  exp(v)         % 벡터 v의 각 성분은 e(오일러의 수)의 거듭제곱을 한 값

   abs(v)         % 벡터 v의 각 성분의 절대값 

   abs([-1; 2; -3]) % 벡터의 각 성분의 절대값을 반환 

   -v                % 벡터 v의 각 성분에 음수를 곱함

   -1*v             % -v와 동일 


  이제 벡터 v를 하나 생성합니다. 'v = [1;2;3]' 명령어는 열 백터 v를 생성합니다. '1 ./ v' 명령어는 벡터 v의 각 성분을 역수로 만든 벡터를 얻습니다.  즉 벡터 v의 값은 [1/1, 1/2, 1/3]가 됩니다. 이걸 행렬에 적용하면 '1 ./ A' 명령어는 행렬 A의 각 성분의 역수가 구합니다. 다시 말해, 점(.)은 성분 단위로 연산합니다. 'log(v)' 명령어는 벡터 v의 성분 단위로 로그 값을 계산합니다. 'exp(v)'는 밑이 e(오일러의 수)이고 v의 성분으로 각각 거듭제곱한 값입니다. 그래서 e의 1승, e의 2승, e의 3승이 됩니다. 왜냐하면 원래 v값이 1 2 3이었이기 때문입니다. 또한, 'abs(v)' 명령어는 v의 성분 단위의 절댓값을 구할 수 있습니다. v는 원래 양수였이므로 'abs([-1; 2;-3]'명령어는 각 성분의 절댓값을 반환합니다. 결과는 [1;2;3]입니다. 음수 값에 대한 성분 단위의 절대값을 얻습니다. '-v' 명령어는 v의 값들에 대한 음수 값을 얻습니다. 이것은 '-1 * v ' 와 같지만 보통은= -1 * v 대신에 -v를 씁니다. 



   And what else can you do? Here's another neat trick. So, let's see. Let's say I want to take v an increment each of its elements by one. Well one way to do it is by constructing a three by one vector that's all ones and adding that to v.  So if I do that, this increments v by from 1, 2, 3 to 2, 3, 4. The way I did that was, length(v) is 3, so ones(length(v),1), this is ones of 3 by 1, so that's ones(3,1) on the right and what I did was v plus ones v by one, which is adding this vector of our ones to v, and so this increments v by one, and another simpler way to do that is to type v plus one. So she has v, and v plus one also means to add one element wise to each of my elements of v. 


   v = [1;2;3];

   length(v);                % 벡터 v의 행의 길이를 반환

   ones(length(v),1).  % ones(3,1) 과 동일

   v + ones(3,1)           % 벡터 v의 각 성분에 1을 더함

   v + 1                         % v + ones(3,1)과 동일


   또 어떤 것들이 가능할까요? 또 다른 트릭이 있습니다. v의 각 원소들이 1씩 증가한 값을 계산할 것입니다. 한 가지 방법은 모든 원소가 1인 3 X 1 행렬을 만들고 v와 더합니다. 즉, 벡터 v의 값은 [1;2;3]의 값을 [2; 3; 4]로 바꾸는 것입니다. 'length(v)'의 값은 3입니다. 'ones(length(v),1)'은 ones(3,1)이 되고, ones(3,1)은 우측에 배치합니다.  따라서 'v + ones(3,1)'은 벡터 v와 모든 원소가 1인 벡터를 더하는 것입니다. 즉, 벡터 v는 1씩 증가합니다. 또 다른 방법으로는 'v + 1' 명령어는 v의 각 성분 단위에서 1을 더하는 것입니다. 



   Now, let's talk about more operations. So here's my matrix A, if you want to buy A transposed, the way to do that is to write A prime, that's the apostrophe symbol, it's the left quote, so it's on your keyboard, you have a left quote and a right quote. So this is actually the standard quotation mark. Just type A transpose, this gives me the transpose of my matrix A. And, of course, A transpose, if I transpose that again, then I should get back my matrix A.

 

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

   A'                              % 행렬 A의 전치

   (A')'                                   % 행렬 A와 동일


   다른 연산에 대해 알아봅니다. 행렬 A가 있습니다. A' 명령어는 행렬 A의 전치 행렬을 구합니다.  여기서 ' 는 시작할 때 쓰는 왼쪽 작은따옴표입니다. 키보드에 왼쪽 작은따옴표와 오른쪽 작은따옴표가 있습니다.  

이게 일반적인 표기 방법입니다. 그냥 A'라고 치면 A의 전치 행렬을 구해줍니다. 물론, A의 전치 행렬을 

다시 전치시키면, 원래의 행렬 A가 만들어집니다. 



   Some more useful functions. Let's say lower case a is 1 15 2 0.5, so it's 1 by 4 matrix. Let's say val equals max of A this returns the maximum value of A which in this case is 15 and I can do val, ind max(a) and this returns val and ind which are going to be the maximum value of A which is 15, as well as the index. So it was the element number two of A that was 15 so ind is my index into this. Just as a warning, if you do max(A), where A is a matrix, what this does is this actually does the column wise maximum. 


   a = [1, 15, 2, 0.5];

   val = max(a)               % 벡터 a의 성분 중 최대값을 반환

   [val, ind] = max(a)     % 벡터 a의 최대값과 인덱스를 반환 

   max(A)                        % 행렬 A의 열 단위 최대값을 반환 


   또 다른 몇 가지 유용한 함수들이 있습니다. 소문자 a = [1, 15, 2, 0.5]라는 1x4 행렬이 있다고 가정합니다. 'val = max(a)' 명령어는 벡터 a의 행렬 성분 중에서 최대값을 반환합니다. 여기서는 15입니다. '[val, ind] = max(a)' 명령어는 벡터 a의 최대값과 인덱스 값을 반환합니다. 여기서는 val = 15, ind = 2입니다.'max(A)' 명령어는 행렬 A의 열 단위의 최대값을 구합니다. 주의할 점입니다. 여기서는 5와 6의 값을 돌려줍니다.



   But say a little more about this in a second. Still using this example that there for lowercase a. If I do a < 3, this does the element wise operation. Element wise comparison, so the first element of A is less than three so this one. Second element of A is not less than three so this value says zero cuz it's false. The third and fourth elements of A are less than three, so that's just 1 1. So that's the element-wise comparison of all four elements of the variable a < 3. And it returns true or false depending on whether or not there's less than three. Now, if I do find(a < 3), this will tell me which are the elements of a, the variable a, that are less than 3, and in this case, the first, third and fourth elements are less than 3. 


   a = [1, 15, 2, 0.5];

   a < 3                       % 벡터 a의 각 성분과 비교하여 참 또는 거짓을 반환  

   find (a < 3)              % 벡터 a의 각 성분과 비교하여 참인 성분의 인덱스를 반환


   

   이제 다른 연산을 알아봅시다. 벡터 a를 예로 들겠습니다. 'a < 3' 명령어는 벡터 a의 성분 단위로 비교하여 참 또는 거짓의 값을 반환합니다. 벡터 a의 첫 번째 성분의 값 1이 3보다 작으므로 진실인 1로 표시합니다. 벡터 a의 두 번째 성분의 값 15는 3보다 작지 않으므로 거짓인 0으로 표시합니다. 벡터 a의 3번째 성분의 값 2와 4번째 성분의 값 0.5는 3보다 작으므로 각각 1 1로 표시합니다. 이것이 a < 3에 대한 성분 단위 비교 방법입니다. 3보다 작은지 아닌지에 따라 참 또는 거짓을 반환합니다. 'find (a < 3)' 명령어는 벡터 a의 성분들의 값 중에서 3보다 작은 값들이 있는 벡터의 성분의 위치인 인덱스를 반환합니다. 여기서는 1번째 3번째 및 4번째 성분입니다.  



   For our next example, let me set a to be equal to magic(3). The magic function returns, let's type help magic. The magic function returns these matrices called magic squares. They have this, you know, mathematical property that all of their rows and columns and diagonals sum up to the same thing. So, you know, it's not actually useful for machine learning as far as I know, but I'm just using this as a convenient way to generate a three by three matrix. And these magic squares have the property that each row, each column, and the diagonals all add up to the same thing, so it's kind of a mathematical construct. I use this magic function only when I'm doing demos or when I'm teaching octave like those in, I don't actually use it for any useful machine learning application. 


   A = magic(3)            % 3 X 3 행렬의 마방진 생성 


   다음 예제입니다. 'A = magic(3)'명령어는 3 X 3 행렬의 마방진을 만듭니다. 'help magic' 명령어는 마방진에 대한 설명을 표시합니다.  magic 함수는 마방진 (magic squares) 행렬을 생성합니다. 마방진의 수학적인 성질은 모든 행과 열, 대각선의 합계가 같습니다. 머신 러닝 분야에서는 쓸모가 없지만, magic() 함수는 3 X 3 정방 행렬을 간편하게 생성하는 방법입니다. 마방진은 각 행과, 열, 대각선의 합이 모두 같기 때문에 일종의 수학적인 생성 방법 중 하나입니다. 저는 magic 함수를 설명할 때나 옥타브를 가르칠 때만 사용합니다. 머신 러닝 분야에서 사용할 일은 없습니다.


   But let's see, if I type RC = find(A > 7) this finds All the elements of A that are greater than equal to seven, and so r, c stands for row and column. So the 1,1 element is greater than 7, the 3,2 element is greater than 7, and the 2,3 element is greater than 7. So let's see. The 2,3 element, for example, is A(2,3), is 7 is this element out here, and that is indeed greater than equal seven. By the way, I actually don't even memorize myself what these find functions do and what all of these things do myself. And whenever I use the find function, sometimes I forget myself exactly what it does, and now I would type help find to look at the document.


     [r.c] = find (A >= 7)      % 행렬 A의 모든 성분들 중에서 7 보다 크거나 같은 성분을 반환


   '[r.c] = find (A >= 7)' 명령어는 행렬 A의 모든 성분들 중에서 7 보다 크거나 같은 성분을 찾습니다.  r은 행(row), c는 열(column)을 뜻합니다. 출력 값은 (1,1), (3,2), (2,3) 성분의 값이 7보다 큽니다. A(2,3)의 값은 7이고, 7보다 크거나 같습니다. 사실 저는 사용법을 일일이 기억하지 않습니다. find 함수의 역할이 무엇인지를 다 기억할 수는 없습니다. 만일 정확한 사용법이 기억나지 않을 때는 'help find' 명령어를 사용합니다. 



    Okay, just two more things that I'll quickly show you. One is the sum function, so here's my a, and then type sum(a). This adds up all the elements of a, and if I want to multiply them together, I type prod(a) prod sends the product, and this returns the product of these four elements of A. Floor(a) rounds down these elements of A, so 0.5 gets rounded down to 0. And ceil, or ceiling(A) gets rounded up to the nearest integer, so 0.5 gets rounded up to 1. 


   a = [1, 15, 2, 0.5];


   sum(a)                % 벡터 a의 모든 성분을 합산

   prod(a)               % 벡터 a의 모든 성분을 곱셈

   floor(a)               % 벡터 a의 모든 성분의 값의 끝자리 내림, 0.5는 0

   ceil(a)                % 벡터 a의 모든 성분의 값을 올림, 05는 1


   이제 2가지만 더 알아봅니다. 하나는 sum() 함수입니다. 행렬 a가 있습니다. 'sum(a)' 명령어는 벡터 a의 모든 성분을 더합니다. 'prod(a)' 명령어는 벡터 a의 모든 성분을 곱합니다. 'floor(a)' 명령어는 벡터 a의 모든 성분의 값의 끝자리 내림합니다. 즉, 0.5를 내림하면 0입니다. 'ceil(a)'명령어는 벡터 a의 모든 성분의 값을 올림 합니다. 즉, 0.5를 올림 하면 1입니다.



   You can also, let's see. Let me type rand(3), this generates a three by three matrix. If i type max(rand(3), what this does is it takes the element-wise maximum of 3 random 3 by 3 matrices. So you notice all of these numbers tend to be a bit on the large side because each of these is actually the max of a element wise max of two randomly generated matrices. 


   rand(3)         % 랜덤 숫자를 가진 3 X 3 행렬을 생성

   max(rand(3), rand(3))  

                        % 랜던 숫잘르 가진 3 X 3 행렬 두 개에서 같은 위치의 성분 값 중 큰수를 반환  



   이런 함수도 있습니다. 'rand(3)' 명령어는 랜덤 숫자를 가진 3 X 3 행렬을 생성합니다. 'max(rand(3), rand(3))' 명령어는 랜덤 숫자를 가진 3 X 3 행렬 두 개에서 같은 위치의 성분들의 값 중에 더 큰 수를 반환합니다. 위의 행렬보다 숫자가 약간 더 커 보이는 이유입니다. 



   This is my magic number. This is my magic square, three by three A. Let's say I type max A, and then this will be a [],1, what this does is this texts the column wise maximum. So the max of the first column is 8, max of second column is 9, the max of the third column is 7. This 1 means to take the max among the first dimension of 8. In contrast, if I were to type max A, this funny notation, two, then this takes the per row maximum. So the max of the first row is eight, max of second row is seven, max of the third row is nine, and so this allows you to take maxes either per row or per column. And remember the default's to a column wise element. So if you want to find the maximum element in the entire matrix A, you can type max(max(A)) like so, which is 9. Or you can turn A into a vector and type max(A(:)) like so and this treats this as a vector and takes the max element of that vector.


   A = magic(3);

  max(A, [],1)         % 행렬 A에서 각 열의 최대값을 반환 , 디폴트 값

   max(A, [],2)         % 행렬 A에서 각 행의 최대값을 반환 

  max(A(:))              % 행렬 A를 벡터로 변환 후 최대값을 반환


   이것은 제가 사용하는 매직 넘버입니다. 이것은 마방진 3 X 3 행렬 A입니다. 'max(A, [],1)' 명령어는 행열 A에서 각 열의 최대값을 반환합니다. 그러므로 첫 번째 열의 최대값인 8, 두 번째 열은 9, 3번째 열은 7입니다. 1의 의미는 8이 속한 열의 성분들 값 중에서 8이 최대 값이라는 의미입니다. 반대로, 재밌는 표기법이지만 'max(A, [],2)' 명령어는 행렬 A에서 각 행의 최대값을 각각 구합니다. 첫 번째 행의 최대값은 8, 두 번째 행은 7, 세 번째 행은 9입니다. 이것이 각 행, 또는 각 열의 최대값을 구하는 방법입니다. 그리고 기억해두세요, dafault 값은 열 단위로 최대값을 구합니다. 그래서 'max(max(A))' 명령어는 행렬 A의 모든 성분의 값 중에서 최대값을 구합니다. 여기서는 9입니다. 또는 'A(:)'은 행렬 A의 모든 성분을 벡터로 변환합니다. 'max(A(:))' 명령어는 행렬 A를 벡터로 변환 후에 최대값을 구합니다. 즉, 해당 벡터의 최대값을 구합니다. 

     



   Finally let's set A to be a 9 by 9 magic square. So remember the magic square has this property that every column and every row sums the same thing, and also the diagonals, so just a nine by nine matrix square. So let me just sum(A, 1). So this does a per column sum, so we'll take each column of A and add them up and this is verified that indeed for a nine by nine matrix square, every column adds up to 369, adds up to the same thing. Now let's do the row wide sum. So the sum(A,2), and this sums up each row of A, and indeed each row of A also sums up to 369. Now, let's sum the diagonal elements of A and make sure that also sums up to the same thing. So what I'm gonna do is construct a nine by nine identity matrix, that's eye nine. And let me take A and construct, multiply A element wise, so here's my matrix A. I'm going to do A .^ eye(9). What this will do is take the element wise product of these two matrices, and so this should Wipe out everything in A, except for the diagonal entries. And now, I'm gonna do sum sum of A of that and this gives me the sum of these diagonal elements, and indeed that is 369. 


    A = magic(9);

    sum(A,1)          % 행렬 A의 열 단위 성분들의 합을 반환

    sum(A,2)          % 행렬 A의 행 단위 성분들의 합을 반환 

   sum(sum(A .* eye(9)))  % 명령어는 대각선 성분들의 합을 반환. 


   마지막으로, 행렬 A가 9 X 9 마방진 행렬입니다. 마방진은 모든 행과 열, 대각선의 합계가 같습니다. 'A = magic(9)'명령어로 9 X 9 마방진을 생성합니다. 'sum(A, 1)' 명령어는 행렬 A의 열 단위로 합을 반환합니다. 9 X 9 행렬에서 각각의 열의 합은 369로 같습니다. 행 단위의 총합을 구합니다. 'sum(A,2)' 명령어는 행렬 A의 행 단위로 합을 반환합니다. 각 행의 합은 369로 똑같습니다. 이제 대각선의 합을 구해서 합계가 같은 지를 진단합니다. 'eye(9)' 명령으로 9 X 9 항등 행렬을 생성합니다. 'A .* eye(9)' 명령어는 행렬 A와 항등 행렬을 각 성분 단위에서 곱셈을 수행합니다. 따라서, 행렬 A의 대각선의 성분들만 남고 나머지는 0의 값을 갖습니다. 'sum(sum(A.*eye(9)))' 명령어는 대각선에 위치한 모든 성분들의 합계를 구합니다. 여기서는 369입니다. 



   You can sum up the other diagonals as well. So this top left to bottom left, you can sum up the opposite diagonal from bottom left to top right. The commands for this is somewhat more cryptic, you don't really need to know this. I'm just showing you this in case any of you are curious. But let's see. Flipud stands for flip up down. But if you do that, that turns out to sum up the elements in the opposite. So the other diagram, that also sums up to 369. Here, let me show you. Whereas eye(9) is this matrix. Flipup(eye(9)), takes the identity matrix, and flips it vertically, so you end up with, excuse me, flip UD, end up with ones on this opposite diagonal as well.


   flipud(A)           % 행렬 A의 첫 번째 행을 마지막 행으로 뒤집고 순차적으로 진행


   반대쪽 대각선도 마찬가지로 합계를 구할 수 있습니다. 좌측 상단에서 우측 하단까지의 대각선의 합계를 구한 것처럼 좌측 하단에서 우측 상단까지의 대각선 합계도 구할 수 있습니다. 명령어는 다소 복잡합니다. 여러분의 호기심을 위해 풀어보겠습니다. 'flipud(A)' 명령어는 행렬 A의 첫 번째 행을 마지막 행으로 뒤집고 순차적으로 진행합니다. flipud() 함수를 사용하여 반대쪽 대각선의 성분들의 합계를 구합니다. 여기서는 369입니다. 'sum(sum(A.*flipud(eye(9))))' 명령어로 구합니다.



   Just one last command and then that's it, and then that'll be it for this video. Let's set A to be the three by three magic square game. If you want to invert a matrix, you type pinv(A). This is typically called the pseudo-inverse, but it does matter. Just think of it as basically the inverse of A, and that's the inverse of A. And so I can set temp = pinv(A) and temp times A, this is indeed the identity matrix, where it's essentially ones on the diagonals, and zeroes on the off-diagonals, up to a numeric round off.


   A = magic(3)

   temp = pinv(A)                   % 행렬 A의 역행렬을 생성

   temp * A                              % 행렬 A의 역행렬과 행렬 A를 곱셈하여 항등행렬 생성


   이제 명령어 하나만 남았습니다. ' A = magic(3) 명령어는 3 X 3 마방진 행렬 A를 생성합니다. 'pinv(A)' 명령어는 역행렬을 구합니다. 보통은 pseudo-inverse라고 부르지만 신경 쓸 필요 없습니다. 그냥 A의 역행렬일 뿐입니다. 'temp = pinv(A)'로 역행렬을 구한 후에 'temp * A를 계산하면 항등 행렬이 만들어집니다. 각 대각선은 1이고, 대각선이 아닌 부분은 0입니다. 실수로 반올림하면 모두 0입니다.


   So, that's it for how to do different computational operations on data and matrices. And after running a learning algorithm, often one of the most useful things is to be able to look at your results, so to plot or visualize your result. And in the next video, I'm going to very quickly show you how again with one or two lines of code using Octave. You can quickly visualize your data or plot your data and use that to better understand what you're learning algorithms are doing.


   지금까지 행렬과 벡터의 곱셈입니다. 데이터와 행렬 사이에 서로 다른 계산을 어떻게 수행하는지 살펴보았습니다. 학습 알고리즘을 실행하고 결과를 확인하는 가장 유용한 방법은 시각화하고 도식화하는 것입니다. 다음 강의에서는 옥타브 프로그램을 활용하여 빠르게 데이터를 시각화하고 도식화할 수 있는 한 두 줄의 코드로 실행하는 방법을 배울 것입니다. 학습 알고리즘이 어떻게 동작하는 지를 좀 더 잘 이해할 수 있습니다. 



앤드류 응의 머신러닝 동영상 강의 




정리하며 - 데이터 계산


1) 행렬 계산

    A .* B                     : 행렬 A와 행렬 B의 같은 위치의 성분끼리 곱셈

    A .^2                      : 행렬 A의 각 성분의 값에 제곱 

    1 ./ v                      : 벡터 v의 각 성분을 역수로 만든 벡터

     log(v)                     : 벡터 v의 성분 단위로 로그 값

    exp(v)                    : 밑이 e(오일러의 수)고 v의 성분으로 각각 거듭제곱

   abs(v)                     : v의 성분 단위의 절대값 

    -v                           : -1 * v와 동일

    v + 1                      : v의 각 성분 단위에서 1을 더함  



2) 행렬 관리

   max(a)                     : 벡터 a의 행렬 성분 중 최대값

   max(A,[],1)              : 행열 A에서 각 열의 최대값  

   max(A,[],2)              : 행렬 A에서 각 행의 최대값 

   max(A(:))                 : 행렬 A를 벡터로 변환 한 후에 최대값 

   A'                             : 행렬 A의 전치행렬

   a < 3                        : 벡터 a의 성분 단위로 비교하여 참 또는 거짓의 값을 반환

   find (a < 3)               : 벡터 a의 성분들 중 3보다 작은 값들의 이 있는 벡터의 성분의 위치 반환

   A = magic(4             : 마방진 행렬 반환 4 X 4 행렬

   [r.c] = find (A >= 7)  : 행렬 A의 모든 성분들 중에서 7 보다 크거나 같은 성분 위치 반환

   sum(a)                     : 벡터 a의 모든 성분을 더함

   sum(A, 1)                 : 행렬 A의 열 단위로 합계 

   prod(a)                    : 벡터 a의 모든 성분을 곱함

   floor(a)                    : 벡터 a의 모든 성분의 값의 끝자리 내림

   ceil(a)                      : 벡터 a의 모든 성분의 값을 올림

   rand(3)                    : 랜덤 숫자를 가진 3 X 3 행렬 생성

   max(rand(3),rand(3)) : 랜덤 숫자를 가진 3 X 3 행렬 두 개에서 성분의 각 값 중 큰 수를 반환 

    flipud(A                    : 행렬 A의 첫 번째 행을 마지막 행으로 뒤집고 순차적으로 진행

    pinv(A)                     : 행렬 A의 역행렬을 생성

매거진의 이전글 앤드류 응의 머신러닝 정리 (5-2):옥타브 데이터관리
작품 선택
키워드 선택 0 / 3 0
댓글여부
afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari