Config Router

  • Google Sheets
  • CCNA Online training
    • CCNA
  • CISCO Lab Guides
    • CCNA Security Lab Manual With Solutions
    • CCNP Route Lab Manual with Solutions
    • CCNP Switch Lab Manual with Solutions
  • Juniper
  • Linux
  • DevOps Tutorials
  • Python Array
You are here: Home / How to split data into training/testing sets using sample function

How to split data into training/testing sets using sample function

August 19, 2021 by James Palmer

There are numerous approaches to achieve data partitioning. For a more complete approach take a look at the createDataPartition function in the caTools package.
Here is a simple example:
data(mtcars)

## 75% of the sample size
smp_size <- floor(0.75 * nrow(mtcars)) ## set the seed to make your partition reproducible set.seed(123) train_ind <- sample(seq_len(nrow(mtcars)), size = smp_size) train <- mtcars[train_ind, ] test <- mtcars[-train_ind, ] It can be easily done by: set.seed(101) # Set Seed so that same sample can be reproduced in future also # Now Selecting 75% of data as sample from total 'n' rows of the data sample <- sample.int(n = nrow(data), size = floor(.75*nrow(data)), replace = F) train <- data[sample, ] test <- data[-sample, ] By using caTools package: require(caTools) set.seed(101) sample = sample.split(data$anycolumn, SplitRatio = .75) train = subset(data, sample == TRUE) test = subset(data, sample == FALSE)

Related

Filed Under: Uncategorized

Recent Posts

  • How do I give user access to Jenkins?
  • What is docker volume command?
  • What is the date format in Unix?
  • What is the difference between ARG and ENV Docker?
  • What is rsync command Linux?
  • How to Add Music to Snapchat 2021 Android? | How to Search, Add, Share Songs on Snapchat Story?
  • How to Enable Snapchat Notifications for Android & iPhone? | Steps to Turn on Snapchat Bitmoji Notification
  • Easy Methods to Fix Snapchat Camera Not Working Black Screen Issue | Reasons & Troubleshooting Tips to Solve Snapchat Camera Problems
  • Detailed Procedure for How to Update Snapchat on iOS 14 for Free
  • What is Snapchat Spotlight Feature? How to Make a Spotlight on Snapchat?
  • Snapchat Hack Tutorial 2021: Can I hack a Snapchat Account without them knowing?

Copyright © 2025 · News Pro Theme on Genesis Framework · WordPress · Log in