Toolkit
Here you can find the toolkit functions on top of DataChain for common DS/ML operations (e.g. train/test split). Import these functions from datachain.toolkit.
toolkit
train_test_split
Splits a DataChain into multiple subsets based on the provided weights.
This function partitions the rows or items of a DataChain into disjoint subsets, ensuring that the relative sizes of the subsets correspond to the given weights. It is particularly useful for creating training, validation, and test datasets.
Parameters:
-
dc(DataChain) –The DataChain instance to split.
-
weights(list[float]) –A list of weights indicating the relative proportions of the splits. The weights do not need to sum to 1; they will be normalized internally. For example: -
[0.7, 0.3]corresponds to a 70/30 split; -[2, 1, 1]corresponds to a 50/25/25 split. -
seed(int, default:None) –The seed for the random number generator. Defaults to None.
Returns:
-
list[DataChain]–list[DataChain]: A list of DataChain instances, one for each weight in the weights list.
Examples:
Train-test split:
Train-test-validation split:
Note
Splits reuse the same best-effort shuffle used by DataChain.shuffle. Results
are typically repeatable, but earlier operations such as merge, union, or
custom SQL that reshuffle rows can change the outcome between runs. Add order by
stable keys first when you need strict reproducibility.