Model Evaluation and Validation

You can find this article and source code at my GitHub

Testing

Two types of our problems

Think about a simple case... How well is my model doing with a regression problem?

It seems that, though the line in the right graph fits better to the original data points. But if we add one more new data point for testing purpose, the left one works better since it's more generalized.

How do we measure the generalization?

For a regression problem...

For a classification problem...


Notice that both models fit the training set well, but once we introduce the testing set, the model on the left makes less mistakes than the model on the right.

This issue can be handled easily in a Python package called "sklearn".

from sklearn.model_selection import train_test_split
X_train, y_train, X_test, y_test = train_test_split(X, y, test_size=0.25) # 25% total samples will be split into the test set

A golden rule is...

Never use your testing data for training purpose.
That is, never let your model know anything about your testing data. Your model should not learn anything from the testing data.


Evaluation

There is a metric for classification problems called "confusion matrix"

You can fill the blank by yourself to see whether you understand this metric correctly.

The answers are 6, 1, 2 and 5 for True Positives, False Negatives, False Positives, and True Negatives, respectively.


Accuracy

We have a very basic method to calculate the accuracy...

Again, "sklearn" can do this simply with several lines of code

from sklearn.metrics import accuracy_score
accuracy_score(y_true, y_predict)

Regression metrics

from sklearn.metrics import mean_absolute_error
from sklearn.linear_model import LinearRegression

classifier = LinearRegression()
classifier.fit(X_train, y_train)

guesses = classifier.predict(X_test)
error = mean_absolute_error(y_test, guesses)

But there is a problem with the mean absolute error (MAE) is that the formula of MAE is not differentiable, therefore it cannot be adopted to some common method we will use later such as the gradient descent.

An alternative method is the mean squared error (MSE).

from sklearn.metrics import mean_squared_error
from sklearn.linear_model import LinearRegression

classifier = LinearRegression()
classifier.fit(X_train, y_train)

guesses = classifier.predict(X_test)
error = mean_squared_error(y_test, guesses)

Another common metric we use here is the R2 score.

The formula is as below, and the error in the two figures is calculated with the MSE formula.

from sklearn.metric import r2_score

y_true = [1, 2, 3]
y_pred = [3, 2, 3]

r2_score(y_true, y_pred)

Type of Errors

Error due to bias (underfitting)

Error due to variance (overfitting)

There is the trade-off...


Model Complexity Graph


K-Fold Cross Validation

This is a very useful way to recycle our data...

With this algorithm, for example, in the above graph, we will go train our model 4 times with the different splitting result. And then we average the 4 results in order to find the final model.

"sklearn" is awesome!

from sklearn.model_selection import KFold

kf = KFold(12, 3)
for train_idx, test_idx in kf:
    print(train_idx, test_idx)

If we want to "eliminate" possible bias, we can also add randomized selection in the K-Fold algorithm.

"sklearn" is awesome AGAIN!

from sklearn.model_selection import KFold

kf = KFold(12, 3, shuffle=True)
for train_idx, test_idx in kf:
    print(train_idx, test_idx)

Thanks for reading. If you find any mistake / typo in this blog, please don't hesitate to let me know, you can reach me by email: jyang7[at]ualberta.ca

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末迂尝,一起剝皮案震驚了整個(gè)濱河市总珠,隨后出現(xiàn)的幾起案子筋蓖,更是在濱河造成了極大的恐慌,老刑警劉巖衫画,帶你破解...
    沈念sama閱讀 206,723評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異压汪,居然都是意外死亡牵囤,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,485評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門(mén)窜骄,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)锦募,“玉大人,你說(shuō)我怎么就攤上這事邻遏】纺叮” “怎么了虐骑?”我有些...
    開(kāi)封第一講書(shū)人閱讀 152,998評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)赎线。 經(jīng)常有香客問(wèn)我廷没,道長(zhǎng),這世上最難降的妖魔是什么垂寥? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 55,323評(píng)論 1 279
  • 正文 為了忘掉前任颠黎,我火速辦了婚禮,結(jié)果婚禮上滞项,老公的妹妹穿的比我還像新娘狭归。我一直安慰自己,他們只是感情好文判,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,355評(píng)論 5 374
  • 文/花漫 我一把揭開(kāi)白布过椎。 她就那樣靜靜地躺著,像睡著了一般戏仓。 火紅的嫁衣襯著肌膚如雪疚宇。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 49,079評(píng)論 1 285
  • 那天赏殃,我揣著相機(jī)與錄音敷待,去河邊找鬼。 笑死仁热,一個(gè)胖子當(dāng)著我的面吹牛榜揖,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播股耽,決...
    沈念sama閱讀 38,389評(píng)論 3 400
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼根盒,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了物蝙?” 一聲冷哼從身側(cè)響起炎滞,我...
    開(kāi)封第一講書(shū)人閱讀 37,019評(píng)論 0 259
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎诬乞,沒(méi)想到半個(gè)月后册赛,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,519評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡震嫉,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,971評(píng)論 2 325
  • 正文 我和宋清朗相戀三年森瘪,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片票堵。...
    茶點(diǎn)故事閱讀 38,100評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡扼睬,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出悴势,到底是詐尸還是另有隱情窗宇,我是刑警寧澤措伐,帶...
    沈念sama閱讀 33,738評(píng)論 4 324
  • 正文 年R本政府宣布,位于F島的核電站军俊,受9級(jí)特大地震影響侥加,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜粪躬,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,293評(píng)論 3 307
  • 文/蒙蒙 一担败、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧镰官,春花似錦提前、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,289評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至警检,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間害淤,已是汗流浹背扇雕。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,517評(píng)論 1 262
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留窥摄,地道東北人镶奉。 一個(gè)月前我還...
    沈念sama閱讀 45,547評(píng)論 2 354
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像崭放,于是被迫代替她去往敵國(guó)和親哨苛。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,834評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容

  • 恐懼的最初來(lái)源于人類(lèi)進(jìn)化種的避險(xiǎn)需求币砂。心理上的恐懼其實(shí)和任何具體的建峭、真正迫在眉睫的危險(xiǎn)無(wú)關(guān)。心理上的恐懼總是源于“...
    王增利閱讀 388評(píng)論 0 0
  • 今天佳娃老師給我們分享了財(cái)富是什么决摧?財(cái)富包括了所有亿蒸,精神財(cái)富,物質(zhì)財(cái)富掌桩,我們身休每一個(gè)部位都是財(cái)富边锁,包括健康、美麗...
    辛勒換成果閱讀 211評(píng)論 0 0
  • 【日更123】 今天在看有關(guān)“概率論”的講課視頻波岛,想補(bǔ)上曾經(jīng)沒(méi)有好好上過(guò)的課茅坛。要是在學(xué)生時(shí)代早知道會(huì)有這一天,那當(dāng)...
    唐斬2086閱讀 164評(píng)論 0 1