トレーニングセットとテストセット

Pythonで学ぶMachine Learningの前処理

James Chapman

Curriculum Manager, DataCamp

分割の目的

 

  1. 過学習 の軽減
  2. ホールドアウトセットでの性能評価
Pythonで学ぶMachine Learningの前処理

データセットの分割

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
   X_train y_train            
0      1.0       n
1      4.0       n
       ...
5      5.0       n
6      6.0       n

   X_test y_test
0     9.0      y
1     1.0      n
2     4.0      n
Pythonで学ぶMachine Learningの前処理

層化サンプリング

 

  • $100$ サンプルのデータセット:クラス1 が $80$、クラス2 が $20$
  • $75$ サンプルのトレーニングセット:クラス1 が $60$、クラス2 が $15$
  • $25$ サンプルのテストセット:クラス1 が $20$、クラス2 が $5$
Pythonで学ぶMachine Learningの前処理

層化サンプリング

X_train,X_test,y_train,y_test = train_test_split(X, y, stratify=y, random_state=42)
y["labels"].value_counts()
class1    80
class2    20
Name: labels, dtype: int64
Pythonで学ぶMachine Learningの前処理

層化サンプリング

y_train["labels"].value_counts()
class1    60
class2    15
Name: labels, dtype: int64
y_test["labels"].value_counts()
class1    20
class2    5
Name: labels, dtype: int64
Pythonで学ぶMachine Learningの前処理

練習しましょう!

Pythonで学ぶMachine Learningの前処理

Preparing Video For Download...