# Predict Function & Generating a Training Set

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

• Check my resource function CrossValidateModel. Commented May 2, 2022 at 17:04
• @SjoerdSmit Did not know about that WFR function -- thanks for sharing! Commented May 3, 2022 at 14:19
• I tend to use the TrainTestSplit resource function: resources.wolframcloud.com/FunctionRepository/resources/… (but @SjoerdSmit 's CrossValidateModel is also very nice) Commented May 4, 2022 at 0:42
• 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. Commented May 28, 2023 at 12:34
• There is a checkmark next to (each) answer. You can click on it to accept that answer. You can also upvote answers.
– Syed
Commented Jan 23 at 12:31

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" -> {}|>}];


To get the first 3 rows:

d[[;;3]]


To get the last 3 rows:

d[[-3 ;; -1]]

To get the first 30%:

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

• 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. Commented May 4, 2022 at 17:39