機器學習筆記6 —— Matlab編程基礎
本章節主要用在Matlab編程時一些指令的方便查找。
顯示命令:
pwd %顯示當前路徑nls %顯示當前路徑的文件夾ncd path %打開該路徑nwho %查看當前路徑的所有變數nwhos %更詳細地顯示變數的名稱、維度和數據精度nclear %清除所有變數naddpath(path) %將路徑添加到當前搜索處n
基本運算:
a+b %加法na-b %減法na*b %乘法na/b %除法na^b %冪指數nlog(a) %對數運算 nexp(a) %指數運算nabs(a) %取絕對值運算n
邏輯運算:
1 == 1 % 判斷是否相等nans = 1 %Truen1 == 0 nans = 0 %False n1 ~= 0 % 判斷是否不相等nans = 1n1 && 0 %AND (和運算)nans = 0n1 || 0 %OR(或運算)nans = 1 nxor(1,0) %異或運算nans = 1 n
賦值:
a = 3; % 賦值數字n%假如我們不想讓其輸出可以在後面加上分號(;)nb = Hello world; %賦值字元串n%更複雜的輸入我們可以使用:ndisp(a)ndisp(sprintf(2 decimals :%0.2f,a)) %sprintf列印字元串n
向量和矩陣:
A = [1,2; 3,4; 5,6;] %建立矩陣nA =n 1 2n 3 4n 5 6nv = [1,2,3] %建立向量nv =n 1 2 3n%向量另一種表示nv = 1:0.1:2 %a:x:b表示[a,b]區間,x表示遞增尺度nnv =n 1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000 1.7000 1.8000 1.9000 2.0000n%其它生成矩陣的方法:nones(2,3) %生成2*3的元素都為1的矩陣nans =n 1 1 1n 1 1 1nzeros(1,3) %生成1*3的零矩陣nans =n 0 0 0nrand(1,3) %生成1*3的隨機數矩陣nans =nn 0.9134 0.6324 0.0975neye(2,3) %生成2*3的單位矩陣nans =n 1 0 0n 0 1 0neye(3)nnans =n 1 0 0n 0 1 0n 0 0 1nnA = [1,2; 3,4; 5,6;];nsize(A) %計算矩陣的維度nans =n 3 2nsize(A,1) %顯示該矩陣的行數nans =n 3nsize(A,2)%顯示該矩陣的列數nans =n 2nv = [1,2,3]; % 計算向量的維度nlength(v)nans =n 3nA(1,2) %索引矩陣第1行第2列的元素nans =n 2nA(1,:) %索引矩陣第1行所有元素nans =n 1 2nA(:,1) %索引矩陣第1列所有元素nans =n 1n 3n 5nA([1 3],:) %索引矩陣第1行和第3行所有元素nans =n 1 2n 5 6nA = [A,[7;8;9]] %加入一列新的向量nA =n 1 2 7n 3 4 8n 5 6 9nA(:) %將矩陣A的元素變成列向量nans =n 1n 3n 5n 2n 4n 6n 7n 8n 9nA = [1,2;3,4]; %合併矩陣nB = [4,5;6,7];nC = [A B]nC =n 1 2 4 5n 3 4 6 7nC = [A;B]nC =n 1 2n 3 4n 4 5n 6 7nA*B %矩陣相乘nans =n 16 19n 36 43nA.*B %矩陣對應元素相乘(兩個矩陣需是相同維度)nans =nn 4 10n 18 28nA.^2 %矩陣每個元素作平方處理nans =n 1 4n 9 16n1./A %矩陣每個元素取倒數nans =n 1.0000 0.5000n 0.3333 0.2500nA %轉置矩陣nans =n 1 3n 2 4nA = magic(3) %生成每一行每一列相加起來都是相等的矩陣nA =n 8 1 6n 3 5 7n 4 9 2n
數據處理:
%裝載文件nload xxx %xxx為你當前路徑的文件名nload (xxx) %以字元串的形式輸入nv = xxx(1:10) %建立向量v,元素分別為xxx文件內1~10的數據n%將當前的數據存儲nsave xxx.mat n
畫圖:
%plot nt = [0:0.01:0.98];ny1 = sin(2*pi*4*t);ny2 = cos(2*pi*4*t);nplot(t,y1);nhold on; %在原來的圖像中畫圖nplot(t,y2);nxlabel(time); %X軸單位nylabel(value); %Y軸單位nlegend(sin,cos); %將兩條曲線表示出來ntitle(myplot); %加入標題nprint -dpng myplot.png; %輸出圖片為png格式nclose %關閉n
figure(1);plot(t,y1); %分開畫圖nfigure(2);plot(t,y2);nnsubplot(1,2,1); %將圖像分為1*2的格子,使用第1個nplot(t,y1);nsubplot(1,2,2); %將圖像分為1*2的格子,使用第2個nplot(t,y2);naxis([0.5 1 -1 1]); %改變軸的刻度 x軸變為[0.5,1] y軸變為[-1,1]n
控制語句:
%for:n v = zeros(5,1);nfor i = 1:5, n v(i) = 2^i;nend; %注意以end為結尾標誌nans:nv =n 2n 4n 8n 16n 32nn%while:ni = 1;nwhile i <= 3;n v(i) = 6;n i = i + 1;nend;nans:nv =n 6n 6n 6n 16n 32nn%break:ni = 1 ;nwhile true,n v(i) = 7;n i = i + 1;n if i==3,n break;n end;nend;nans:nv =n 7n 7n 2n 1n 1nn%if-else:nif v(1)== 1,n disp(I);nelseif v(1) == 7,n disp(love); nelse n disp(you);nend;nans:n loven
計算代價函數 :
%首先新建一個.m 文件,用來定義代價函數:nfunction J = costFunctionJ(X,y,theta)n% X is the "design matrix" containing our training examples;n%y is the class labelsnnm = size(X,1); %number of training examplesnpredictions = X * theta; %predictions of hypothesis on all m examplesnsqrErrors = (predictions - y) .^2; %squared errorsnnJ = 1/(2 * m) * sum(sqrErrors);n %——————————————————————————————————————————————————————————————————%nX = [1 1;1 2;1 3];ny = [1;2;3];ntheta = [0;1];nj = costFunctionJ(X,y,theta);nans: %剛好擬合nj =n 0n theta = [0;0];nj = costFunctionJ(X,y,theta);nans: nj =n 2.3333n%驗證一下:n(1^2+2^2+3^2)/(2*3)nans =n 2.3333n
矢量的表示:
線性回歸方程: (筆記4的推導)
%unvectorized implementation 非矢量化表示nprediction = 0.0 ; nfor j = 1 : n + 1,n prediction = prediction + thrta(j) * x(j)nend;n%vectorized implementation 矢量表示nprediction = theta * x;n
下面是關於本章節的題目:
題目1
Suppose I first execute the following in Octave/Matlab:
A = [1 2; 3 4; 5 6]; % 3*2矩陣nB = [1 2 3; 4 5 6]; % 2*3矩陣n
Which of the following are then valid commands? Check all that apply. (Hint: A denotes the transpose of A.)
A.C = A + B; B.C = B * A; C.C = A + B; D.C = B * A; Answer:A、B分析:相同維度的矩陣才能夠相加。相乘的條件是A*B(A的列=B的行)題目2Let Which of the following indexing expressions gives ? Check all that apply.
A.B = A(:, 1:2);B.B = A(1:4, 1:2);C.B = A(0:2, 0:4)D.B = A(1:2, 1:4);Answer:A、B分析:請看上面向量和矩陣那塊。題目3Let be a 10x10 matrix and be a 10-element vector. Your friend wants to compute the product and writes the following code:
v = zeros(10, 1); nfor i = 1:10n for j = 1:10n v(i) = v(i) + A(i, j) * x(j);n end;nend;n
How would you vectorize this code to run without any FOR loops? Check all that apply.
A.v = A * x;
B.v = Ax;C.v = A .* x;D.v = sum (A * x);Answer:A分析:看起來好像不知道怎麼下手,我們先按題目條件列出已知條件: , 。然後我們跟著程序走一遍:當 ,, ,
一直這樣累加下去,這不就是我們矩陣*向量的結果嗎?所以答案選A。題目4Say you have two column vectors and , each with 7 elements (i.e., they have dimensions 7x1). Consider the following code:
z = 0;nfor i = 1:7n z = z + v(i) * w(i)nend;n
Which of the following vectorizations correctly compute ? Check all that apply.
A.z = sum (v .* w);B.z = v * w;C.z = v * w;D.z = v .* w;Answer:A、B分析:
之前一直以為A是不對的,所以搞了這麼久。這個程序無非就是兩個7 x 1 的向量一個倒置跟另一個相乘,最後變成一個1 x 1的數。這裡要注意倒置的順序,不能將其變成7 x 7。題目5In Octave/Matlab, many functions work on single numbers, vectors, and matrices. For example, the sin function when applied to a matrix will return a new matrix with the sin of each element. But you have to be careful, as certain functions have different behavior. Suppose you have an 7x7 matrix . You want to compute the log of every element, the square of every element, add 1 to every element, and divide every element by 4. You will store the results in four matrices, . One way to do so is the following code:
for i = 1:7n for j = 1:7n A(i, j) = log(X(i, j));n B(i, j) = X(i, j) ^ 2;n C(i, j) = X(i, j) + 1;n D(i, j) = X(i, j) / 4;n end;nend;n
Which of the following correctly compute , , , or ? Check all that apply.
A.C = X + 1;B.D = X / 4;C.A = log (X);D.B = X ^ 2;Answer:A、B、C分析:
B = X .^ 2例子:
X=[1,2,3;4,5,6;7,8,9]nfor i = 1:3n for j = 1:3n B(i, j) = X(i, j) ^ 2;n end;nend;nnB =n 1 4 9n 16 25 36n 49 64 81n
注意了,是點乘,就是矩陣裡面每一個數字分別進行平方。
筆記整理自Coursera吳恩達機器學習課程。
避免筆記的冗雜,翻閱時不好找,所以分成幾個部分寫,有興趣的同學可以關注一下其它的筆記。
機器學習筆記1 —— 機器學習定義、有監督學習和無監督學習
機器學習筆記2 —— 線性模型、價值函數和梯度下降演算法
機器學習筆記3 —— 線性代數基礎
機器學習筆記4 —— 多特徵量線性回歸
機器學習筆記5 —— 正規方程
推薦閱讀: