train_test_split関数を使ってデータ分割
2020-01-30
2020-01-30
前置き
機械学習では、通常に単一のデータセットを2つのサブデータに分割する:トレーニングデータとテストデータ。時にはトレーニングと検証、テストの3つのサブデータに分割します。トレーニングデータは、モデルを構築するためのものです。テストデータは、未知のデータに対してモデルを使用して、モデルのパフォーマンスを評価するためのものです。scikit-learnライブラリのtrain_test_split関数を使用して、データ分割ができます。
基本的な使い方
```>>> from sklearn.model_selection import train_test_split
>>>
>>> x = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
>>> y = [0, 1, 0, 0, 0, 1, 1, 0, 1, 1]
>>>
>>> x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
>>>
>>> x_train
['i', 'g', 'h', 'a', 'f', 'c', 'b', 'j']
>>> y_train
[1, 1, 0, 0, 1, 0, 1, 1]
>>> x_test
['e', 'd']
>>> y_test
[0, 0]
```>>>
>>> x = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
>>> y = [0, 1, 0, 0, 0, 1, 1, 0, 1, 1]
>>>
>>> x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
>>>
>>> x_train
['i', 'g', 'h', 'a', 'f', 'c', 'b', 'j']
>>> y_train
[1, 1, 0, 0, 1, 0, 1, 1]
>>> x_test
['e', 'd']
>>> y_test
[0, 0]