chainsawriot

Home | About | Archive

What I've learnt from ml-class

Posted on Dec 13, 2011 by Chung-hong Chan

ml-class 將在十六日終結,我從此課獲益不淺,以下是五件我在此課學到最有用的東西。

1. Linear Algebra

之前說過,在上這個課前是完全沒有讀過 linear algebra 。奇就奇在為何我可以係生物統計學碩士。不會 linear algebra 的統計學人,就像彈結他不會彈 Barre Chord 那樣,你是會彈到某幾首歌的,但卻不能彈深奧的歌。
((我最近都想問問以前在碩士課教 Stat 的教授,為何不教 Linear algebra 。我是知道此問題的答案,因為當年教流行病學的教授,也即是我的論文指導老師說過,只需要訓練我們看得明 SPSS 的 output 就可以了。再者,當年同學見到課堂上講到任何 equation 都即時怕怕,就別說甚麼 linear algebra 了。到尾,我們連甚麼是 maximum likelihood 也不知道。據說我是舊制的最後一屆,新一屆將課程再行簡化,向「看得明 SPSS 的 output 就可以了」更進一步。)) ((外國的 Biostat prerequisite ,除了有 linear algebra 之外,還有 multivariate calculus 。沒有此兩知識,讀完 biostat 也枉然。枉然的意思是,你可能可以應用 biostat 的一些技巧,但卻不能步入 biostat 研究的奧堂。我正是處於此枉然狀態。))

舉例,有數據如下

Sales Price
160 126
180 103
200 82
220 75
240 82
260 40
280 20

如想建立 regression model ,以 price 估計 sales ,在 R 的做法很易的:

[code]
# < -
sales <- c(126, 103, 82, 75, 82, 40, 20)
price <- c(160, 180, 200, 220, 240, 260, 280)
lm(sales~price)
[/code]

其實 under the hood ,也是 linear algebra 。

[code]
# <-
data.mat <- matrix(c((rep(1, length(price))), price), ncol=2, byrow=FALSE)
beta.hat <- solve(t(data.mat) %*% data.mat) %*% t(data.mat) %*% sales
y.hat <- data.mat %*% beta.hat
[/code]

2. Vectorization

我會用到的工具 ( R, Octave )是 vector/matrix 語言。市面上還有不少此類工具,如 Python/numpy 或 Perl/PDL 。用到此類工具,如知道 vectorization 技巧,可取代很多的 loop ,加快速度。 ((當然,加速了,就用多了記憶體,這是 space-time tradeoff 。))
假設我不知道 R 可以計 Standard Deviation 。我自行 implement 了以下兩個不同的版本。 forloop.sd 是用兩個 for loop 去計算 mean (bar.x) 及 sum of squared difference,而 vec.sd 是用 vectorization 的方法計算。

[code]
#< -
fl.sd <- function(x) {
sum.x <- 0
for (i in 1:length(x)) {
sum.x <- sum.x + x[i]
}
bar.x <- sum.x/length(x)
sum.sq.diff <- 0
for (i in 1:length(x)) {
sum.sq.diff <- sum.sq.diff + (x[i] - bar.x)^2
}
return(sqrt(sum.sq.diff*(1/(length(x)-1))))
}
vec.sd <- function(x) {
bar.x <- sum(x)/length(x)
sq.diff <- (x - bar.x)^2
return(sqrt(sum(sq.diff) * (1/(length(x)-1))))
}
[/code]

再進行以下的實驗:

[code]
# <-
n <- 10
n.tested <- c()
fl.time <- c()
vec.time <- c()
while (n <= 10000000) {
n.tested <- append(n.tested, n)
fl.time <- append(fl.time, system.time(fl.sd(rnorm(n)))[3])
vec.time <- append(vec.time, system.time(vec.sd(rnorm(n)))[3])
n <- n*10
}
plot(n.tested, fl.time,type="l", log="x", xlab="no of cases", ylab="time")
lines(n.tested, vec.time, type="l", col="red")
legend("topleft", legend=c("Loop","Vectorization"), col=c("black", "red"), lty=c(1,1), box.col="white")
[/code]

上圖的 x-axis 是 log scale 的,是用 rnorm 生產出來的亂數數量。而 y-axis 是計算 sd 所需的時間。從圖中所見, 當 no of cases 仍是低於 10000 時,有沒有 vectorization 的時間差別不大。但當增加至 100000 時,差別開始出現。到 no of cases 增至一千萬時, forloop 版本要用 37 秒計算,而 vectorization 版本只需 1.7 秒。當 no of cases 每增加十倍時,計算所需時間亦約增加十倍。這是因為計算 sd 的算法屬於 Big-O notation 的 O(N) 。 ((又稱 linear time ,代表 processing time 與 input size 成線性比例。)) Vectorization 比 forloop 快,是因為 R (或其他 matrix/vector 語言) 的 Matrix/vector operations 是經過專門優化的。在寫如 Artificial neural network 或 collaborative filtering 等等的 algorithm ,如果不用上 vectorization ,根本不能成事。

3. Algorithmic Model Culture

Ref: Brieman. Stat Sci 2001; 16: 199-231

嚴格來說並不是在 ml-class 學,而是自己好奇去讀的。 Machine Learning 和 Statistics 都是在做 model ,為何 Machine Learning 好似好 free ,而 Statistics 卻是諸多限制?
原來傳統 Statistics 假定數據產生自某種 data model ,而我們假定我們知道此 data model 的形態。我們用 regression 之類的工具去為此 data model 作 parameterization 。但是在非統計學的 Algorithmic Model Culture ,卻認為我們從來不知 data model 的形態。 model 的目的只是找出一個可由 input variables 預測 output variable 的 function 。此 culture 也特別注重 model validation 。

4. 2x2 table

在講 classification problem 時, Machine Learning 在分析 2x2 table 時用了完全不同的 terminology 。以下是我為此而製作的 conversion table 。

Machine Learning Biostatistics
Precision Positive Predictive Value
Recall Sensitivity
? Negative Predictive Value
? Specificity
? Likelihood ratio

在選擇 Threshold 的問題上,在 Machine Learning 是用 F1 score ,而 F1 score 是 precision 和 recall 的 harmonic mean 。但是在 medical research ,用的方法仍是 plot 一個 ROC curve 。實制是找出 sensitivity + specificity 最高的 threshold 。
如果我在下一篇文用 F1 score 來找 threshold 的話,恐怕又是收到「不夠 intuitive 」的評價吧。

5. Good data > good algorithm

"It's not who has the best algorithm who wins. It's who with more data."

此句話是真的,但當然 feature x 亦應有資料去 predict y 。例如你只有 acceleration 的數據,卻要去估計 force 。就算數據有無限多,也不會準。但是,如果有 acceleration 和 mass 的數據去估計 force 。 ((f=ma, Newton's laws of motion)) 要是你用無限多的數據去建立 model ,你用任何一種 algorithm 的誤差 ((variance and bias)) ,都應該差不多地低。


Powered by Jekyll and profdr theme