pub fn train_test_split(
x: &Tensor,
train_ratio: f64,
) -> Result<(Tensor, Tensor), MattenMlprepError>Expand description
Splits the rows of a 2D tensor into (train, test) by an ordered,
deterministic partition — no shuffling.
n_train = floor(n_rows * train_ratio)
train = rows[0 .. n_train]
test = rows[n_train .. n_rows]The split is fully deterministic and reproducible. If you need a randomized split, shuffle the rows yourself first (a seeded variant is planned but not in this release; see RFC-024 §6).
§Errors
MattenMlprepError::ExpectedMatrixifxis not rank-2.MattenMlprepError::InvalidRatioiftrain_ratiois not finite or not in(0.0, 1.0).MattenMlprepError::EmptySplitiffloor(rows * train_ratio) == 0.MattenMlprepError::DynamicTensor(with thedynamicfeature) ifxis dynamic.
use matten::Tensor;
use matten_mlprep::train_test_split;
// 4 rows, 1 feature; 0.75 -> 3 train rows, 1 test row.
let x = Tensor::new(vec![10.0, 20.0, 30.0, 40.0], &[4, 1]);
let (train, test) = train_test_split(&x, 0.75).unwrap();
assert_eq!(train.shape(), &[3, 1]);
assert_eq!(test.shape(), &[1, 1]);
assert_eq!(train.as_slice(), &[10.0, 20.0, 30.0]);
assert_eq!(test.as_slice(), &[40.0]);