机器学习 Machine Learning- 吴恩达Andrew Ng Week2-Octave

Posted 架构师易筋

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了机器学习 Machine Learning- 吴恩达Andrew Ng Week2-Octave相关的知识,希望对你有一定的参考价值。

Coursera、YouTube课程地址

因为Coursera的课程还有考试和论坛,后续的笔记是基于Coursera (2021-05-22)
https://www.coursera.org/learn/machine-learning/home/welcome

https://www.youtube.com/playlist?list=PLOXON7BTL9IW7Ggbc09jLqGmzkwPI4-3V
截止2021-5-3, 有112 个视频
在这里插入图片描述

1. 第26课 working on and submitting programming exercises - Octave Tutorial

  1. cd 到代码文件 m1-class-ex1;
  2. 运行函数 warmUpExercise()
  3. submit() 提交到系统,后面输入用户名、密码
    在这里插入图片描述
  4. Enter your choice [1-9] : 1 (填写1)
  5. 输入邮箱
  6. 输入密码

会告诉你结果正确与否,如果错误需要修正后重新提交。
在这里插入图片描述

密码可以在如下网站动态生成,或者设置为固定的。

在这里插入图片描述

2. Octave Basic Operations

课程联系的指令如下

5+6
3-2
5*8
12
1/2
2^6
1 == 2 % false
1 ~= 2
1 && 0 % and
1 || 0
xor(1,0)
PS1('>>> ');
a = 3
a = 3; % semicolon supressing output
b = 'hi';
b
c = (3>=1);
c
a=pi;
a
disp(a);
disp(sprintf('2 decimals: %0.2f', a))
disp(sprintf('6 decimals: %0.6f', a))
format long
a
format short
a
A = [1 2; 3 4; 5 6]
A = [1 2;
3 4;
5 6]
v = [1 2 3]
v = [1; 2; 3]
a = 1:0.1:2
v = 1:6
ones(2,3)
C = 2*ones(2,3)
C = [2 2 2; 2 2 2]
w = ones(1,3)
w = zeros(1,3)
w = rand(1,3)
rand(3, 3)
rand(3, 3)
w = randn(1, 3)
w = -6 + sqrt(10)*(randn(1,10000))
hist(w)
hist(w, 50)
eye(4)
I = eye(6)
eye(3)
help eye  % q if you want to quit
help rand
help help

hist(w) 二维图形表示数据方位,x轴表示数值,y轴表示数值的个数。
在这里插入图片描述
hist(w, 50)矩阵更粗放一点
在这里插入图片描述

  1. Octave Moving Data Around
    资源下载:https://github.com/tuanavu/coursera-stanford
    例子文件下载:https://github.com/tuanavu/coursera-stanford/tree/master/machine_learning/lecture/week_2/v_octave_tutorial_week_2
    课程运行的命令如下:
A
A = [1 2; 3 4; 5 6]
size(A)
sz = size(A)
size(sz)
sizae(A, 1)
size(A,1)
size(A,2)
v = [1 2 3 4]
length(v)
length(A)
length([1;2;3;4;5])
pwd
ls
ls
cd week2
ls
load featuresX.dat 
load priceY.dat 
load('featuresX.dat')
who
featuresX
size(featuresX)
size(priceY)
priceY
whos
clear featuresX
whos
v = priceY(1: 10)
save hello.mat v;
ls
clear
whos
who
load hello.mat 
who
v
save hello.txt v -ascii
A = [1 2; 3 4; 5 6]
A(3,2)
A(2,:)
A(:,2)
A([1 3],:)
A
A([1 3],:)
A
A(:,2)
A(:,2) = [10;11;12]
A = [A, [100; 101; 102]];
A
[100; 101; 102]
size(A)
A(:)
A = [1 2; 3 4; 5 6]
B = [11 12; 13 14; 15 16]
C = [A B]
C = [A ; B]
  1. Octave: Computing on Data
    因为下面的计算命令,方便查阅,所以把命令和结果记录如下
>> A = [1 2; 3 4; 5 6]
A =

   1   2
   3   4
   5   6

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

   11   12
   13   14
   15   16

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

   1   1
   2   2

