2019-01-20

本周學(xué)習(xí)回顧:

主要學(xué)習(xí)了驗證模型好壞矫钓,方法有準(zhǔn)確率accuracy,precision铁瞒,recall妙色,f1score,fbetascore慧耍;將數(shù)據(jù)集拆分為訓(xùn)練集和測試集身辨,訓(xùn)練模型和驗證模型的好壞;用sklearn中Grid Search的方法選出模型的最佳參數(shù)芍碧。本周和下周都在做第一個項目煌珊。

Recap

In this lesson, you got a glimpse at the ways that we can measure how well our models are performing.

Training & Testing Data

First, it is important to?always?split your data into training and testing. Then you will measure how well your model performs on the test set of data after being fit training data.

Classification Measures

If you are fitting your model to predict categorical data (spam not spam), there are different measures to understand how well your model is performing than if you are predicting numeric values (the price of a home).

As we look at classification metrics, note that the?wikipedia page?on this topic is wonderful, but also a bit daunting. I frequently use it to remember which metric does what.

Specifically, you saw how to calculate:

Accuracy

Accuracy is often used to compare models, as it tells us the proportion of observations we correctly labeled.


Often accuracy is not the only metric you should be optimizing on. This is especially the case when you have class imbalance in your data. Optimizing on only accuracy can be misleading in how well your model is truly performing. With that in mind, you saw some additional metrics.

Precision

Precision focuses on the?predicted?"positive" values in your dataset. By optimizing based on precision values, you are determining if you are doing a good job of predicting the positive values, as compared to predicting negative values as positive.


Recall

Recall focuses on the?actual?"positive" values in your dataset. By optimizing based on recall values, you are determining if you are doing a good job of predicting the positive values?without?regard of how you are doing on the?actual?negative values. If you want to perform something similar to recall on the?actual?'negative' values, this is called?specificity.


F-Beta Score

In order to look at a combination of metrics at the same time, there are some common techniques like the F-Beta Score (where the F1 score is frequently used), as well as the ROC and AUC. You can see that the?\betaβ?parameter controls the degree to which precision is weighed into the F score, which allows precision and recall to be considered simultaneously. The most common value for beta is 1, as this is where you are finding the harmonic average between precision and recall.


ROC Curve & AUC

By finding different thresholds for our classification metrics, we can measure the area under the curve (where the curve is known as a ROC curve). Similar to each of the other metrics above, when the AUC is higher (closer to 1), this suggests that our model performance is better than when our metric is close to 0.


You may end up choosing to optimize on any of these measures. I commonly end up using AUC or an F1 score in practice. However, there are always reason to choose one measure over another depending on your situation.

Regression Measures

You want to measure how well your algorithms are performing on predicting numeric values? In these cases, there are three main metrics that are frequently used.?mean absolute error,?mean squared error, and?r2?values.

As an important note, optimizing on the mean absolute error may lead to a different 'best model' than if you optimize on the mean squared error. However, optimizing on the mean squared error will?always?lead to the same 'best' model as if you were to optimize on the?r2?value.

Again, if you choose a model with the best r2 value (the highest), it will also be the model that has the lowest (MSE). Choosing one versus another is based on which one you feel most comfortable explaining to someone else.

Mean Absolute Error (MAE)

The first metric you saw was the mean absolute error. This is a useful metric to optimize on when the value you are trying to predict follows a skewed distribution. Optimizing on an absolute value is particularly helpful in these cases because outliers will not influence models attempting to optimize on this metric as much as if you use the mean squared error. The optimal value for this technique is the median value. When you optimize for the R2 value of the mean squared error, the optimal value is actually the mean.


Mean-Squared Error (MSE)

The mean squared error is by far the most used metric for optimization in regression problems. Similar to with MAE, you want to find a model that minimizes this value. This metric can be greatly impacted by skewed distributions and outliers. When a model is considered optimal via MAE, but not for MSE, it is useful to keep this in mind. In many cases, it is easier to actually optimize on MSE, as the a quadratic term is differentiable. However, an absolute value is not differentiable. This factor makes this metric better for gradient based optimization algorithms.


R2 Score

Finally, the r2 value is another common metric when looking at regression values. Optimizing a model to have the lowest MSE will also optimize a model to have the the highest R2 value. This is a convenient feature of this metric. The R2 value is frequently interpreted as the 'amount of variability' captured by a model. Therefore, you can think of MSE, as the average amount you miss by across all the points, and the R2 value as the amount of the variability in the points that you capture with a model.



Grid Search in sklearn

