ใช้ train_test_split ให้ถูกต้อง
การแบ่งชั้น random_state และสัดส่วน
ใช้ train_test_split ให้ถูกต้อง เป็นบทเรียน Data Science Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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_splitFour 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 ให้ถูกต้อง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Data Science Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Data Science Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ใช้ train_test_split ให้ถูกต้อง”
การแบ่งชั้น random_state และสัดส่วน คุณปฏิบัติ Data Science Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Data Science Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Data Science Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “ใช้ train_test_split ให้ถูกต้อง” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Data Science Academy นี้ได้ไหม
ได้ บทเรียน Data Science Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เหตุใดจึงต้องกันชุดทดสอบไว้
- ใช้ train_test_split ให้ถูกต้อง
- การตรวจสอบไขว้แบบ K-Fold
- หยุดการรั่วไหลของข้อมูลตั้งแต่เริ่มต้น