Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Machine Learning with Python

Similar presentations


Presentation on theme: "Introduction to Machine Learning with Python"— Presentation transcript:

1 Introduction to Machine Learning with Python
資工魂

2 Mitch Resnick (MIT Media Lab) 人人都該學程式設計
創意思考 有系統的推論 團隊合作 邏輯

3 why Python 「優雅」「明確」「簡單」
Python 的 Windows 安裝檔基本上包含整個標準函式庫,且通常也包含許多額外套件 我們並不需要發明新的輪子,大多數時候,我們只需要找到一個好的輪子,然後對它做一些修補優化就可以了。

4

5 Python 保留字 常數 型態定義詞 控制陳述 運算子 模組相關 # False None True # class def
# as assert break continue del elif else except finally for global if nonlocal pass raise return try while with yield 運算子 # and not or is in lambda 模組相關 # from import

6 程式語言 輸入 輸出 運算式 判斷語句 迴圈語句 函數 語句塊 (縮排) 註解 # 數學運算 關係運算 邏輯運算 = + - * /
語句塊 (縮排) 註解 # 數學運算 = + - * / 關係運算 < > <= >= == != 邏輯運算 and or not

7 函數 定義函數 使用函數

8 輸出行事曆所有Event

9 迴圈 土法鍊鋼 使用2個for迴圈 print (Event[Sun,0]) print (Event[Sun,1]) … print (Event[Sun,23]) print (Event[Mon,0]) print (Event[Mon,1]) print (Event[Mon,23]) print (Event[Sat,0]) print (Event[Sat,23]) for weekday in (7天) for hour in range (24小時) print (EVENT[weekday, hour])

10 判斷 if (身高未滿170) 全殘 else if (身高未滿180) 半殘 else 身心健全

11 網軍

12 code def 階級 (帳號) : if 登入次數 <= 30 新訓 菜 elif 登入次數 <= 210 二兵 elif 登入次數 <= 575 一兵 elif 登入次數 <= 1000 上兵 elif 登入次數 > 1000 if 擔任過站務 if 登入次數 <= 1500 少尉 elif 登入次數 <= 2000 中尉 elif 登入次數 <= 3000 上尉 elif 登入次數 <= 3500 少校 elif 登入次數 <= 4000 中校 elif 登入次數 <= 4500 上校 elif 登入次數 <= 5000 少將 elif 登入次數 <= 5500 中將 elif 登入次數 <= 6000 上將 elif 登入次數 > 6000 四星上將 else 下士 中士 上士 三等士官長 二等士官長 一等士官長 士官督導長 def 網軍 if 登入次數 <= 30 新訓 菜 elif 登入次數 <= 210 二兵 elif 登入次數 <= 575 一兵 elif 登入次數 <= 1000 上兵 elif 登入次數 > 1000 if 擔任過板主 or 擔任過小組長 if 登入次數 <= 1500 少尉 elif 登入次數 <= 2000 中尉 elif 登入次數 <= 3000 上尉 elif 登入次數 <= 3500 少校 elif 登入次數 <= 4000 中校 elif 登入次數 <= 4500 上校 elif 登入次數 <= 5000 少將 elif 登入次數 <= 5500 中將 elif 登入次數 <= 6000 上將 elif 登入次數 > 6000 四星上將 else 下士 中士 上士 三等士官長 二等士官長 一等士官長 士官督導長

13 迴圈 for 帳號 in 使用者列表 階級 (帳號) #使用先前定義的階級函數

14 一例一休 平日加班費: 延長工作時間在2小時以內者 加給3分之1以上 再延長工作時間在2小時以內者 加給3分之2以上 休息日 4小時以內者
以4小時計 逾4小時至8小時以內者 以8小時計 逾8小時至12小時以內者 以12小時計 加班費算法同平日 國定假日及特休 加倍發給 例假日 加倍 & 事後補假休息 if (平日) 加班時數 = 打卡時數 - 8 if 加班時數 <= 2 加班費 = 加班時數 * 1.33 * 時薪 elif 加班時數 <= 4 x = 加班時數 – 2 加班費 = (2 * x * 1.66)*時薪 else 違反勞基法 elif (休息日) 加班時數 = 打卡時數 if 加班時數 <= 4 #時數 = 4 加班費 = (2 * * 1.66)*時薪 elif 加班時數 <= 8 #時數 = 8 加班費 = (2 * * 1.66)*時薪 elif 加班時數 <= 12 #時數 = 12 加班費 = (2 * * 1.66)*時薪 elif (國定假日及特休) … elif (例假日)

15 here we go 安裝 Anaconda 安裝套件(如有需要) https://www.continuum.io/downloads
conda install 套件名稱 pip install 套件名稱

16 Anaconda spider

17 實戰 研究計畫經費表資料庫查詢

18 web crawler 自動化 讀檔 網路爬蟲 取得網際網路資源 request 解析網頁的內容  beautifulsoup4 寫檔

19 CLRPG3D0043研究經費收支明細表(專帳) 31800 風濕過敏科 郭昶甫 計劃名稱: 巨量資料及統計中心 [ ] 開始日期: 2016/10/1 結束日期: 2017/9/30 展延日期:
費用別 預算金額 本院補助 院外補助 院內核銷金額 院外核銷金額 餘額 人事費 有關研究他項費用 310000 283640 26360

