Sorry for not writing for a long time. But this code is only suitable for dividing data into training and validation. As I wrote in my question, i need to split my dataset equivalently. The number of samples for each class should be equal in both dataset (train and validation).

For example train dataset should contain 10 000 samples (5000 for 0 class and 5000 for 1), validation dataset - 2000 samples (1000 for 0, and 1000 for 1).