Octave 機器學習常用命令
A、Basic operations and Moving data around
1. 在命令列模式用shift + 斷行符號即可附加下一行輸出
2. length命令apply到矩陣時返回較高的一維的dimension
3. help + 命令是顯示命令的簡要協助資訊
4. doc + 命令 是顯示命令的詳細協助文檔
5. who 命令 顯示 當前所有建立的變數
6. whos 命令 顯示當前所有建立變數的詳細資料
7. 儲存變數到.mat 檔案save hello.mat b 以二進位壓縮儲存資料
8. save hello.txt v -ascii 以可讀形式檔案儲存 即文字格式設定
9. :means every elements in this col
10. A([1 3], : ) 擷取第 1、3兩行所有列的資料
11. A = [A, [100; 101; 102]] 在A矩陣後面加一列col vector [100,101,102]
12. size(A) 返回一個1行2列矩陣 表明第1和第2個dimensional 的大小
13. C = [A B]等價於C = [A, B] []為向後面的列添加,串連兩個矩陣 [] 為concat 串連矩陣或者字串
14. C= [A; B] ;號表示向下面行添加,因此會增加相應行數,列數不變
B、Computing on data
1. A.*B是矩陣/向量點乘 A*B是矩陣相乘
2. log(v) 和exp(v)求以e為底的對數和指數
3. abs()求絕對值
4. A‘ 求A的轉置矩陣
5. max函數返回矩陣中最大元素的值和索引 [val, ind] = max(a)
6. A < 3 會判斷A當中的每一個是否小於3,若小於3,對應位置返回true,否則對於位置返回false
7. find(A<3) 返回矩陣中所有值小於3的索引
8. [r, c] = find(A >= 7) 傳回值大於等於7的element的row及col的索引
9. prod(a) 求矩陣a裡面所有元素的乘積
10. floor(a) 對矩陣a中元素向下取整
11. ceil(a)對矩陣a中元素向上取整
12. rand(3) 產生3X3的隨機方陣
13. max(A,[],1) 求矩陣A的每一列的最大值(最後一維是1表明為dimension 1)
14. max(A,[],2) 求矩陣A的每一行的最大值
15. sum(A, 1) 對矩陣A第一維度(即每列)求和(注意matlab中第一維預設是列,然後是行,再然後依次類推。。。)
16. sum(A, 2) 對矩陣A第二維度(即每行)求和
17. sum(sum(A.*eye(9))) 求矩陣A的對角線元素之和
18. 矩陣翻轉操作 flipud Flip matrix in up/down direction. 將矩陣上下翻轉, 類似還有左右翻轉 fliplr, rot90, flipdim. flipud(X) returns X with columns preserved and rows flipped in the up/down direction.
19. pinv(A) 及inv(A) 求矩陣A 的逆矩陣
C、Plotting data
· t = [0.1 : 0.01 : 0.98]; y = sin(t); plot(t, y) 畫正弦曲線
· hold on; 保留當前曲線,畫下一條曲線
· xlabel 標定x軸說明
· legend('sin','cos') 添加圖例
· title('my plot') 添加圖片標題
· print -dpng 'myPlot.png' 儲存圖片
· 線條顏色標註控制
b blue . point - solid
g green o circle : dotted
r red x x-mark -. dashdot
c cyan + plus -- dashed
m magenta * star (none) no line
y yellow s square
k black d diamond
w white v triangle (down)
^ triangle (up)
< triangle (left)
> triangle (right)
p pentagram
h hexagram
· subplot 畫子圖,即將多個圖合成一個圖
· axis([0.5 1 -1 1]) 設定x軸範圍為0.5~1,y軸範圍為-1~1
· clf Clear current figure.
· imagesc(A) imagesc Scale data and display as image. 把矩陣A畫成彩色小方格
· imagesc(A), colorbar, colormap gray; colorbar 顯示色彩坡形條,表明顏色含義; colormap 設定colormap性質 即RGB三色 A color map matrix may have any number of rows, but it must have exactly 3 columns. Each row is interpreted as a color, with the first element specifying the intensity of red light, the second green, and the third blue. Color intensity can be specified on the interval 0.0 to 1.0.
· a = 1; b = 2; c=3; 不會打出a b c的值; a = 1, b = 2, c=3 會打出a b c 的值
D、Control statements: for, while, if statements
for 迴圈
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
for i = 1:10, v(i) = 2 ^i; end; >> v v = 2 4 8 16 32 64 128 256 512 1024 >> indices = 1:10 for i = indices, disp(i) end; |
while 迴圈
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
>> while i <= 5, v(i) = 100; i = i+1; end; >> v v = 100 100 100 100 100 64 128 256 512 1024 >> i = 1; >> while true, v(i) = 999; i = i + 1; if i == 6, break; end; end; >> exit v = 999 999 999 999 999 64 128 256 512 1024 |
if elseif 判斷分支語句
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
v(1)=2 v = 2 999 999 999 999 64 128 256 512 1024 >> if v(1) == 1, disp('The value is one'); elseif v(1) == 2, disp('The value is two'); else disp('The value is not one or two.'); end; The value is two |
函數的定義和使用
定義函數 squareThisNumber.m檔案
| 1 2 3 |
function y = squareThisNumber(x) y = x^2; |
調用
| 1 2 3 |
squareThisNumber(5) ans = 25 |
· addpath 增加matlab搜尋函數的路徑
· matlab裡面定義的函數可以返回多於一個值, 這是其與C C++等程式設計語言的不同之處,C\C++裡面的函數有唯一傳回值可以允許多個傳回值可以帶來編程上的方便
| 1 2 3 |
function [a,b] = squareAndCubeThisNumber(x) a = x^2; b = x^3; |
調用
| 1 2 3 4 5 |
>> [x1,x2] = squareAndCubeThisNumber(5) x1 = 25 x2 = 125 |
cost function J 函數樣本
| 1 2 3 4 5 6 7 8 9 10 11 |
function J = costFunctionJ(X, y, theta) % X is the "design matrix" containing our training examples. % Y is the class labels m = size(X,1); % number of training examples predictions = X*theta; % predictions of hypothesis on all m examples sqrErrors = (predictions - y).^2; % squared errors J = 1/(2*m) * sum(sqrErrors); |
調用
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
X = [1 1; 1 2; 1 3]; y = [1; 2; 3]; theta = [0;1]; >> j = costFunctionJ(X, y, theta) j = 0 % squared errors is 0 in this example >> theta = [0;0] theta = 0 0 >> j = costFunctionJ(X, y, theta) j = 2.3333 % squared errors is 2.3333 in this example % which is (1^2 + 2^2 + 3^2) / (2 * 3) = 2.3333 |