20 檢示網頁原始碼 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=big5"> <title>資料庫查詢</title> </head> <body background="../data/backg.gif" bgproperties="fixed"> <center><h3> CLRPG3D0043研究經費收支明細表(專帳)</h3></center><center><h3> 風濕過敏科 郭昶甫 </h3></center><center><h3>計劃名稱: 巨量資料及統計中心 [ ]</h3></center><center><h3>開始日期: 2016/10/1 結束日期: 2017/9/30 展延日期: </h3></center><CENTER><TABLE border=5 cellspacing=3 cellpadding=3><TR><td>費用別</td><td>預算金額</td><td>本院補助</td><td>院外補助</td><td>院內核銷金額</td><td>院外核銷金額</td><td>餘額</td></TR><TR><td><a HREF=MRPE.ASP?MRPNO=CLRPG3D0043&FID=A>人事費</A></td><td> </td><td> </td><td></td><td> </td><td></td><td> </td></TR><TR><td><a HREF=MRPE.ASP?MRPNO=CLRPG3D0043&FID=Z>有關研究他項費用</A></td><td>310000</td><td>310000</td><td></td><td>283640</td><td></td><td>26360</td></TR></TABLE>研究經費查詢無誤,<a href=mrpd.asp>回查詢螢幕</a></CENTER> </body> </html>

21 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=big5"> <title>資料庫查詢</title> </head> <body background="../data/backg.gif" bgproperties="fixed"> <center><h3> CLRPG3D0043研究經費收支明細表(專帳)</h3></center> <center><h3> 風濕過敏科 郭昶甫 </h3></center> <center><h3>計劃名稱: 巨量資料及統計中心 [ ]</h3></center> <center><h3>開始日期: 2016/10/1 結束日期: 2017/9/30 展延日期: </h3></center> <CENTER> <TABLE border=5 cellspacing=3 cellpadding=3> <TR> <td>費用別</td> <td>預算金額</td> <td>本院補助</td> <td>院外補助</td> <td>院內核銷金額</td> <td>院外核銷金額</td> <td>餘額</td> </TR> <td><a HREF=MRPE.ASP?MRPNO=CLRPG3D0043&FID=A>人事費</A></td> <td> </td> <td></td> <td> </td> <td> </td> <td><a HREF=MRPE.ASP?MRPNO=CLRPG3D0043&FID=Z>有關研究他項費用</A></td> <td>310000</td> <td>283640</td> <td>26360</td> </TABLE> 研究經費查詢無誤,<a href=mrpd.asp>回查詢螢幕</a> </CENTER> </body> </html>

22 beautifulSoup4 解析<tag>的套件
soup = BeautifulSoup(res.text, "html5lib") list soup.select('h3') soup.select('td') 元素和值 soup.select('<tag>名稱')[index].text

23 CLRPG3D0043研究經費收支明細表(專帳) 31800 風濕過敏科 郭昶甫 計劃名稱: 巨量資料及統計中心 [ ] 開始日期: 2016/10/1 結束日期: 2017/9/30 展延日期:
費用別 預算金額 本院補助 院外補助 院內核銷金額 院外核銷金額 餘額 人事費 有關研究他項費用 310000 283640 26360

24 request 解析URL的套件 get https://zh.wikipedia.org/wiki/巨量資料
requests.get(url) post requests.post(url, headers, data)

25 就是幫我們減少人力的東西!

26 實戰 將大量檔案根據Result.xml內的文字重新命名

27 根據Result、Remark、AdditionalPattern內的文字更改副檔名為PNG的檔案,若沒有就跳過。
Header:Well_01

28 演算法 for 檔案 in 目錄 開啟結尾為result.xml的檔案 解析xml檔 Result字串 Remark字串 AdditionalPattern字串 合併為dataString 修改檔名 dataString+ header + Img_XX.png

29 解析xml 使用BeautifulSoup
#解析xml soup = BeautifulSoup(res, "xml") #處理tag Result = soup.select('Result')[0] SResult = Result.select('string')[0].text.strip() Remark = soup.select('Remark')[0] SRemark = Remark.select('string')[0].text.text.strip() AdditionalPattern = soup.select('AdditionalPattern')[0] SAdditionalPattern = AdditionalPattern.select('string')[0].text. text.strip() #合併字串 dataString = SResult + '_' + SRemark + '_' + SAdditionalPattern + '_'

30 修改檔名 os.rename #取得header f.split('Result.xml') header = (f.split('Result.xml')[0]) #修改檔名 img1 = header+'Img_01.png' src1 = os.path.join(outputroot, img1) #組合路徑和檔名 img1 = dataString+img1 #加上dataString dst1 = os.path.join(outputroot, img1) #組合路徑和檔名 os.rename (src1, dst1) #rename

31 遞迴處理路徑下所有資料夾和檔案os.walk for root, dirs, files in os.walk(inputPath): for f in files: if (f.endswith('Result.xml')): #找出Result.xml res = open(os.path.join(root, f),'r') #開啟檔案 解析xml檔 修改檔名

32 demo

33 不要去重新發明輪子 分享自己的輪子 做事要用適合你的工具, 而不是選一個工具來挑戰你的工作。

34 more Fansboard Kobe Bryant Shot Selection

35 and more Will patients show up to their medical appointments?
根據人口數據和預先存在的條件來預測病人是否會真正出現醫療預約。

36 Datasets 300,000 observations and 15 variables
Target variable:  狀態 Status (Show-Up or No-Show) Independent variables: 年齡 Age 性別 Gender 糖尿病 Diabetes 酒精 Alcoholism 高血壓 Hypertension 殘障 Handcap smokes 學歷 Scholarship 結核 Tuberculosis 簡訊提醒 Sms_Reminder 等待時間 AwaitingTime 週幾 DayOfTheWeek

37 Create dummy variables avoid overfitting
Split dataset into train, validate, and test Select classification algorithms to be trained with train data use 3 popular algorithms, including decision tree, random forests, and multi-layer perceptron (neural network) Apply cross-validation to evaluate best parameters on validation data Evaluate top model with best parameters on test data


Download ppt "Introduction to Machine Learning with Python"

Similar presentations


Ads by Google