Posts

Showing posts with the label Data preparation

機器學習之陷阱 - Imbalance Class Classification

Image
幾年前的筆者,一直覺得Build Model是一件很不可思議的事,一提到建模這個課題,就會眼前一亮,充滿好奇和期待,不知道讀者們有沒有嘗試過在做分類(Classification)的時候,一看到數據,也沒有想太多,直接就把數據倒進模型作訓練,再看看Performance有多少,看到準確度有92%,表現很不錯吧,我看的是不一定了,為了讓讀者們更好理解文中的意思,我會採用"十個男人九個滾"的例子來闡釋。 假設我收集了100個男人的資料,9成2的人都曾經對伴侶不忠,其餘的就管它是還在考慮或是打死不會,目的是從這些會出軌和不會的男人身上,讓我的Model學習出軌的男人特點,從而預測其他男人會否對伴侶不忠,剛才說過我的模型有92%的準成度,筆者還以為可以慶功食牛雜,女人們以為可以試試伴侶是否不忠的時候,才發現所有的男人都被預測為"出軌",當中包括真的會出軌的92人, 其餘8個白白被"砌了生豬肉",還未出去滾已慘遭被判為出軌,要是這樣,倒不如直接說成"十個男人十個滾",連Build Model的氣力也省去了,所以零分重作吧! Actual / Predict           滾             沒滾 / 唸緊   滾 92 0 沒滾 / 唸緊 8 0 但是為何會發生這種事呢?明明準確度很高,結果卻不似預期。其實原因很簡單,這都是數據的類別分佈出現不均勻和分類器(Classifier)的設計所造成的,因為一般的Classifier的學習目標是優化準成度,透過獲得愈少的誤差,才不會考慮類別的分佈,也就是少數的聲音很容易被埋沒,導致Classifier難以從少數類別的Pattern中學習,最壞的結果就是剛才的結果:所有數據都分成同一類別。 雖然Imbalance Class不是一個新鮮的問題,但對於入門級的人,想要對模型或數據分析技巧更上一層樓的話,這些基礎是少不了,如果沒有這些理論在背後支持,我相信街上每一個的Programmer也會比你強,只要寫幾句Coding,便能輕鬆實現建模,測試和預測,那麼你就好好想一下作為數據分析師的價值何在。...

Pandas - 快速處理和變換數據

Image
對於編程初學者,其中一個難題是轉換或修改原始數據。 沒有Package的支持,會很困難和耗時的。 如果您是忠實的R用戶,您應該知道R有一個特殊的Data Type:Data Frame。結構十分像Database裡的Table一樣有Fields和Records,在處理和變換的工序也非常方便,令你可以再進一步分析。 而Python亦多得Pandas這個Package,讓我們可以使用這個數據結構來進行分析。 我們可通過在CMD / Terminals輸入以下命令來安裝。 安裝Pandas Mac $ sudo pip install pandas Window > pip install pandas Ubuntu $ sudo apt-get install python-pandas 應用 Pandas也使閱讀檔案(. json, . csv, . html等)的程序更簡單。 具有csv格式的文件更容易處理,因為它本身就呈現Table的格式,所以我不會用CSV的檔案作例子。以Json格式的為例,參考我們之前文章所使用的數據,json文件中儲存了一系列會員的資訊。 假設您已把“data.json”的文件保存在您自己的Desktop上,那麼打開你自己的Python編輯器和感受Pandas的強大支援! > import os, pandas > desktop_dir = os.path.join(os.path.expanduser("~"),"Desktop")    # get the desktop directory > jsonfile_dir = os.path.join(desktop_dir,"data.json")    # as data.json saved in desktop > json_data = pandas.read_json(jsonfile_dir)    # json_data is Data Frame type > print json_data 看到嗎? 結果顯示了一個很有結構的表格,為了獲得我們會員的資料,我們會寫上json_data.profile來獲得"P...

Python - 利用Dictionary處理數據

Image
本文章將分享如何利用編程技術更有效地處理原始數據。 請參考 之前的文章 安裝Python和相關模組,並下載原始數據(JSON格式)。 在剛才的例子中,10個成員的年齡被提取並存儲在一個數組中。 在會員資料中,用戶有1個Primary Key(會員ID)和3個Attributes(姓名,性別,年齡), 因此我們需要建立三組陣列(List),並把這些資料儲存在它們中。 > (name, gender, age, ...) = ([], [], [], ...) # multiple declare the arrays > for i in range(len(raw_data["profile"])):        name.append(raw_data["profile"][i]["name"])        gender.append(raw_data["profile"][i]["gender"])        age.append(raw_data["profile"][i]["age"]) Another way is that a dictionary with key value can be used to store the member list from JSON data. 除了List,Dictionary也是用作儲存多個Elements的Data Type,以Key和Value作為一組來儲起每一個會員的資料,其格式跟JSON一樣,所以我們只需輸入已知的Key便能獲得它的Value。 > from pprint import pprint  > members = {}    # declare a dictionary to store the member > for i in range(len(raw_data["profile"])):       mem_id = raw_data["profile"][i]...

Data cleansing - Enhance your data quality

Image
Most of time, there is inaccurate or wrong data due to system bugs or human-input error. In fact, these kind of unexpected error usually occurs in our database and occupied certain proportion of overall observations. "Quality data, quality results." This statement is valid especially when we generate the summary report with statistics. Any missing or duplicated records could mislead the statistical results. Additionally, we sometimes find the abnormal value for some attribute (say zero transaction amount)  in a record. For predictive modelling in machine learning, outliers with extreme value are kicked out besides the improper data to prevent from affecting the model performance. When studied in the university, I usually neglected the importance of cleansing the data. The reason seems to be my curiosity of studying the analytical method is far away from that of processing the data. Of course, I have learned the lesson from my own jobs and awarded the significance of dat...