Grid Search in sklearn is very simple. We'll illustrate it with an example. Let's say we'd like to train a support vector machine, and we'd like to decide between the following parameters:

kernel:?poly?or?rbf.

C: 0.1, 1, or 10.

(Note:?These parameters can be used as a black box now, but we'll see them in detail in the?Supervised Learning Section?of the nanodegree.)

The steps are the following:

1. Import GridSearchCV

fromsklearn.model_selectionimportGridSearchCV

2. Select the parameters:

Here we pick what are the parameters we want to choose from, and form a dictionary. In this dictionary, the keys will be the names of the parameters, and the values will be the lists of possible values for each parameter.

parameters = {'kernel':['poly','rbf'],'C':[0.1,1,10]}

3. Create a scorer.

We need to decide what metric we'll use to score each of the candidate models. In here, we'll use F1 Score.

fromsklearn.metrics import make_scorerfromsklearn.metrics import f1_scorescorer = make_scorer(f1_score)

4. Create a GridSearch Object with the parameters, and the scorer. Use this object to fit the data.

# Create the object.grid_obj= GridSearchCV(clf, parameters, scoring=scorer)# Fit the datagrid_fit= grid_obj.fit(X, y)

5. Get the best estimator.

best_clf= grid_fit.best_estimator_

Now you can use this estimator?best_clf?to make the predictions.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市泌豆,隨后出現(xiàn)的幾起案子定庵,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,039評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蔬浙,死亡現(xiàn)場離奇詭異猪落,居然都是意外死亡,警方通過查閱死者的電腦和手機畴博,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評論 3 395
  • 文/潘曉璐 我一進(jìn)店門笨忌,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人俱病,你說我怎么就攤上這事官疲。” “怎么了庶艾?”我有些...
    開封第一講書人閱讀 165,417評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長擎勘。 經(jīng)常有香客問我咱揍,道長,這世上最難降的妖魔是什么棚饵? 我笑而不...
    開封第一講書人閱讀 58,868評論 1 295
  • 正文 為了忘掉前任煤裙,我火速辦了婚禮,結(jié)果婚禮上噪漾,老公的妹妹穿的比我還像新娘硼砰。我一直安慰自己,他們只是感情好欣硼,可當(dāng)我...
    茶點故事閱讀 67,892評論 6 392
  • 文/花漫 我一把揭開白布题翰。 她就那樣靜靜地躺著,像睡著了一般诈胜。 火紅的嫁衣襯著肌膚如雪豹障。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,692評論 1 305
  • 那天焦匈,我揣著相機與錄音血公,去河邊找鬼。 笑死缓熟,一個胖子當(dāng)著我的面吹牛累魔,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播够滑,決...
    沈念sama閱讀 40,416評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼垦写,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了彰触?” 一聲冷哼從身側(cè)響起梯澜,我...
    開封第一講書人閱讀 39,326評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后晚伙,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體吮龄,經(jīng)...
    沈念sama閱讀 45,782評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,957評論 3 337
  • 正文 我和宋清朗相戀三年咆疗,在試婚紗的時候發(fā)現(xiàn)自己被綠了漓帚。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,102評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡午磁,死狀恐怖尝抖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情迅皇,我是刑警寧澤昧辽,帶...
    沈念sama閱讀 35,790評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站登颓,受9級特大地震影響搅荞,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜框咙,卻給世界環(huán)境...
    茶點故事閱讀 41,442評論 3 331
  • 文/蒙蒙 一咕痛、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧喇嘱,春花似錦茉贡、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至作烟,卻和暖如春悔据,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背俗壹。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評論 1 272
  • 我被黑心中介騙來泰國打工科汗, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人绷雏。 一個月前我還...
    沈念sama閱讀 48,332評論 3 373
  • 正文 我出身青樓头滔,卻偏偏與公主長得像,于是被迫代替她去往敵國和親涎显。 傳聞我的和親對象是個殘疾皇子坤检,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,044評論 2 355

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

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,334評論 0 10
  • 今天早上8點多踩著10厘米高根鞋出門,晚上9點多進(jìn)家門期吓,期盼已久的年會終于舉行早歇,作為主持人,演3個節(jié)目,我玩嗨了箭跳,...
    水木的小院閱讀 317評論 7 4
  • 宋依哲暑期班考級計劃 依哲考八級晨另,音階學(xué)完,練習(xí)曲第一頁學(xué)完谱姓。第二項第一頁的一半學(xué)完借尿。 暑假前練習(xí)曲第一頁彈熟背譜...
    A0天翼音樂閱讀 205評論 0 0