Posts

Showing posts with the label Pandas

研究代號:活在球內盡是數據

Image
最近Pokemon Go在全球掀起一股熱潮,世界各地的訓練員,來自不同年齡,香港從以前全民皆股,全民皆炒,變成全民通街走;由之前的雨傘XX遍地開花,變成遍地開櫻花,這遊戲無疑能短時間改變了我們日常生活習慣,是好是壞,那就要看看玩家自己本身了。筆者個人也十分熱愛寵物小精靈,GBA遊戲:舊版,金銀,紅藍寶石通通玩過,Pokemon Go的出現不但讓自己找回童年的回憶,更成為下班後的減壓良物。 小時候的畫功真的有點不堪入目(囧) 當然在娛樂的時候,我也不忘做一點分析的工作,本文並不是分享Pokemon Go的攻略,如果想知道關於pokemon go的攻略,不妨瀏覽我朋友 小影 寫下的 攻略 ,既實用亦具參考價值。而我研究的Scope會更大,除了pokemon go的精靈,更會研究所有手提遊戲機(GBA, 3ds)版本的精靈。 近日,我發現了一個網站免費提供API (https://pokeapi.co/),能夠從中獲得小精靈們的數據,由最舊的黃版至X-Y版也包含在內,由於Request的次數是有上限的,所以我把獲取下來的數據先放在MongoDB,也即是NoSQL的資料庫,需要時從資料庫拿出來就好了。之後的資料整合及分析會交給Python中的Pandas, Scikit-learn的package來處理。 手提遊戲機版本(以下統稱為GAMEBOY版本)的精靈至X-Y新世代,一共有750隻精靈 寶可夢 (不要問我為何要刪掉寶可夢),基本屬性已有18種,加上有些精靈是擁有兩種屬性,屬性的組合實在多不勝數,另外,精靈的 基礎能力 也分為6個維度:血量 (HP),速度 (speed),攻擊力 (attack),特攻力 (special_attack),防禦力 (defensive),特防力 (special_defensive)。為了顯示每一只精靈的能力,我plot了一幅簡潔易明的圖表: 如果我們有3-dimension的資料,當然可以用3D圖來顯示,可是換着更高維度的呢?用4D, 5D圖?開玩笑的,其實有一個實用的方法:先利用Principal Componets Analysis (PCA) 是把6個dimension降成2個dimension,再用平面圖表示每一點,大家不用理會X, Y-axis的意思,可以了解點與...

邏輯迴歸 - 讓你知道"是"或"否"

Image
另一種稱為邏輯回歸 (Logistic Regression)的回歸模型用於描述某類別 (Categorical Variable)與獨立變量 (Independent Variables)之間的關係,並估計出現某類別的概率。 邏輯回歸中的輸出變量是分類而不是間隔。 可能讀者們早已聽過機器學習的Classification,它是Data Mining和Pattern Recognition中常見的應用之一。透過Logit Function的轉換,目標變量的類型變成了Categorical,不再是Numeric。 為了讓初學者容易理解邏輯回歸的精要,在本文中,會利用只有兩個類別的目標變數會作為例子(兩個類別為“是”或“否”)。 公式: $$ln(\frac{p}{1-p}) = w_0 + w_1x_1 + w_2x_2+...+w_nx_n=\sum_{i=1}^{m}{w_ix_i}=w^Tx$$ 當中的$p$為得到“是”的機率,透過一些轉換,我們會獲得新的公式(如下)。假設我們已經知道所有自變量, $x$和每個自變量的比重$w$,我們便能計算出得到“是”的機率。至於“否”的機率,明顯地是$1 - p$ $$p=\frac{1}{1+e^{-w^Tx}}$$ 邏輯回歸的Cost function的公式定義在下面,這其實由統計學上的Maximum Likelihood Estimation衍生出來,同樣地,目的是把Cost function的減至最少,從而獲得相應的比重, $w_i$。 $$J(w) = -\frac{1}{m}[\sum_{j=1}^{m}{(y^{(j)}log(h_w(x^{(j)}) + (1-y^{(j)})(log(h_w(x^{(j)}))}]$$ 從機器學習的角度出發,邏輯回歸運算的要求較低,易於實現和解釋。 然而,面對一些非線性關係的自變量,違反了模型本身的假設,其表現亦會減弱,即是更容易錯誤地分類。 在Python實現 1. 安裝Scikit-Learn 參考 先前的文章 安裝用作機器學習的Package。 2. 數集 數據 及可視化 假設我們有一組客戶的數據,包括他們的個人資料,消費量和今年有沒有續會( 按這裡下載 )。 我們的目標是根據這些歷史數據 (Training Data)預測一些新加...

Logistic Regression - Tell us "Yes" or "No"

Image
Another regression model called Logistic regression is used to describe the relationship between the categorical dependable variable and independable variables with logistic function and estimate the probabilities of binary response. The output variable in logistic regression is categorical rather than interval. You might have heard the terms, Classification, which is one of common applications in data mining or automatic pattern recognition. We can classify the binary, ordinal or nominal target variables by performing Logistic Regression. In this tutorial, only binary target variables ("Yes", "No") is taken as example of classification in application since it is less complicated for beginner to get a grip on. Formula: $$ln(\frac{p}{1-p}) = w_0 + w_1x_1 + w_2x_2+...+w_nx_n=\sum_{i=1}^{m}{w_ix_i}=w^Tx$$ The probability of being "Yes", $p$ can be expressed as below by modifying above formula. If the probability exceeds the threshold (generally 0.5), the...

線性迴歸 - 統計和預測的最基礎

Image
線性回歸 (Linear Regression) 應用於不同的方面以達到不同的目的。 描述自變量Independent Variables($ x_i $)和因變量Dependent Variables($ y $)之間的關係的方法,或者通過輸入($ x_i $)預測目標變量($ y $)。 還有助於我們了解在更改一個或多個獨立變量時對因變量變化的影響。 公式: $$y = w_0+w_1x_1+w_2x_2+ ... +w_nx_n$$ 也可以表示為: $$h_w(x)=\sum_{i=1}^{m}{w_ix_i}=w^Tx$$ Cost Function定義為$h_w(x^{(j)})$和$y^{(j)}$之間的差的平方和。 統計學上,被稱為平方誤差(Sum of Square Error, SSE)。 為了使直線符合$n$數據點,Cost Function / SSE需要最小化以實現優化目標,這過程又稱OLS Estimation(Ordinary least squares Estimation)。 直觀地想,預測值(Predicted value)和實際目標(Actual value)值之間的差異越小,預測模型給出的結果越接近實際值,也暗示這個模型的愈準確,誤差值亦愈小,$R^2$表示線性回歸模型解釋數據的能力。愈高的$R^2$意味著模型愈有能力解釋現有的數據。 它還量度總變異, $SST$和回歸平方誤差的和, $SSR$的比例。 $R^2=\frac{SSR}{SST}$, where $0\leq R^2\leq 1$ 但是,線性回歸面對著非線性關係的變數,不如其他模型那樣精確,如神經網絡,始終它假設輸入和輸出的線性關係。 線性回歸的另一個問題是它對Outlier(主要是Influence Point) 的敏感性,會影響預測結果的準確性。 在我們訓練模型之前,可以在某些圖表中或者統計結果中被識別,把Outlier踢出來解決這個問題。 In previous chapter , I have introduced the overall process of data mining. Some of you might not understand well. It should be fine since the s...

Linear Regression - Simple model for statistics and prediction

Image
No doubt that Linear Regression are competent to be applied to different aspects and purposes. It can be an approach to describe the relationship between the independent variables ($x_i$) and dependable variable ($y$), or predict the target variables ($y$ ) with the inputs ($x_i$). It also helps us to understand how much effect on change of dependent variable, when changing one or more independent variables. Formula: $$y = w_0+w_1x_1+w_2x_2+ ... +w_nx_n$$ It is also presented as: $$h_w(x)=\sum_{i=1}^{m}{w_ix_i}=w^Tx$$ Cost Function is defined as sum of square of difference between $h_w(x^{(j)})$ and $y^{(j)}$. In statistics, it is called sum of square error, SSE. In order to fit the straight line to $n$ data points, cost function / SSE needs to be minimized to achieve the optimization goal.  $$J(w)=\sum_{j=1}^{m}{h_w(x^{(j)})-y^{(j)})^2}$$ Think it intuitively, the less difference between predicted and actual target values, the predictive model is giving the result which...

Pandas - make Data Frame as easy as R

Image
For a programming beginner,  one of the main difficulties is to transform or modify the raw data. Without the support of installed library, it is hard and time-consuming to handle this part. If you are the loyal R user, you should know that there is a special variable type: data frame. It is quite convenient to retrieve the attributes and corresponding values and further analysis. Thanks to Pandas package which is one of the modules in Python, we are allowed to analyze with the well-defined data structure. Let's install by inputting below command in CMD / terminal. Installation of Pandas Mac $ sudo pip install pandas Window > pip install pandas Ubuntu $ sudo apt-get install python-pandas Application Pandas also makes the procedures of reading file (.json, .csv, .html, etc.) simpler. The file with csv format is easier to handle since it has already been in the table-like structure. As a consequence I will take Json data as the example instead of .csv file. Refer to t...

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...