octave 机器学习
Octave is an open-source high-level programming language designed to perform efficient numerical computations solving linear and non-linear equations. Moreover, Octave gives the user the ability to use statistics and data analysis.
Octave是一种开放源代码的高级编程语言,旨在执行解决线性和非线性方程式的高效数值计算。 此外,Octave使用户能够使用统计数据和数据分析。
Due to its fast developing time and easy to learn syntax, Octave is one of the most used programming languages for Machine Learning along with Python and R.
由于其快速的开发时间和易于学习的语法,Octave是与Python和R一起用于机器学习的最常用编程语言之一。
Being a more productive language, it is a good choice for students and beginners in ML. In general, people tend to only prototype in Octave and only after succeeding, proceed into larger-scale implementations of the algorithms in languages like C++ and Java, or any other low-level languages which are more efficient than Octave when time is concerned.
作为一种生产力更高的语言,对于ML的学生和初学者来说,它是一个不错的选择。 通常,人们倾向于仅在Octave中进行原型制作,并且只有在成功之后才可以使用C ++和Java之类的语言或在时间上比Octave更高效的任何其他低级语言进行算法的大规模实现。
Now I am going to present everything you need to know in order to master Octave and use it for implementing your models in Machine Learning.
现在,我将介绍您需要掌握的所有知识,以掌握Octave并将其用于在Machine Learning中实现模型。
基本操作 (Basic Operations)
This section is intended to give you foundational knowledge about logic operations, assigning variables, and other simple concepts to prepare you for more complex operations.
本部分旨在为您提供有关逻辑运算,分配变量和其他简单概念的基础知识,以使您为更复杂的运算做准备。
快速笔记 (Quick note)
Indentation is not necessary in Octave.
在八度中不需要缩进。
Equal and not equal
平等 与 不平等
1 == 1 % equal 1 ~= 2 % not equal
Comment
评论
% this is a comment in Octave
And operator and Or operator
和 运算符和 或 运算符
1 && 2 % and operator 1 || 2 % or operator
Assign a variable
分配一个变量
a = 76 % assign variable a an integer
b = 'Octave' % assign variable b a string
Print with decimal places
用 小数位 打印
disp(sprintf('2 decimals: %0.2f', a)) % print variable a with 2 %decimalsdisp(sprintf('6 decimals: %0.6f', a)) % print variable a with 6 %decimals
Format of decimal number
十进制数字 格式
format short % this is the default format
format long % displays more decimals
Matrices and vectors
矩阵 和 向量
% In Octave the semicolon implies go to the next lineA = [1 2; 3 4; 5 6]; % this is a 3x2 matrixv = [1 2 3]; % this is a row vector or a 1x3 matrixx = [1; 2; 3] % this is a column vector or a 3x1 matrix
Special matrices
特殊 矩阵
A = ones(2,3); % creates a matrix of size 2x3 filled with 1B = zeros(1,3); % creates a matrix of size 1x3 filled with 0 C = rand(3,4); % returns a matrix with random elements between (0,1)D = randn(1,3); % Gaussian-distribution matrixE = eye(3,3); % returns the identity matrix
Incrementing
增量式
v = 1:0.1:2; % start from 1, increment with 0.1, up to 2v = 1:6; % start from 1, increment with 1, up to 6
Histogram
直方图
x =
hist(x); % draws a histogram of x
移动数据 (Moving Data Around)
Here we will discuss how to play with the variables and data learnt above.
在这里,我们将讨论如何使用上面学习的变量和数据。
A = [1 2; 3 4; 5 6];
Get matrix dimensions
获取矩阵 尺寸
size(A) % returns the dimension of matrix 3x2
size(A, 1) % returns the first dimension of matrix
size(A, 2) % returns the second dimension of matrix
length(A) % returns the longest dimension
Load file
载入 档案
load file.dat % loads a specific file in the current directory
Show variables in current workspace
在当前工作空间中 显示变量
who % show all the variables declared in the current scope
whos % for a more detailed version of who
Get data from specific file
从特定文件 获取数据
v = cost(1:10);
Save file
储存 档案
save helloWorld.txt v -ascii % saves a file in the current directory
Access elements in matrix
矩阵中的 访问 元素
A(1, 2) % gives the number in row 1, column 2
A(2, :) % gives all row 2
A(: ,2) % gives all column 2
Replace elements
替换 元素
A(: ,2) = [100; 101; 102] % replace elements in column 2
Concatenate matrices
串联 矩阵
A = [1 2; 3 4; 5 6];
B = [7 8; 9 10; 11 12];
C = [A B] % concatenate horizontally
D = [A;B] % concatenate vertically
计算数据 (Computing Data)
Now that you know the basic principles in Octave, it is time to learn how to compute different variables and functions.
既然您已经了解了Octave的基本原理,就该学习如何计算不同的变量和函数了。
A = [1 2; 3 4; 5 6];
B = [7 8; 9 10; 11 12];
C = [1 1; 2 2];
Multiply matrices
乘法 矩阵
A * C % multiplies matrix A with matrix C
% you should pay attention if the matrices can be multiplied
Element-wise operations
按元素 操作
A .^ 2 % squares each cell in matrix A
log(A) % logs each cell in matrix A
exp(A) % exponentiates each cell in matrix A
abs(A) % absolute value of each cell in matrix A
A + 1 % add numbers to each cell in matrix A
Transpose matrix
转置 矩阵
A' % transposes matrix A
Maximum value of matrix
矩阵 最大值
max(A) % does column-wise maximum
Mathematical operations on matrix
矩阵的 数学运算
sum(A) % sum of all elements in matrix A
prod(A) % product of all elements in matrix A
floor(A) % round down of all elements in matrix A
ceil(A) % round up of all elements in matrix A
Create random matrix
创建 随机矩阵
rand(3) % creates a 3x3 random matrix
Convert matrix to vector
将 矩阵 转换 为矢量
A(:) % convert matrix A to a vector
Inverse matrix
逆 矩阵
pinv(A) % invert matrix A
绘制数据 (Plotting Data)
An important part in developing good algorithms in ML is plotting data. By visualizing your data set and model, you could get a better idea of what needs improvement.
开发ML中好的算法的重要部分是绘制数据。 通过可视化数据集和模型,您可以更好地了解需要改进的地方。
t = [0:0.01:0.98];
y1 = sin(2 * pi * 4 * t);
plot(t, y1); % plots function y1 with the help of thold on; % allows you to print 2 graphs togethery2 = cos(2 * pi * 4 * t);
plot(t, y2) % plots function y2 with the help of t
Label the axis of a plot
标记图 的轴
xlabel('time') % names the x-axis of the plot
ylabel('value') % names the y-axis of the plotlegend('sin', 'cos') % creates a legend of the plot
title('trigonometric plot') % sets the title o the plot
Save the plot
保存 情节
rint -dpng 'trigoPlot.png' % saves the plot as an image
Get rid of figure
摆脱 数字
close % gets rid of the current figure
Multiple figures at the save time
节省 多个数字
figure(1); plot(t, y1) % plots the first figure
figure(2); plot(t, y2) % plots the second figure
Change the ranges on the axis
更改 轴上 的范围
axis([0.5 1 -1 1]) % x-axis : 0.5 to 1 y-axis : -1 to 1
Clear figure
清晰的 身影
clf % deletes the figure from memory
控制声明 (Control Statements)
In any programming language it is necessary to know how to use control statements as it can ease you work and make you more efficient.
在任何编程语言中,都有必要知道如何使用控制语句,因为它可以简化您的工作并提高您的效率。
For loop
对于 循环
for i = 1:20,
v(i) = (i + 1) * 2;
end;
While loop
While 循环
i = 1;
while i <= 5,
x(i) = i * 100;
i = i + 1;
end;
If and else
如果 和 其他
v(1) = 100;
if v(1) == 99,
disp('This statement is false');
elseif v(1) == 100,
disp('This statement is true');
else v(1) == 101,
disp('This statement is false');
end;
If you are not entirely sure what a specific command does or you want to learn more about how it works, you can type help name_of_command to see all the information regarding that command.
如果您不能完全确定特定命令的功能,或者想了解有关该命令如何工作的更多信息,可以键入help name_of_command来查看有关该命令的所有信息。
You can find the full documentation and download Octave from here.
您可以找到完整的文档并从此处下载Octave。
资源资源 (Resources)
[1]Andrew Ng, Machine Learning, Machine Learning course on Coursera.
翻译自: https://levelup.gitconnected.com/develop-machine-learning-algorithms-with-octave-dbf10cf9caf3
octave 机器学习