机器学习数据分析软件:Octave

最近学习了Octave数学软件,这个软件类似于MATLAB,但是操作要比MATLAB简单很多,上手快,对数据分析有很大的帮助,所以这里就简单的把部分练习的代码放在这里,如果你想实现一下,可以按照代码内容来,或者自己编写

GNU Octave, version 7.1.0
Copyright (C) 1993-2022 The Octave Project Developers.
This is free software; see the source code for copying conditions.
There is ABSOLUTELY NO WARRANTY; not even for MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  For details, type 'warranty'.

Octave was configured for "x86_64-w64-mingw32".

Additional information about Octave is available at https://www.octave.org.

Please contribute if you find this software useful.
For more information, visit https://www.octave.org/get-involved.html

Read https://www.octave.org/bugs.html to learn how to submit bug reports.
For information about changes from previous versions, type 'news'.

>> 1+1
ans = 2
>> 2_4
ans = 24
>> 2-4
ans = -2
>> 2*4
ans = 8
>> 2^3
ans = 8
>> 1 == 2 % false
ans = 0
>> 1 ~= 2
ans = 1
>> 1 && 0 % and
ans = 0
>> 1 || 0 % or
ans = 1
>> xor(1,0)
ans = 1
>> a = 3;
>> disp(a)
3
>> disp(sprintf('2 decimals: %0.2f',a))
2 decimals: 3.00
>> format long
>> a
a = 3
>> b=pi;
>> b
b = 3.141592653589793
>> format short
>> b
b = 3.1416


>> 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:0.1:2
v =

 Columns 1 through 10:

    1.0000    1.1000    1.2000    1.3000    1.4000    1.5000    1.6000    1.7000    1.8000    1.9000

 Column 11:

    2.0000

>> v = 1:6
v =

   1   2   3   4   5   6

>> ones(2,3)
ans =

   1   1   1
   1   1   1

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

   2   2   2
   2   2   2
   2   2   2

>> zeros(1,3)
ans =

   0   0   0

>> rand(1,5)
ans =

   0.388980   0.041283   0.117873   0.078976   0.599495

>> randn(2,3) %gaosi
ans =

   0.814927   0.041009  -0.066768
  -0.392299  -0.322274   0.081502




>> w = -6 + sqrt(10)*(randn(1,10000));
>> hist(w)
>> hist(w,100)
>>  eye(10) %E function
ans =

Diagonal Matrix

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

>> size(A)
ans =

   3   2

>> size(A, 1)
ans = 3
>> length(A)
ans = 3
>> pwd
ans = C:\Users\Administrator
>> ls
 AppData
'Application Data'
 Contacts
 Cookies
 Desktop
 Documents
 Downloads
 Favorites
 IntelGraphicsProfiles
 Links
'Local Settings'
 Music
'My Documents'
 NTUSER.DAT
 NTUSER.DAT{95de2aa9-7b20-11e9-87bd-fbd6aa594282}.TM.blf
 NTUSER.DAT{95de2aa9-7b20-11e9-87bd-fbd6aa594282}.TMContainer00000000000000000001.regtrans-ms
 NTUSER.DAT{95de2aa9-7b20-11e9-87bd-fbd6aa594282}.TMContainer00000000000000000002.regtrans-ms
 NetHood
 Pictures
 PrintHood
 PycharmProjects
 Recent
'Saved Games'
 Searches
 SendTo
 Templates
 Videos
 ntuser.dat.LOG1
 ntuser.dat.LOG2
 ntuser.ini
''$'\343\200\214\345\274\200\345\247\213\343\200\215\350\217\234\345\215\225'
>> cd C:/Users/Administrator/Desktop
>> load data1.txt
error: load: unable to find file data1.txt
>> load data1.dat
>> load('data1.dat')
>> who
Variables visible from the current scope:

A      a      ans    b      data1  v      w

>> whos
Variables visible from the current scope:

variables in scope: top scope

  Attr   Name        Size                     Bytes  Class
  ====   ====        ====                     =====  =====
         A           3x2                         48  double
         a           1x1                          8  double
         ans         1x22                        22  char
         b           1x1                          8  double
         data1      97x2                       1552  double
         v           1x6                         24  double
         w           1x10000                  80000  double

