1. 程式人生 > >吳恩達機器學習(第2周--Octave/Matlab Tutorial)【下】

吳恩達機器學習(第2周--Octave/Matlab Tutorial)【下】

第2周--Plotting Data

>> t = [0:0.01:0.98];

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

>> plot(t,y1)
>> y1 = cos(2*pi*4*t);
>> plot(t,y2)
error: 'y2' undefined near line 1 column 8
>> y1 = sin(2*pi*4*t);
>> y2 = cos(2*pi*4*t);
>> plot(t,y2)
>>
>> plot(t,y1)
>> hold on
>> plot(t,y2,'r')
>> xla
error: 'xla' undefined near line 1 column 1
>> xlabel('time')
>> ylabel('value')
>> legend('sin','cos')
>> title('my plot')
>> print -dpng 'myPlot.png'
>> close
>> figure(1);plot(t,y1);
>> figure(2);plot(t,y2);
>> subplot(1,2,1); % Divides the plot a 1x2 grid, access first element
>> plot(t,y1);
>> subplot(1,2,2);
>> plot(t,y2);
>>
>> axis([0.5 1 -1 1])
>> clf;
>> A = magic(5)
A =

   17   24    1    8   15
   23    5    7   14   16
    4    6   13   20   22
   10   12   19   21    3
   11   18   25    2    9

>> imagesc(A)
>> imagesc(A), colorbar,colormap gray;
>> imagesc(magic(15)), colorbar,colormap gray;
>>
>> a = 1, b = 2, c = 3
a =  1
b =  2
c =  3
>> a = 1; b = 2; c = 3;
>>

第2周--Control Statements: for, while, if statement

>> v = zeros(10,1)
v =

   0
   0
   0
   0
   0
   0
   0
   0
   0
   0

>> for i = 1:10
v(i) = 2*i;
end;
>> v
v =

    2
    4
    6
    8
   10
   12
   14
   16
   18
   20

>> for i = 1:10
v(i) = 2^i;
end;
>> v
v =

      2
      4
      8
     16
     32
     64
    128
    256
    512
   1024

>> indices = 1:10;
>> indices
indices =

    1    2    3    4    5    6    7    8    9   10

>> for i = 1:indices
disp(i);
end;
 1
>> i = 1;
>> 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
>> v
v =

    999
    999
    999
    999
    999
     64
    128
    256
    512
   1024

>> v(1)
ans =  999
>> v(1) =2;
>> 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
>>
>>
>> edit squareThisNumber(x).m

>> squareThisNumber(5)
ans =  25
>> edit squareAndCubeThisNumber.m

>> [a,b] = squareAndsquareAndCubeThisNumber(5);
error: 'squareAndsquareAndCubeThisNumber' undefined near line 1 column 8
>> [a,b] = squareAndCubeThisNumber(5);
>> a
a =  25
>> b
b =  125
>> x = [1 1;1 2; 1 3]
x =

   1   1
   1   2
   1   3

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

   1
   2
   3

>> theta = [0;1];
## Copyright (C) 2018 Administrator
## 
## This program is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
## 
## This program is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see
## <https://www.gnu.org/licenses/>.


## -*- texinfo -*- 
## @deftypefn {} {@var{retval} =} costFunctionJ (@var{input1}, @var{input2})
##
## @seealso{}
## @end deftypefn


## Author: Administrator <
[email protected]
> ## Created: 2018-07-02 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   predications = X*theta; %predications of hypothesis on all m   % examples   sqrErrors = (predications - y) .^2; % squared errors      J = 1/(2*m) * sum(sqrErrors);    endfunction
>> edit costFunctionJ.m

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

   1   1
   1   2
   1   3

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

   1
   2
   3

>> theta = [0;1];
>> j = costFunctionJ(X,y,theta)
j = 0
>> theta = [0;0];
>> j = costFunctionJ(X,y,theta)
j =  2.3333
>> (1^2 + 2^2 +3^2)/(2*3)
ans =  2.3333
>>

第2周--Vectorization






相關推薦

機器學習2--Octave/Matlab Tutorial

第2周--Plotting Data>> t = [0:0.01:0.98]; >> y1 = sin(2*pi*4*t); >> plot(t,y1) >> y1 = cos(2*pi*4*t); >> plo

Coursera--機器學習-5筆記Neural Networks——Learning

