0Pricing
Data Science Academy · 강의

train_test_split 제대로 사용하기

계층화, random_state, 비율을 알아봅니다

train_test_split 제대로 사용하기은(는) CoddyKit의 무료 Data Science Academy 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 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/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Data Science Academy 강의 전체를 잠금 해제할 수 있습니다. Data Science Academy 강의에는 총 4개의 강의가 포함되어 있습니다.

“train_test_split 제대로 사용하기”에서 뭘 배우나요?

계층화, random_state, 비율을 알아봅니다 브라우저에서 직접 실행하는 실습 코드로 Data Science Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Data Science Academy을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Data Science Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.

“train_test_split 제대로 사용하기” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Data Science Academy 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Data Science Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. 테스트 세트를 따로 두는 이유
  2. train_test_split 제대로 사용하기
  3. K-겹 교차 검증
  4. 데이터 누수를 시작 전에 막기
← Data Science Academy(으)로 돌아가기