1. 程式人生 > >sklearn中的交叉驗證(Cross-Validation)

sklearn中的交叉驗證(Cross-Validation)

這個repo 用來記錄一些python技巧、書籍、學習連結等,歡迎star

sklearn是利用python進行機器學習中一個非常全面和好用的第三方庫,用過的都說好。今天主要記錄一下sklearn中關於交叉驗證的各種用法,主要是對sklearn官方文件 Cross-validation: evaluating estimator performance進行講解,英文水平好的建議讀官方文件,裡面的知識點很詳細。

先匯入需要的庫及資料集

In [1]: import numpy as np

In [2]: from sklearn.model_selection import train_test_split

In
[3]: from sklearn.datasets import load_iris In [4]: from sklearn import svm In [5]: iris = load_iris() In [6]: iris.data.shape, iris.target.shape Out[6]: ((150, 4), (150,))

1.train_test_split

對資料集進行快速打亂(分為訓練集和測試集)

這裡相當於對資料集進行了shuffle後按照給定的test_size 進行資料集劃分。

In [7]: X_train, X_test, y_train, y_test = train_test_split(
   ...:         iris.data, iris.target, test_size=.4
, random_state=0) #這裡是按照6:4對訓練集測試集進行劃分 In [8]: X_train.shape, y_train.shape Out[8]: ((90, 4), (90,)) In [9]: X_test.shape, y_test.shape Out[9]: ((60, 4), (60,)) In [10]: iris.data[:5] Out[10]: array([[ 5.1, 3.5, 1.4, 0.2], [ 4.9, 3. , 1.4, 0.2], [ 4.7, 3.2, 1.3, 0.2], [ 4.6, 3.1, 1.5, 0.2], [ 5. , 3.6, 1.4, 0.2]]
) In [11]: X_train[:5] Out[11]: array([[ 6. , 3.4, 4.5, 1.6], [ 4.8, 3.1, 1.6, 0.2], [ 5.8, 2.7, 5.1, 1.9], [ 5.6, 2.7, 4.2, 1.3], [ 5.6, 2.9, 3.6, 1.3]]) In [12]: clf = svm.SVC(kernel='linear', C=1).fit(X_train, y_train) In [13]: clf.score(X_test, y_test) Out[13]: 0.96666666666666667

2.cross_val_score

對資料集進行指定次數的交叉驗證併為每次驗證效果評測

其中,score 預設是以 scoring=’f1_macro’進行評測的,餘外針對分類或迴歸還有:
這裡寫圖片描述
這需要from sklearn import metrics ,通過在cross_val_score 指定引數來設定評測標準;
cv 指定為int 型別時,預設使用KFoldStratifiedKFold 進行資料集打亂,下面會對KFoldStratifiedKFold 進行介紹。


In [15]: from sklearn.model_selection import cross_val_score

In [16]: clf = svm.SVC(kernel='linear', C=1)

In [17]: scores = cross_val_score(clf, iris.data, iris.target, cv=5)

In [18]: scores
Out[18]: array([ 0.96666667,  1.        ,  0.96666667,  0.96666667,  1.        ])

In [19]: scores.mean()
Out[19]: 0.98000000000000009

除使用預設交叉驗證方式外,可以對交叉驗證方式進行指定,如驗證次數,訓練集測試集劃分比例等

In [20]: from sklearn.model_selection import ShuffleSplit

In [21]: n_samples = iris.data.shape[0]

In [22]: cv = ShuffleSplit(n_splits=3, test_size=.3, random_state=0)

In [23]: cross_val_score(clf, iris.data, iris.target, cv=cv)
Out[23]: array([ 0.97777778,  0.97777778,  1.        ])

cross_val_score 中同樣可使用pipeline 進行流水線操作

In [24]: from sklearn import preprocessing

In [25]: from sklearn.pipeline import make_pipeline

In [26]: clf = make_pipeline(preprocessing.StandardScaler(), svm.SVC(C=1))

