0Pricing
Data Science Academy · レッスン

train_test_splitを正しく使う

stratify、random_state、比率

「train_test_splitを正しく使う」はCoddyKit上の無料Data Science Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはData Science Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Data Science Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

One Helper, Two Sets

The function train_test_split takes your features and target and hands back train and test pieces in one tidy call.

from sklearn.model_selection import train_test_split

Four Things Come Back

It returns four objects in a fixed order: train features, test features, train target, test target. Unpack them carefully so each lands in the right name.

X_train, X_test, y_train, y_test = train_test_split(X, y)

Set the Test Size

Use test_size to choose how much data to reserve. A value of 0.2 means 20% goes to the test set.

train_test_split(X, y, test_size=0.2)

Shuffling by Default

By default the rows are shuffled before splitting. That mixing prevents any hidden order in your file from biasing either set.

Make It Reproducible

Pass random_state to lock the shuffle. The same number gives the same split every run, so your results are repeatable. 🔁

train_test_split(X, y, test_size=0.2, random_state=42)

Why Reproducibility Matters

Without a fixed seed, every run reshuffles and your scores wobble. A locked random_state lets teammates reproduce your exact numbers.

The Imbalance Problem

If a class is rare, a plain random split might dump most of it into one set. Now train and test no longer reflect the same class balance.

Stratify to the Rescue

Pass stratify equal to your target so both sets keep the same class proportions. Essential for classification with uneven classes.

train_test_split(X, y, test_size=0.2, stratify=y, random_state=42)

Keep X and y Aligned

The split keeps each row's features and label together. Row 7's features always travel with row 7's label, never scrambled apart.

Choosing the Ratio

Common splits are 80/20 or 70/30. With lots of data you can spare a smaller test slice; with little data, give the test set a bit more.

Split Before You Touch

Run the split first, before scaling or filling values. Doing prep on the full set leaks test information back into training.

Quick Check

Your target classes are very imbalanced. Which argument helps?

Recap

Unpack four sets, set test_size, fix random_state for repeatability, and use stratify when classes are uneven. Split first, prep later. 🔁

よくある質問

「train_test_splitを正しく使う」レッスンは無料ですか?

はい。「train_test_splitを正しく使う」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Data Science Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Data Science Academyコースには全4レッスンが含まれています。

「train_test_splitを正しく使う」で何を学びますか?

stratify、random_state、比率 ブラウザで直接実行するハンズオンコードでData Science Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Data Science Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのData Science Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「train_test_splitを正しく使う」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このData Science Academyレッスンでコードを書いて実行できますか?

はい。すべてのData Science Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. テストセットを分けておく理由
  2. train_test_splitを正しく使う
  3. K分割交差検証
  4. データリークを未然に防ぐ
← Data Science Academyに戻る