Total is 10230 elements using 81662 bytes

>> size(data1)
ans =

   97    2
   
>> clear w
>> who
Variables visible from the current scope:

A      a      ans    b      data1  v

>> v = data1(1:10)
v =

   6.1101   5.5277   8.5186   7.0032   5.8598   8.3829   7.4764   8.5781   6.4862   5.0546

>> save hello.mat data1.dat
warning: save: no such variable 'data1.dat'
>> save a.txt v -ascii




>> A(3,2)
ans = 6
>> A(2,:)
ans =

   3   4

>> A([1 3],:)
ans =

   1   2
   5   6

>> A
A =

   1   2
   3   4
   5   6

>> A(:,2)=[10,11,12]
A =

    1   10
    3   11
    5   12

>> A = [A,[100,101,102]]
error: horizontal dimensions mismatch (3x2 vs 1x3)
>> A = [A,[100;101;102]]
A =

     1    10   100
     3    11   101
     5    12   102

>> A(:)
ans =

     1
     3
     5
    10
    11
    12
   100
   101
   102

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

   1   2
   3   4

>> B = [5 6;7 8]
B =

   5   6
   7   8

>> C = [A B]
C =

   1   2   5   6
   3   4   7   8

>> C = [A; B]
C =

   1   2
   3   4
   5   6
   7   8

>> size(C)
ans =

   4   2

>> size(C,2)
ans = 2
>> size(C,1)
ans = 4
>> A * B
ans =

   19   22
   43   50

>> A .* B
ans =

    5   12
   21   32

>> A .^ 2
ans =

    1    4
    9   16

>> 1 ./ A
ans =

   1.0000   0.5000
   0.3333   0.2500

>> log(A)
ans =

        0   0.6931
   1.0986   1.3863

>> exp(A)  % ex
ans =

    2.7183    7.3891
   20.0855   54.5982

>> abs(A) % 求绝对值
ans =

   1   2
   3   4

>> -A
ans =

  -1  -2
  -3  -4

>> A + ones(length(B),1)
ans =

   2   3
   4   5

>> A
A =

   1   2
   3   4

>> A + 1
ans =

   2   3
   4   5

>> A' % zhuanzhi
ans =

   1   3
   2   4

>> max(A)
ans =

   3   4

>> [val, ind]=max(A)
val =

   3   4

ind =

   2   2

>> find(A<3)
ans =

   1
   3

>> A =magic(6)
A =

   35    1    6   26   19   24
    3   32    7   21   23   25
   31    9    2   22   27   20
    8   28   33   17   10   15
   30    5   34   12   14   16
    4   36   29   13   18   11

>> [r , c] = find(A <= 3)
r =

   2
   1
   3

c =

   1
   2
   3

>> A
A =

   35    1    6   26   19   24
    3   32    7   21   23   25
   31    9    2   22   27   20
    8   28   33   17   10   15
   30    5   34   12   14   16
    4   36   29   13   18   11

>> sum(A)
ans =

   111   111   111   111   111   111

>> prod(A) % product
ans =

   3.1248e+06   1.4515e+06   2.7332e+06   3.1856e+07   2.9733e+07   3.1680e+07

>> floor(A)  % 向下取整
ans =

   35    1    6   26   19   24
    3   32    7   21   23   25
   31    9    2   22   27   20
    8   28   33   17   10   15
   30    5   34   12   14   16
    4   36   29   13   18   11

>> ceil(A) % 向上取整
ans =

   35    1    6   26   19   24
    3   32    7   21   23   25
   31    9    2   22   27   20
    8   28   33   17   10   15
   30    5   34   12   14   16
    4   36   29   13   18   11

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

   0.8349   0.4311   0.8388
   0.8539   0.5075   0.7513
   0.9351   0.6399   0.6021

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

   35   36   34   26   27   25

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

   35
   32
   31
   33
   34
   36

>> max(max(A))
ans = 36
>> max(A(:))
ans = 36
>>
>> sum(A,1)
ans =

   111   111   111   111   111   111

>> sum(A,2)
ans =

   111
   111
   111
   111
   111
   111

>> sum(sum(A.*eye(6)))
ans = 111
>> sum(sum(A.*flipud(eye(6))))
ans = 111
>> flipud(eye(6))
ans =