In [27]: cross_val_score(clf, iris.data, iris.target, cv=cv)
Out[27]: array([ 0.97777778,  0.93333333,  0.95555556])

3.cross_val_predict

cross_val_predictcross_val_score 很相像,不過不同於返回的是評測效果,cross_val_predict 返回的是estimator 的分類結果(或迴歸值),這個對於後期模型的改善很重要,可以通過該預測輸出對比實際目標值,準確定位到預測出錯的地方,為我們引數優化及問題排查十分的重要。

In [28]: from sklearn.model_selection import cross_val_predict

In [29]: from sklearn import metrics

In [30]: predicted = cross_val_predict(clf, iris.data, iris.target, cv=10)

In [31]: predicted
Out[31]: 
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])

In [32]: metrics.accuracy_score(iris.target, predicted)
Out[32]: 0.96666666666666667

4.KFold

K折交叉驗證,這是將資料集分成K份的官方給定方案,所謂K折就是將資料集通過K次分割,使得所有資料既在訓練集出現過,又在測試集出現過,當然,每次分割中不會有重疊。相當於無放回抽樣。

In [33]: from sklearn.model_selection import KFold

In [34]: X = ['a','b','c','d']

In [35]: kf = KFold(n_splits=2)

In [36]: for train, test in kf.split(X):
    ...:     print train, test
    ...:     print np.array(X)[train], np.array(X)[test]
    ...:     print '\n'
    ...:     
[2 3] [0 1]
['c' 'd'] ['a' 'b']


[0 1] [2 3]
['a' 'b'] ['c' 'd']

5.LeaveOneOut

LeaveOneOut 其實就是KFold 的一個特例,因為使用次數比較多,因此獨立的定義出來,完全可以通過KFold 實現。


In [37]: from sklearn.model_selection import LeaveOneOut

In [38]: X = [1,2,3,4]

In [39]: loo = LeaveOneOut()

In [41]: for train, test in loo.split(X):
    ...:     print train, test
    ...:     
[1 2 3] [0]
[0 2 3] [1]
[0 1 3] [2]
[0 1 2] [3]


#使用KFold實現LeaveOneOtut
In [42]: kf = KFold(n_splits=len(X))

In [43]: for train, test in kf.split(X):
    ...:     print train, test
    ...:     
[1 2 3] [0]
[0 2 3] [1]
[0 1 3] [2]
[0 1 2] [3]

6.LeavePOut

這個也是KFold 的一個特例,用KFold 實現起來稍麻煩些,跟LeaveOneOut 也很像。


In [44]: from sklearn.model_selection import LeavePOut

In [45]: X = np.ones(4)

In [46]: lpo = LeavePOut(p=2)

In [47]: for train, test in lpo.split(X):
    ...:     print train, test
    ...:     
[2 3] [0 1]
[1 3] [0 2]
[1 2] [0 3]
[0 3] [1 2]
[0 2] [1 3]
[0 1] [2 3]

7.ShuffleSplit

ShuffleSplit 咋一看用法跟LeavePOut 很像,其實兩者完全不一樣,LeavePOut 是使得資料集經過數次分割後,所有的測試集出現的元素的集合即是完整的資料集,即無放回的抽樣,而ShuffleSplit 則是有放回的抽樣,只能說經過一個足夠大的抽樣次數後,保證測試集出現了完成的資料集的倍數。

In [48]: from sklearn.model_selection import ShuffleSplit

In [49]: X = np.arange(5)

In [50]: ss = ShuffleSplit(n_splits=3, test_size=.25, random_state=0)

In [51]: for train_index, test_index in ss.split(X):
    ...:     print train_index, test_index
    ...:     
[1 3 4] [2 0]
[1 4 3] [0 2]
[4 0 2] [1 3]

8.StratifiedKFold

這個就比較好玩了,通過指定分組,對測試集進行無放回抽樣。

In [52]: from sklearn.model_selection import StratifiedKFold

In [53]: X = np.ones(10)

In [54]: y = [0,0,0,0,1,1,1,1,1,1]

