I am trying to use the Predict[] function to do a regression analysis. I have the data imported from a CSV and based on my past Python experience the libraries I used always have a slick way to take a percentage of the data and use it as a training set.

Does Mathematica have any function to take a portion of a dataset and use it for training? Most of the examples I can find are using ExampleData where this step is already done.

Thank You

  • 5
    $\begingroup$ Check my resource function CrossValidateModel. $\endgroup$ May 2, 2022 at 17:04
  • $\begingroup$ @SjoerdSmit Did not know about that WFR function -- thanks for sharing! $\endgroup$ May 3, 2022 at 14:19
  • 1
    $\begingroup$ I tend to use the TrainTestSplit resource function: resources.wolframcloud.com/FunctionRepository/resources/… (but @SjoerdSmit 's CrossValidateModel is also very nice) $\endgroup$ May 4, 2022 at 0:42
  • $\begingroup$ Another note I'd like to make is that Predict and Classify already do training-test validation internally. Most of the time you don't need to do this manually. $\endgroup$ May 28, 2023 at 12:34
  • $\begingroup$ There is a checkmark next to (each) answer. You can click on it to accept that answer. You can also upvote answers. $\endgroup$
    – Syed
    Jan 23 at 12:31

1 Answer 1


To take only some piece of a dataset works the same like for matrices or arrays. E.g.:

d = Dataset[{
   <|"a" -> 1, "b" -> "x", "c" -> {1}|>,
   <|"a" -> 2, "b" -> "y", "c" -> {2, 3}|>,
   <|"a" -> 3, "b" -> "z", "c" -> {3}|>,
   <|"a" -> 4, "b" -> "x", "c" -> {4, 5}|>,
   <|"a" -> 5, "b" -> "y", "c" -> {5, 6, 7}|>,
   <|"a" -> 6, "b" -> "z", "c" -> {}|>}];

enter image description here

To get the first 3 rows:


enter image description here

To get the last 3 rows:

d[[-3 ;; -1]]

To get the first 30%:

d[[;; Round[Length[d] 0.3]]]

enter image description here

  • $\begingroup$ Thanks for all the replies. CrossValidateModel, TrainTestSplit, and direct parsing of the Dataset are all good options and answer what I was looking for. Thank you all. $\endgroup$
    – wpilgri
    May 4, 2022 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.