>> A * C
ans =

    5    5
   11   11
   17   17

>> A . * B
error: parse error:

  syntax error

>>> A . * B
        ^
>> A .* B
ans =

   11   24
   39   56
   75   96

>> A
A =

   1   2
   3   4
   5   6

>> B
B =

   11   12
   13   14
   15   16

>> A .* B
ans =

   11   24
   39   56
   75   96

>> A
A =

   1   2
   3   4
   5   6

>> A .^ 2
ans =

    1    4
    9   16
   25   36

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

   1
   2
   3

>> 1 ./ v
ans =

   1.0000
   0.5000
   0.3333

>> 1 ./ A
ans =

   1.0000   0.5000
   0.3333   0.2500
   0.2000   0.1667

>> log(v)
ans =

        0
   0.6931
   1.0986

>> exp(v)
ans =

    2.7183
    7.3891
   20.0855

>> v
v =

   1
   2
   3

>> 10 ^ 0.6931
ans = 4.9329
>> 2 ^ 0.6931
ans = 1.6168
>> log(v)
ans =

        0
   0.6931
   1.0986

>> 2 ^ 1.0986
ans = 2.1415
>> exp
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
        LC_ALL = (unset),
        LC_NUMERIC = "C",
        LC_TIME = "C",
        LANG = "en_CN.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
error: Invalid call to exp.  Correct usage is:

 -- exp (X)

Additional help for built-in functions and operators is
available in the online version of the manual.  Use the command
'doc <topic>' to search the manual index.

Help and information about Octave is also available on the WWW
at https://www.octave.org and via the help@octave.org
mailing list.
>> 2.71828 ^ 1.0986
ans = 3.0000
>> exp(v)
ans =

    2.7183
    7.3891
   20.0855

>> v
v =

   1
   2
   3

>> abs(v)
ans =

   1
   2
   3

>> abs([-1; 2; -3])
ans =

   1
   2
   3

>> -v
ans =

  -1
  -2
  -3

>> v
v =

   1
   2
   3

>> v + ones(length(v), 1)
ans =

   2
   3
   4

>> length(v)
ans = 3
>> ones(3,1)
ans =

   1
   1
   1

>> v + ones(3, 1)
ans =

   2
   3
   4

>> v + 1
ans =

   2
   3
   4

>> v
v =

   1
   2
   3

>> v + 1
ans =

   2
   3
   4

>> A
A =

   1   2
   3   4
   5   6

>> A
A =

   1   2
   3   4
   5   6

>> A'
ans =

   1   3   5
   2   4   6

