trainTestSplit¶
Purpose¶
Returns test and training splits for a given set of dependent and independent variables.
Format¶

{ y_train, y_test, X_train, X_test } =
trainTestSplit
(y, X, train_pct)¶  Parameters
y (Nx1 vector, or NxK matrix.) – The dependent variables.
X (Nx1 vector, or NxP matrix.) – The independent variables.
train_pct (Scalar) – The percentage of observations to include in the training set.
 Returns
y_train – The (train_pct * N) observations from the original y which correspond to the observations selected for X_train.
y_test – The remaining observations from the original y not selected for the training set.
X_train – (train_pct * N) x P matrix of independent variables.
X_test – The remaining observations from the original X which were not selected to be in the training set.
Examples¶
// Set seed for repeatable sampling
rndseed 23324;
y = { 7, 2, 5, 1, 3, 4 };
X = { 1 3,
9 6,
6 1,
8 4,
9 5,
1 8 };
// Shuffle data and create training set with 2/3 of
// the observations and 1/3 for the test set
{ y_train, y_test, X_train, X_test } = trainTestSplit(y, X, 0.67);
After the above code:
y_train = 3 X_train = 9 5
7 1 3
1 8 4
4 1 8
y_test = 2 X_test = 9 6
5 6 1
Remarks¶
The observations from X and y are first randomly shuffled such that the corresponding rows of X and y are kept together. For repeatable shuffling, use the rndseed
keyword before calling trainTestSplit()
.
See also
Functions cvSplit()
, rndi()
, sampleData()
, splitData()