In [55]: skf = StratifiedKFold(n_splits=3)

In [56]: for train, test in skf.split(X,y):
    ...:     print train, test
    ...:     
[2 3 6 7 8 9] [0 1 4 5]
[0 1 3 4 5 8 9] [2 6 7]
[0 1 2 4 5 6 7] [3 8 9]

9.GroupKFold

這個跟StratifiedKFold 比較像,不過測試集是按照一定分組進行打亂的,即先分堆,然後把這些堆打亂,每個堆裡的順序還是固定不變的。

In [57]: from sklearn.model_selection import GroupKFold

In [58]: X = [.1, .2, 2.2, 2.4, 2.3, 4.55, 5.8, 8.8, 9, 10]

In [59]: y = ['a','b','b','b','c','c','c','d','d','d']

In [60]: groups = [1,1,1,2,2,2,3,3,3,3]

In [61]: gkf = GroupKFold(n_splits=3)

In [62]: for train, test in gkf.split(X,y,groups=groups):
    ...:     print train, test
    ...:     
[0 1 2 3 4 5] [6 7 8 9]
[0 1 2 6 7 8 9] [3 4 5]
[3 4 5 6 7 8 9] [0 1 2]

10.LeaveOneGroupOut

這個是在GroupKFold 上的基礎上混亂度又減小了,按照給定的分組方式將測試集分割下來。

In [63]: from sklearn.model_selection import LeaveOneGroupOut

In [64]: X = [1, 5, 10, 50, 60, 70, 80]

In [65]: y = [0, 1, 1, 2, 2, 2, 2]

In [66]: groups = [1, 1, 2, 2, 3, 3, 3]

In [67]: logo = LeaveOneGroupOut()

In [68]: for train, test in logo.split(X, y, groups=groups):
    ...:     print train, test
    ...:     
[2 3 4 5 6] [0 1]
[0 1 4 5 6] [2 3]
[0 1 2 3] [4 5 6]

11.LeavePGroupsOut

這個沒啥可說的,跟上面那個一樣,只是一個是單組,一個是多組

from sklearn.model_selection import LeavePGroupsOut

X = np.arange(6)

y = [1, 1, 1, 2, 2, 2]

groups = [1, 1, 2, 2, 3, 3]

lpgo = LeavePGroupsOut(n_groups=2)

for train, test in lpgo.split(X, y, groups=groups):
    print train, test

[4 5] [0 1 2 3]
[2 3] [0 1 4 5]
[0 1] [2 3 4 5]

12.GroupShuffleSplit

這個是有放回抽樣

In [75]: from sklearn.model_selection import GroupShuffleSplit

In [76]: X = [.1, .2, 2.2, 2.4, 2.3, 4.55, 5.8, .001]

In [77]: y = ['a', 'b','b', 'b', 'c','c', 'c', 'a']

In [78]: groups = [1,1,2,2,3,3,4,4]

In [79]: gss = GroupShuffleSplit(n_splits=4, test_size=.5, random_state=0)

In [80]: for train, test in gss.split(X, y, groups=groups):
    ...:     print train, test
    ...:     
[0 1 2 3] [4 5 6 7]
[2 3 6 7] [0 1 4 5]
[2 3 4 5] [0 1 6 7]
[4 5 6 7] [0 1 2 3]

13.TimeSeriesSplit

針對時間序列的處理,防止未來資料的使用,分割時是將資料進行從前到後切割(這個說法其實不太恰當,因為切割是延續性的。。)

In [81]: from sklearn.model_selection import TimeSeriesSplit

In [82]: X = np.array([[1,2],[3,4],[1,2],[3,4],[1,2],[3,4]])

In [83]: tscv = TimeSeriesSplit(n_splits=3)

In [84]: for train, test in tscv.split(X):
    ...:     print train, test
    ...:     
[0 1 2] [3]
[0 1 2 3] [4]
[0 1 2 3 4] [5]

這個repo 用來記錄一些python技巧、書籍、學習連結等,歡迎star