套裝分析工具 MATLAB 進階工具箱 Fuzzy 系統工具箱 FIS / ANFIS toolbox 資料庫工具箱 Database Toolbox 統計工具箱 Statistics Toolbox 系統整合工具箱 JavaBuilder 開放原始碼工具 R software Python QGIS 1
MATLAB Fuzzy-related toolbox FIS Fuzzy Inference System Fuzzy Logic Toolbox software provides command-line functions and an app for creating Mamdani and Sugeno fuzzy systems ANFIS Adaptive Neuro-Fuzzy Inference System ANFIS is a FIS using neuro-adaptive learning methods in training neural networks. 其它 Open Source 應用軟體工具 Python: Scikit-Fuzzy Java: jfuzzy R CRAN: sets, frbs, anfis 2
Fuzzy Logic 模糊集合理論 (fuzzy set theory), 簡稱模糊理論 以量化方法處理模糊概念的學問 起源於 Lotfi A. Zadeh 教授於 1965 年發表在 Information and Control 的著名論文 "Fuzzy Sets" 模糊概念 : 這兩本書很類似 ; 他開車有點快 ; 這張照片拍得相當漂亮等 歸屬函數 (membership function, mf) 定義個體與集合的歸屬程度, 其值是 0 到 1 之間的任何值 當個體屬於集合的程度越大時, 其歸屬程度就越接近於 1, 反之則越接近於 0 利用歸屬程度 (degree) 取代 0 與 1 的二分法概念 利用歸屬函數定義輸入及輸出之特徵變數 (feature) 模糊規則 (fuzzy rules) If-Then control 假如輸入特徵為歸屬函數 x, 則輸出特徵為歸屬函數 y 3
Membership Function Display degree of feature variable by function X axis: feature range (distribution) Y axis: degree Degree between 0 and 1 Ex: Which are the weekend days? The multi-value for weekend-ness if degrees of the truth with fuzzy in-between values 4
Fuzzification 模糊化 : 將特徵變數 (Feature) 賦予數個歸屬函數成為模糊集 (Fuzzy Set) 5
Piece-wise linear functions Triangular: trimf(x,[a b c]) Ex: x = 0:0.1:10; y = trimf(x,[3 6 8]); Trapezoidal: trapmf(x,[a b c d]) Ex: x = 0:0.1:10; y = trapmf(x,[1 5 7 8]); 6
Gaussian distribution function Gaussian: gaussmf(x,[sig c]) Ex: x = 0:0.1:10; y = gaussmf(x,[2 5]); Gaussian combination: gauss2mf(x,[sig1 c1 sig2 c2]) Ex: x=0:0.1:10; y=gauss2mf(x,[2 4 1 8]); Generalized bell-shaped: gbellmf(x,params) Ex: x = 0:0.1:10; y = gbellmf(x,[2 4 6]); 7
Notes on gauss2mf: x = [0:0.1:10]'; y1 = gauss2mf(x,[2 4 1 8]); y2 = gauss2mf(x,[2 5 1 7]); y3 = gauss2mf(x,[2 6 1 6]); y4 = gauss2mf(x,[2 7 1 5]); y5 = gauss2mf(x,[2 8 1 4]); plot(x,[y1 y2 y3 y4 y5]) 8
Sigmoid curves Sigmoidal: sigmf(x,[a c]) Ex: x = 0:0.1:10; y = sigmf(x,[2 4]); Diff. sigmoidal: dsigmf(x,[a1 c1 a2 c2]) Ex: x = 0:0.1:10; y = dsigmf(x,[5 2 5 7]); Prod. Sigmoidal: psigmf(x,[a1 c1 a2 c2]) Ex: x = 0:0.1:10; y = psigmf(x,[2 3-5 8]); 9
Spline-based curves Z-shaped: zmf(x,[a b]) Ex: x = 0:0.1:10; y = zmf(x,[3 7]); Pi-shaped: pimf(x,[a b c d]) Ex: x = 0:0.1:10; y = pimf(x,[1 4 5 10]); S-shaped: smf(x,[a b]) Ex: x = 0:0.1:10; y = smf(x,[1 8]); 10
Defuzzification Fuzzy Rules (If-Then rules) Let input, output features x, y assigned by membership functions A, B, three formats can present the rule Verbose: If x is A then y is B Symbolic: If x == A then y = B Operator: (if x1>=a1 & x2<=a2) x3==a3=>y=b Matlab script rule = [1 2 3 1.5 1] input1 is mf1 AND input2 is mf2 then output is mf3, weight = 1.5; AND: 1; OR: 2 Fuzzy operators AND: min(a,b); OR: max(a,b); NOT: (1-A) 11
Mamdani & Sugeno FIS Mamdani s fuzzy inference Output MF follows common Zadah s Fuzzy set function, i.e. triangular, trapezoidal, Guassian, sigmoidal, etc. Suitable for human input Takagi-Sugeno-Kang s fuzzy inference Output MF is a constant C, or a linear expression (i.e. C=ax1+bx2+c, where a, b, c are constants and C implicates the relationship of input MF x1, x2) Computationally efficient 12
Some MATLAB FIS Functions Function Description mfedit Open Membership Function Editor (GUI) ruleedit Open Rule Editor (GUI) ruleview Open Rule Viewer (GUI) parsrule Parse fuzzy rules (modify existing rule) newfis Create new Fuzzy Inference System (FIS) readfis Load FIS from file genfis1 Generate Sugeno FIS structure from data using grid partition mam2sug Transform Mamdani FIS into Sugeno FIS evalfis Perform fuzzy inference calculations showfis Display annotated FIS writefis Save FIS to file plotmf Plot all membership functions for given variable addvar Add variable to FIS addmf Add membership function to FIS addrule Add rule to FIS (rule list like [1 2 3 1.5 1] is required) showrule Display FIS rules Notes: the membership functions can be referred in previous slides Ref. site: http://www.mathworks.com/help/fuzzy/mamdani fuzzy inference systems.html 13
Arguments and Syntax Function mfedit ruleedit Syntax mfedit('a'); mfedit(a); mfedit; ruleedit('a'); ruleedit(a); ruleview ruleview(a); ruleview('a') ; fis2 = parsrule(fis,txtrulelist); fis2 = parsrule(fis,txtrulelist,ruleformat); parsrule fis2 = parsrule(fis,txtrulelist,ruleformat,lang); newfis newfis(fisname,fistype,andmethod,ormethod,impmethod,aggmethod,defuzzmethod); readfis genfis1 mam2sug evalfis showfis writefis plotmf addvar addmf addrule showrule fismat = readfis('filename'); fismat = readfis; genfis1(data); genfis1(data,nummfs,inmftype,outmftype); sugfis = mam2sug(mamfis); output= evalfis(input,fismat); output= evalfis(input,fismat, numpts); [output, IRR, ORR, ARR]= evalfis(input,fismat); [output, IRR, ORR, ARR]= evalfis(input,fismat,numpts) ; showfis(fismat); writefis(fismat); writefis(fismat,'filename'); writefis(fismat,'filename','dialog'); plotmf(fismat,vartype,varindex); a = addvar(a,'vartype','varname',varbounds) a = addmf(a,'vartype',varindex,'mfname','mftype',mfparams) a = addrule(a,rulelist); showrule(fis); showrule(fis,indexlist); showrule(fis,indexlist,format); showrule(fis,indexlist,'verbose',lang); 14
FIS Editor GUI 15
Example for manually creating FIS % 建立 tip_mfis 模糊規估系統檔, 2 個 input 和 1 個 output % input 和 output features 的範圍分別是 [0 10] 及 [0 25] tipper_fis=newfis('tip_mfis'); tipper_fis=addvar(tipper_fis,'input','service',[0 10]); tipper_fis=addvar(tipper_fis,'input','guality',[0 10]); tipper_fis=addvar(tipper_fis,'output','tip',[0 25]); % 設定 input1( 服務態度 ) 的模糊集, 含 3 個 triangular MF tipper_fis=addmf(tipper_fis,'input',1,'poor','trimf',[0 0 5]); tipper_fis=addmf(tipper_fis,'input',1,'normal','trimf',[0 5 10]); tipper_fis=addmf(tipper_fis,'input',1,'good','trimf',[5 10 10]); % 設定 input2( 餐點品質 ) 的模糊集, 含 3 個 triangular MF tipper_fis=addmf(tipper_fis,'input',2,'poor','trimf',[0 0 5]); tipper_fis=addmf(tipper_fis,'input',2,'normal','trimf',[0 5 10]); tipper_fis=addmf(tipper_fis,'input',2,'good','trimf',[5 10 10]); % 繪製兩個 input feature 的模糊集 plotmf(tipper_fis, input,1); plotmf(tipper_fis,'input',2); % 設定 output( 小費等級 ) 的模糊集, 分低中高三等的 3 個 triangular MF tipper_fis=addmf(tipper_fis,'output',1,'low','trimf',[0 0 13]); tipper_fis=addmf(tipper_fis,'output',1,'medium','trimf',[0 13 25]); tipper_fis=addmf(tipper_fis,'output',1,'high','trimf',[13 25 25]); plotmf(tipper_fis, output,1); % 繪製 output feature 模糊集 16
% 建立模糊準則 % [1](Verbose) If the service is poor OR the quality is poor, then the tip will be low % [2](Verbose) If the service is normal, then the tip will be medium % [3](Verbose) If the service is good OR the quality is good, then the tip will be high. % [1](Symbolic) If (service==poor) (quality==poor)=>(tip==low) % [2](Index) 2 0, 2(1):2 % [3] ([]) [3 3 3 1 2] % 定義模糊準則代碼陣列 rule1 = [1 1 1 1 2]; rule2 = [2 0 2 1 2]; rule3 = [3 3 3 1 2]; rules = [rule1; rule2; rule3]; % 加入模糊準則 tipper_fis = addrule(tipper_fis,rules); showrule(tipper_fis); % 檢視模糊準則 %showrule(tipper_fis,2,'symbolic'); showrule(tipper_fis,3,'indexed'); % 推估測試,ex: 當服務態度及餐點品質為 6.5 及 9.8 時, 給多少小費? evalfis([6.5;9.8], tipper_fis) evalfis([6.5 9.8;9.8 6.5], tipper_fis) % 將建立好的 FIS 寫入檔案 writefis(tipper_fis,'tipper_mamdani'); 17
ANFIS Editor GUI 18
Training Process and Testing 訓練誤差與檢驗誤差 :RMSE 19
Database Toolbox Visual Query Builder - ODBC Create ODBC or JDBC connection ODBC (ex: Access) 在系統中建立好 ODBC 資源連線 (Ex: access_testdbx64 連至 testdb.accdb) 亦可使用 Define ODBC 選項建立 JDBC (ex: MySQL) 將 MySQL JDBC JAR 檔複製到 MATLAB 函式庫目錄, 完整路徑如下 :$matlabroot/java/jarext/ mysql-connector-java-5.1.15-bin.jar 打開路徑資料檔, 位置在 $matlabroot/toolbox/ local/classpath.txt 增加前述之 jar 檔路徑至 classpath.txt 中 20
Start query builder 使用 ODBC 選擇 ODBC 設定, 輸入查詢指令 (Ex: SELECT * FROM dept) 定義與使用 JDBC 連線 輸入資料庫名稱 (Name) 驅動程式 (Driver) 與資料庫連線位置 (URL),Ex: Name: testdb Driver: com.mysql.jdbc.driver URL: jdbc:mysql://localhost:3306/ 所定義之 JDBC 連線參數可被存成 mat 檔 ( 如 jdbc_dbtest.mat 選擇 JDBC 連線時可看到資料庫名稱 (ex: testdb), 選取時會被要求輸入資料庫帳號密碼 輸入查詢資料語句並設定 Matlab 變數名稱 (Ex: dept_data, emp_data) 在變數區點選 Matlab 變數顯示資料 21
使用 ODBC 取得資料 設定 ODBC 選擇 ODBC 22
在 MATLAB 取出 Cell 格式資料 Cell 格式資料為表格要轉換後才能計算 23
使用 JDBC 取得資料 設定 JDBC 連線參數 24
使用 JDBC 取得資料 25
Database query steps 資料擷取步驟 建立連線 conn_jdbc = database(dbname, dbuser, dbpass, jdbcdriver, jdbcurl) 執行查詢 curs = exec(conn_jdbc,sqlquery) 取出資料 :curs = fetch(curs) 取出前 3 筆資料 :curs=fetch(curs,3) 顯示資料 :curs.data 指定第 2 欄資料 :curs.data(:,2) 指定第 2,4 欄資料 :curs.data(:,[2,4]) 指定第 2 到 4 欄資料 :curs.data(:,[2:4]) 26
參數說明 dbhost = 'localhost:3306';// 資料庫位置 dbuser = 'root';// 連線帳號 dbpass = 'root123';// 連線密碼 dbname='testdb';// 資料庫名稱 jdbcdriver = 'com.mysql.jdbc.driver'; // 驅動程式 jdbcurl = 'jdbc:mysql://localhost:3306/'; // 連線字串 sqlquery = 'select * from dept'; // 查詢語句, 跳脫字元為 ', ex: 'A%' 寫為 ''A%'' 27
資料增刪修參考 sqlinsert = 'insert into dept values(50, ''ENGINEERING'',''TAIPEI'''; sqlupdate = 'update dept set loc=''taichung'' where deptno=50' sqldelete = 'delete from dept where deptno=50' 以上 SQL 語句均可使用 exec 函數執行, 例如 curs = exec(conn_jdbc,sqlinsert) 關閉連線 : 關閉擷取資料 : close(curs); 關閉資料庫連線 : close(conn); 28
資料轉換 資料庫取得的資料為 cell 格式, 資料格式相同的欄位可以利用 cell2mat 或 cellstr 將資料轉成陣列 數字陣列 : a = cell2mat(curs.data(:,1)) 字串陣列 : b = cellstr(curs.data(:,[2,3])) 轉成數字格式後的 mat 資料型態可存在 workspace 中, 提供進一步處理分析 例如使用 ANFIS 從 workspace 中匯入資料進行訓練或測試 29
結合 ANFIS 和 DATABASE Toolbox 使用 Database Toolbox 存取資料部分 ( 註 : 資料庫中已存有 tainingdata 和 testingdata 兩個資料表 ) % Create database connection dbname = 'testdb'; dbuser = 'testuser'; dbpass = 'test123'; jdbcdriver = 'com.mysql.jdbc.driver'; jdbcurl = 'jdbc:mysql://localhost:3306/'; conn_jdbc = database(dbname, dbuser, dbpass, jdbcdriver, jdbcurl); % Retrieve training data from database sqlquery = 'select * from trainingdata'; curs = exec(conn_jdbc,sqlquery); curs = fetch(curs); % Convert cell-format data to matlab-format numeric data traindata = cell2mat(curs.data); % Convert cell-format data to matlab-format String data % traindata = cell2str(curs.data); 30
依前述步驟取得資料庫中的測試資料 % Retrieve testing data from database sqlquery = 'select * from testingdata'; curs = exec(conn_jdbc,sqlquery); curs = fetch(curs); testdata = cell2mat(curs.data); 使用 ANFIS Toolbox 進行訓練部分 % Use training data to generate an initial FIS nummfs = 3; mftype = 'gaussmf'; in_fis = genfis1(traindata,nummfs,mftype); % Use ANFIS to train data and return adapted FIS epoch_n = 200; dispopt = zeros(1,4); out_fis = anfis(traindata,in_fis,epoch_n,dispopt); % Output training error and testing error [out_fis,error,stepsize,chkfis,chkerr] = anfis(traindata,in_fis,epoch_n,dispopt,testdata); 31
Statistics Toolbox Dataset Sample dataset: hospital, cereal, etc. Create dataset by loading file: dataset('xlsfile','xxx.xls','sheet','yyy'); Loading yyy sheet of Excel file xxx.xls Add / delete new variables to dataset ds_name.var1 = 'x' Add var1 to dataset 'ds_name' and define 'x' as default value ds_name(:,n)=[] or ds_name.var1=[] Delete the nth columns of dataset ds_name 32
Sort dataset sortrows(dataset_name,{sort_cols}) ex: sortrows(hospital,{'sex','age'}) References: http://www.mathworks.com/help/stats/functionlist.html Sample Dataset http://www.mathworks.com/help/stats/_bq9uxn4.html Function list of statistics http://www.mathworks.com/help/stats/functionlist.html Dataset http://www.mathworks.com/help/stats/dataset.html Process variables: create a dataset http://www.mathworks.com/help/stats/add-and-delete-columns.html dataset class http://www.mathworks.com/help/stats/dataset-class.html 33
開放原始碼之地理資訊架構 NGIS, Open GIS, Open Data, and Open Source GIS 國家地理資訊系統 : 政府基礎地理資料庫 開放式地理資訊系統 : 地理資訊網路服務標準 開放資料 : 原始資料公開 法律上授權 開放源碼地理資訊系統 : 免費自由的 GIS 軟體 Quantum GIS (QGIS) http://www.qgis.org/en/site/ QGIS Tutorial Reference Site: http://www.qgistutorials.com/en/ 34
35
Spatial Database 提供空間資料庫的免費資料庫軟體 PostgreSQL, MySQL 與 PostGIS 空間計算函式相容 http://postgis.net/ 空間資料檔格式 :.shp,.dbf,.shx 等 36
R Language R 語言介紹 R 語法來自 Scheme, 是一種自由軟體函數程式語言, 主要用於統計分析 繪圖 資料探勘 R 本來是由紐西蘭奧克蘭大學的 Ross Ihaka 和 Robert Gentleman 開發 ( 也因此稱為 R), 現在由 R 開發核心團隊 負責開發 R 是基於 S 語言的一個 GNU 計劃專案, 通常用 S 語言編寫的代碼都可在 R 環境下執行 S 語言一種用於統計的程式語言,1975-76 年由貝爾實驗室的 John Chambers, Rick Becker, and Allan Wilks 共同研發 37
R 安裝與操作 CRAN CRAN 套件下載 R 官方網站 (http://www.r-project.org/) 進入 CRAN, 可下載針對 Unix Macintosh 或 Windows 的安裝軟體及原始碼 Base package R 平台中已具有約 25 個基本套件, 包含常用的敘述統計運算, 如 : 平均數 標準差 頻度分布圖 迴歸分析等等 CRAN mirror packages 提供數百種為特定資料分析目的所建置好的指令組合套件, 由核心團隊釋出或 R 的喜好者貢獻 38
Python Python 入門 Python 是一種物件導向 直譯式的電腦程式語言, 具有近二十年的發展歷史 包含功能完備的標準庫, 能輕鬆完成很多常見的任務 Python 虛擬機幾乎可在所有作業系統運行 例如 WinPython 亦可在行動碟上嵌入執行 支援命令式程式設計 物件導向程式設計 函數式編程 面向側面的程式設計 泛型編程多種編程範式 NumPy SciPy Matplotlib 可以讓 Python 程式設計師編寫科學計算程式 39