>> (A')'
ans =

   1   2
   3   4
   5   6

>>
>>
>> a = [1 15 2 0.5]
a =

    1.0000   15.0000    2.0000    0.5000

>> val = max(a)
val = 15
>> [val, ind] = max(a)
val = 15
ind = 2
>> max(A)
ans =

   5   6

>> A
A =

   1   2
   3   4
   5   6

>> a
a =

    1.0000   15.0000    2.0000    0.5000

>> a < 3
ans =

  1  0  1  1

>> find(a < 3)
ans =

   1   3   4

>> A = magic(3)
A =

   8   1   6
   3   5   7
   4   9   2

>> help magic
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
        LC_ALL = (unset),
        LC_NUMERIC = "C",
        LC_TIME = "C",
        LANG = "en_CN.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
'magic' is a function from the file /Applications/Octave-6.2.0.app/Contents/Resources/usr/Cellar/octave-octave-app@6.2.0/6.2.0/share/octave/6.2.0/m/special-matrix/magic.m

 -- magic (N)

     Create an N-by-N magic square.

     A magic square is an arrangement of the integers '1:n^2' such that
     the row sums, column sums, and diagonal sums are all equal to the
     same value.

     Note: N must be a scalar greater than or equal to 3.  If you supply
     N less than 3, magic returns either a nonmagic square, or else the
     degenerate magic squares 1 and [].

Additional help for built-in functions and operators is
available in the online version of the manual.  Use the command
'doc <topic>' to search the manual index.

Help and information about Octave is also available on the WWW
at https://www.octave.org and via the help@octave.org
mailing list.
>> [r, c] = find(A >= 7)
r =

   1
   3
   2

c =

   1
   2
   3

>> A(2,3)
ans = 7
>> help find
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
        LC_ALL = (unset),
        LC_NUMERIC = "C",
        LC_TIME = "C",
        LANG = "en_CN.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
'find' is a built-in function from the file libinterp/corefcn/find.cc

 -- IDX = find (X)
 -- IDX = find (X, N)
 -- IDX = find (X, N, DIRECTION)
 -- [i, j] = find (...)
 -- [i, j, v] = find (...)
     Return a vector of indices of nonzero elements of a matrix, as a
     row if X is a row vector or as a column otherwise.

     To obtain a single index for each matrix element, Octave pretends
     that the columns of a matrix form one long vector (like Fortran
     arrays are stored).  For example:

          find (eye (2))
            => [ 1; 4 ]

     If two inputs are given, N indicates the maximum number of elements
     to find from the beginning of the matrix or vector.

     If three inputs are given, DIRECTION should be one of "first" or
     "last", requesting only the first or last N indices, respectively.
     However, the indices are always returned in ascending order.

     If two outputs are requested, 'find' returns the row and column
     indices of nonzero elements of a matrix.  For example:

          [i, j] = find (2 * eye (2))
              => i = [ 1; 2 ]
              => j = [ 1; 2 ]

     If three outputs are requested, 'find' also returns a vector
     containing the nonzero values.  For example:

          [i, j, v] = find (3 * eye (2))
                 => i = [ 1; 2 ]
                 => j = [ 1; 2 ]
                 => v = [ 3; 3 ]

     Note that this function is particularly useful for sparse matrices,
     as it extracts the nonzero elements as vectors, which can then be
     used to create the original matrix.  For example:

          sz = size (a);
          [i, j, v] = find (a);
          b = sparse (i, j, v, sz(1), sz(2));

     See also: nonzeros.

Additional help for built-in functions and operators is
available in the online version of the manual.  Use the command
'doc <topic>' to search the manual index.

Help and information about Octave is also available on the WWW
at https://www.octave.org and via the help@octave.org
mailing list.
>> sum(a)
ans = 18.500
>> a
a =

    1.0000   15.0000    2.0000    0.5000

>> prod(a)
ans = 15
>> floor(a)
ans =

    1   15    2    0

>> cel(a)
error: 'cel' undefined near line 1, column 1
>> ceil(a)
ans =

    1   15    2    1

>> rand(3)
ans =

   0.5767   0.6445   0.7137
   0.2410   0.5332   0.3078
   0.2649   0.8331   0.6533

>> max(rand(3), rand(3))
ans =

   0.4343   0.9327   0.5560
   0.6521   0.9895   0.4188
   0.5169   0.6980   0.8203

>> A
A =

   8   1   6
   3   5   7
   4   9   2

>> max(A, [], 1)
ans =

   8   9   7

>> max(A, [], 2)
ans =

   8
   7
   9

>> max(A)
ans =

   8   9   7

>> max(max(A))
ans = 9
>> A(:)
ans =

   8
   3
   4
   1
   5
   9
   6
   7
   2

>> A = magic(9)
A =

   47   58   69   80    1   12   23   34   45
   57   68   79    9   11   22以上是关于机器学习 Machine Learning- 吴恩达Andrew Ng Week2-Octave的主要内容,如果未能解决你的问题,请参考以下文章

机器学习- 吴恩达Andrew Ng Week10 知识总结 Large scale machine learning

机器学习- 吴恩达Andrew Ng Week6 知识总结 Machine Learning System Design

机器学习 Machine Learning- 吴恩达Andrew Ng 第11~15课总结

机器学习 Machine Learning- 吴恩达Andrew Ng 第21~25课总结

机器学习 Machine Learning- 吴恩达Andrew Ng Week2-Octave

机器学习 Machine Learning- 吴恩达Andrew Ng 第16~20课总结