Week 5 —— Neural Networks : Learning 目錄 一代價函式和反向傳播 1-1 代價函式 首先定義一些我們需要使用的變數: L =網路中的總層數 sl =第l層中的單位數量(不

Coursera--機器學習-11筆記應用例項:photo OCR

Week 11 ——Application Example: Photo OCR 目錄 影象OCR(Optical Character Recognition) 1-1 問題描述 在這一段介紹一種 機器學習的應用例項 照片OCR技術

機器學習十四章---無監督學習kmeans演算法

一、kmeans演算法 Kmeans演算法的流程: 1.根據我們要分的類別數,就是你要將資料分成幾類(k類),隨機初始化k個點(暫且稱為類別點) 2.計算每個資料點到k個類別點的距離,將其歸類到距離最近的那個類別點 3.計算每一類中包含的資料點的位置的平均值,比如,包含a(x1,y1

機器學習十三章---支援向量機SVM

一、優化目標 邏輯迴歸中的代價函式:  畫出兩種情況下的函式影象可得: y=1: 我們找一條折線來近似表示這個函式影象 y=0:    我們用這兩條折線來近似表示原來的曲線函式可得新的代價函式(假設-log(h(x))為,-log(1

機器學習十章---神經網路的反向傳播演算法

一、簡介 我們在執行梯度下降的時候,需要求得J(θ)的導數,反向傳播演算法就是求該導數的方法。正向傳播,是從輸入層從左向右傳播至輸出層;反向傳播就是從輸出層,算出誤差從右向左逐層計算誤差,注意:第一層不計算,因為第一層是輸入層,沒有誤差。 二、如何計算 設為第l層,第j個的誤差。

機器學習九章---神經網路

神經網路是非線性的分類演算法。模擬人類的神經系統進行計算。 1、原因 當特徵數很大的時候(比如100個),那麼在假設函式的時候要考慮太多項,包含x1x2,x1x3,x2x3等等,不能僅僅單個考慮x1,x2等,這樣一來,在擬合過程中的計算量就會非常大。 2、基本概念 其中,藍色的

機器學習八章---正則化

在我們擬合的時候,根據我們選擇函式的不同可能會出現欠擬合,擬合程度較好,過擬合。 1.欠擬合和過擬合        欠擬合,上面第一張圖就是欠擬合情況,欠擬合表現為所選的函式沒有很好的擬合所給的資料,從影象上看就是很多資料都不在函式上,偏

機器學習七章---邏輯迴歸

一、邏輯迴歸 邏輯迴歸通俗的理解就是,對已知類別的資料進行學習之後,對新得到的資料判斷其是屬於哪一類的。 eg:對垃圾郵件和非垃圾郵件進行分類,腫瘤是惡性還是良性等等。 1.為什麼要用邏輯迴歸: 對於腫瘤的例子:  在外面不考慮最右邊的樣本的時候我們擬合的線性迴歸

機器學習五章--特徵縮放和學習率

一、特徵縮放  ----(1) 對於我們假設的式子(1),可能存在這樣一種情況就是有些資料遠大於另一些資料(eg:x_1>>x_2) 比如房子價格的例子: 房子的面積要遠大於房子的層數和房間數。在這種情況下可以看下圖,所產生的等高線的圈會很窄,在做梯度下降

機器學習十五章---降維PCA

一、目標 1.資料壓縮 在機器學習中,會用到大量資料進行學習訓練,當資料量很大,維度高時,對電腦記憶體的壓力會很大,並且學習的速度也會很慢。 2.視覺化 我們將一些高維的資料降維到1維,2維,3維的話,就可以進行視覺化,將資料以圖表的方式展示出來。 二、主成分分析方法 主成分

Coursera-AndrewNg()機器學習筆記——

訓練 ros 方便 font 就是 梯度下降 全局最優 用法 郵件 一.邏輯回歸問題(分類問題) 生活中存在著許多分類問題,如判斷郵件是否為垃圾郵件;判斷腫瘤是惡性還是良性等。機器學習中邏輯回歸便是解決分類問題的一種方法。二分類:通常表示為y?{0,1},0:“Negat

Coursera--機器學習-程式設計練習8異常檢測和推薦系統對應9課程

exercise 8 —— 異常檢測和推薦系統 在本練習中,第一部分,您將實施異常檢測演算法並將其應用於檢測網路上發生故障的伺服器。 在第二部分中,您將使用協作過濾來構建電影的推薦系統。 1 異常檢測 在這個練習中,您將實現一個異常檢測演算

Coursera--機器學習-程式設計練習7K均值和PCA對應8課程

exercise 7 —— K-means and PCA 在本練習中,您將實現K均值聚類演算法並將其應用於壓縮影象。 在第二部分中,您將使用主成分分析來查詢面部影象的低維表示。 1 K-means 先從二維的點開始,使用K-means進行分類

機器學習十六機器學習流水線、上限分析

目錄 0. 前言 1. 流水線 2. 上限分析(Ceiling analysis) 學習完吳恩達老師機器學習課程的照片OCR,簡單的做個筆記。文中部分描述屬於個人消化後的理解,僅供參考。 如果這篇文章對你有一點小小的幫助,請給個關注

機器學習第一章

機器學習可以分為監督學習和無監督學習兩大類。 一、監督學習 監督學習就是在資料中有特定標註,會對資料進行分類,比如癌症例子。監督學習得到的結果可以預測某個新資料對應的結果(線性迴歸)或是該資料屬於哪一類(邏輯迴歸)。比如癌症的預測,房價的預測等。 圈和叉就分別標識兩類資料,即在學習

演算法工程師修仙之路:機器學習

吳恩達機器學習筆記及作業程式碼實現中文版 神經網路引數的反向傳播演算法 代價函式 假設神經網路的訓練樣本有 m

演算法工程師修仙之路:機器學習

吳恩達機器學習筆記及作業程式碼實現中文版 第六章 神經網路學習 特徵和直觀理解 從本質上講,神經網路能夠通過學習得出其自身的一系列特徵。 神經網路中,單層神經元( 無中間層)的計算可用來表示邏輯運算,比如邏輯與(AND)、邏輯或(OR)。 邏輯

演算法工程師修仙之路:機器學習

吳恩達機器學習筆記及作業程式碼實現中文版 第六章 神經網路學習 非線性假設 無論是線性迴歸還是邏輯迴歸都有這樣一個缺點:當特徵太多時,計算的負荷會非常大。 使用非線性的多項式項,能夠幫助我們建立更好的分類模型。假設我們有非常多的特徵,例如大於 100 個

演算法工程師修仙之路:機器學習

吳恩達機器學習筆記及作業程式碼實現中文版 第五章 正則化 過擬合問題 線性迴歸和邏輯迴歸能夠有效地解決許多問題,但是當將它們應用到某些特定的機器學習應用時,會遇到過擬合(over-fitting)的問題,可能會導致它們效果很差。 正則化(regulari