Permutation Matrix

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

>> pinv(A)  % 伪逆矩阵
ans =

   1.1532e-02  -3.8622e-02   2.5421e-02   5.3595e-03  -1.3931e-02   1.9248e-02
   2.2731e-03  -2.0103e-02   1.6162e-02   1.4619e-02  -3.2449e-02   2.8508e-02
  -8.1436e-03   3.0447e-03  -1.5088e-02   4.2021e-03   2.7736e-02  -2.7423e-03
   5.3971e-02  -4.1708e-02  -1.5849e-03   6.0143e-02  -6.6400e-02   4.5879e-03
  -4.0937e-02   2.3106e-02   2.8508e-02  -5.3282e-02   3.5452e-02   1.6162e-02
  -9.6868e-03   8.3292e-02  -4.4409e-02  -2.2032e-02   5.8600e-02  -5.6755e-02

>> inv(A)
warning: matrix singular to machine precision, rcond = 4.80096e-18
ans =

   3.7530e+14   4.7714e-02  -3.7530e+14  -3.7530e+14   9.3750e-02   3.7530e+14
   3.7530e+14   6.6233e-02  -3.7530e+14  -3.7530e+14   6.2500e-02   3.7530e+14
  -1.8765e+14  -4.0123e-02   1.8765e+14   1.8765e+14  -1.5625e-02  -1.8765e+14
  -3.7530e+14  -1.2804e-01   3.7530e+14   3.7530e+14  -1.5625e-01  -3.7530e+14
  -3.7530e+14  -6.3230e-02   3.7530e+14   3.7530e+14  -6.2500e-02  -3.7530e+14
   1.8765e+14   1.2646e-01  -1.8765e+14  -1.8765e+14   1.0938e-01   1.8765e+14

>> pinv(A)*A 
ans =

   0.777778  -0.222222   0.111111   0.222222   0.222222  -0.111111
  -0.222222   0.777778   0.111111   0.222222   0.222222  -0.111111
   0.111111   0.111111   0.944444  -0.111111  -0.111111   0.055556
   0.222222   0.222222  -0.111111   0.777778  -0.222222   0.111111
   0.222222   0.222222  -0.111111  -0.222222   0.777778   0.111111
  -0.111111  -0.111111   0.055556   0.111111   0.111111   0.944444

>>
>>
>> t = [1 2 3 4 5 6 7 8 9]
t =

   1   2   3   4   5   6   7   8   9

>> y1= sin(pi*4*t)
y1 =

 Columns 1 through 7:

  -4.8984e-16  -9.7969e-16  -1.4695e-15  -1.9594e-15  -2.4492e-15  -2.9391e-15  -3.4289e-15

 Columns 8 and 9:

  -3.9187e-15  -4.4086e-15

>> plot(t,yi)
error: 'yi' undefined near line 1, column 8
>> plot(t,y1)
>> hold on;
>> plot(t,y1,'r')
>> xlabel('time')
>> ylabel('value')
>> print -dpng 'myFirstPlot.png' % 图片储存
>> close
>> figure(1);plot(t,y1)
>> figure(2);plot(t,y1)
>> subplot(1,2,1)
>> plot(t,y1)
>> subplot(1,2,2)
>> plot(t,y1)
>> axis([0.5 1 -1 1])
>> iamgesc(A)
error: 'iamgesc' undefined near line 1, column 1
>> imagesc(A)
>> imagesc(A),colorbar,colormap gray;
>> close



>> pwd
ans = C:\Users\Administrator\Desktop
>> squarThisNumber(10)
error: 'squarThisNumber' undefined near line 1, column 1
>> squareThisNumber(10)
ans = 100

squareThisNumber.m函数:
注意:文件的命名应该和函数名相同,否则无法调用函数!!

function y = squareThisNumber(x)

y = x^2;

costFunctionJ.m函数:

function j = costFunctionJ(X, y, theta)

% X 是设计函数,就是训练样本数
% y 分类标签

m = size(X, 1);  % 计算训练样本个数(行数)
prediction = X * theta;
sqrErrors = (prediction - y).^2;

J = 1/(2*m)*sum(sqrErrors); %主函数

在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jackson的生态模型

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值