機器學習應用建議(一)

決定下一步做什么

這里我們以房價預測為例,假設我們需要使用線性回歸模型對房價進行預測,其代價函數J(θ)如下圖所示。當我們將已經訓練好的模型用來預測房價時,我們發現有較大的誤差,那么我們下一步應該怎么做?

我們可能會想到以下幾種方法:

  • 獲取更多的樣本;
  • 嘗試減少特征變量的數量;
  • 嘗試獲取更多的特征變量;
  • 嘗試增加多項式特征;
  • 嘗試減小正則化參數λ的值;
  • 嘗試增大正則化參數λ的值;
    ......

這些方法可能有用也可能沒用,我們更不應該在實際應用中隨機選擇上述方法。還有一點要說明的是:上述方法中的任意一個方法,在具體實踐過程中都可能轉變為一個為期半年甚至時間更長的項目。因此,我們需要引入機器學習診斷法來幫助我們決定下一步該做什么才是有效的方法。

Question:
Which of the following statements about diagnostics are true? Check all that apply.
A. It's hard to tell what will work to improve a learning algorithm, so the best approach is to with gut feeling and just see what works.
B. Diagnostics can give guidance as to what might be more fruitful things to try to improve a learning algorithm.
C. Diagnostics can be time-consuming to implement and try, but they can still be a very good use of your time.
D. A diagnostics can sometimes rule out certain courses of action (changes to your learning algorithm) as being unlikely to improve its performance significantly.

我們不難選出B,C和D這三個正確答案。

假設評估

對于我們之前所提及的欠擬合問題和過擬合問題,我們是通過畫圖的方法來檢驗的。如若訓練集中有較多的特征變量時,我們就無法將函數圖給呈現出來。

對此,我們可以將數據集分為訓練集和測試集兩個部分,其中訓練集占數據集的70%,測試集占數據集的30%。首先,我們利用訓練集將代價函數J(θ)最小化,然后利用測試集測試模型誤差,從而出模型是否出現欠擬合或過擬合問題。

注:如果數據集有一定規律,則要從數據集中隨機選取70%的樣本作為訓練集,30%的樣本作為測試集。

線性回歸模型

  • 利用訓練集將代價函數J(θ)最小化,得到此時參數θ的值
  • 利用測試集計算誤差:

邏輯回歸模型

  • 利用訓練集將代價函數J(θ)最小化,得到此時參數θ的值
  • 利用測試集計算誤差:

除此之外,對于邏輯回歸模型我們還能計算誤分類率來幫助我們理解邏輯回歸模型的誤差。

誤分類率

因此,我們可將誤差測試的表達式改寫為:

Question:
Suppose an implementation of linear regression (without regularization) is badly overfitting the training set. In this case, we would expect:
A. The training error J(θ) to be low and the test error Jtest(θ) to be high
B. The training error J(θ) to be low and the test error Jtest(θ) to be low
C. The training error J(θ) to be high and the test error Jtest(θ) to be low
D. The training error J(θ) to be high and the test error Jtest(θ) to be high

我們不難選出A這個正確答案。

補充筆記
Evaluating a Hypothesis

Once we have done some trouble shooting for errors in our predictions by:

  • Getting more training examples
  • Trying smaller sets of features
  • Trying additional features
  • Trying polynomial features
  • Increasing or decreasing λ

We can move on to evaluate our new hypothesis.

A hypothesis may have a low error for the training examples but still be inaccurate (because of overfitting). Thus, to evaluate a hypothesis, given a dataset of training examples, we can split up the data into two sets: a training set and a test set. Typically, the training set consists of 70 % of your data and the test set is the remaining 30 %.

The new procedure using these two sets is then:

  1. Learn Θ and minimize Jtrain(Θ) using the training set
  2. Compute the test set error Jtest(Θ)

The test set error

  1. For linear regression:
  1. For classification ~ Misclassification error (aka 0/1 misclassification error):

This gives us a binary 0 or 1 error result based on a misclassification. The average test error for the test set is:

This gives us the proportion of the test data that was misclassified.

模型選擇以及訓練集、交叉驗證集和測試集的劃分

假設我們要在以下的多項式模型中選擇一個合適的模型:

  • (d = 1) hθ(x) = θ0 + θ1x
  • (d = 2) hθ(x) = θ0 + θ1x + θ2x2
  • (d = 3) hθ(x) = θ0 + θ1x + θ2x2 + θ3x3
    ......
  • (d = 10) hθ(x) = θ0 + θ1x + θ2x2 + θ3x3 + ... + θ10x10

其中,參數d表示多項式的次數。對于這種情況,我們使用將數據集劃分為訓練集和測試集的方法。

上圖中,我們假設d=5時其測試誤差最小。但這時我們只是找到了一個對于測試集非常擬合的模型,我們無法判斷其實際泛化誤差是否完美。

因此,我們不能再將數據集只分為兩部分,訓練集和測試集。對此,我們引入交叉驗證集(Validation Set)。我們將數據集分為三個部分,訓練集(60%)、交叉驗證集(20%)和測試集(20%)。

對于上例,我們需要計算三部分數據集的誤差。

最終,我們可得到d=4時測試誤差最小。

Question:
Consider the model selection procedure where we choose the degree of polynomial using a cross validation set. For the final model (with parameters θ), we might generally expect JCV(θ) To be lower than Jtest(θ) because:
A. An extra parameter (d, the degree of the polynomial) has been fit to the cross validation set.
B. An extra parameter (d, the degree of the polynomial) has been fit to the test set.
C. The cross validation set is usually smaller than the test set.
D. The cross validation set is usually larger than the test set.

終上所述,我們不難選出A這一正確答案。

補充筆記
Model Selection and Train/Validation/Test Sets

Just because a learning algorithm fits a training set well, that does not mean it is a good hypothesis. It could over fit and as a result your predictions on the test set would be poor. The error of your hypothesis as measured on the data set with which you trained the parameters will be lower than the error on any other data set.

Given many models with different polynomial degrees, we can use a systematic approach to identify the 'best' function. In order to choose the model of your hypothesis, you can test each degree of polynomial and look at the error result.

One way to break down our dataset into the three sets is:

  • Training set: 60%
  • Cross validation set: 20%
  • Test set: 20%

We can now calculate three separate error values for the three different sets using the following method:

  1. Optimize the parameters in Θ using the training set for each polynomial degree.
  2. Find the polynomial degree d with the least error using the cross validation set.
  3. Estimate the generalization error using the test set with Jtest(d)), (d = theta from polynomial with lower error);

This way, the degree of the polynomial d has not been trained using the test set.

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,106評論 6 542
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,441評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,211評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,736評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,475評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,834評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,829評論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,009評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,559評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,306評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,516評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,038評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,728評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,132評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,443評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,249評論 3 399
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,484評論 2 379

推薦閱讀更多精彩內容

  • 首先找到自己喜歡的圖片,測量好尺寸及比例關系,進行繪畫,對于彩色圖片不知道如何確定明暗關系,那就從網上下載素描的圖...
    我很壞987閱讀 617評論 1 3
  • 一切都是不是騙局呢,不是相互騙,就是自己騙自己。 弄出那么多的技術,高科技,技術有多牛,科技有多高,掌握了技術、科...
    zhaomo閱讀 380評論 0 1
  • MP,Book 1, FPA FPA 好了,今天的課展就到這里,那我要問問我們的爸爸媽媽。我們寶貝兒都表現的棒不...
    MEAnnie閱讀 182評論 0 0