Function to shuffle samples and labels in sync
| 70 | |
| 71 | // Function to shuffle samples and labels in sync |
| 72 | void shuffle_samples_and_labels(std::vector<matrix<int, 0, 1>>& samples, std::vector<unsigned long>& labels) { |
| 73 | std::vector<size_t> indices(samples.size()); |
| 74 | std::iota(indices.begin(), indices.end(), 0); // Fill with 0, 1, 2, ..., N-1 |
| 75 | std::shuffle(indices.begin(), indices.end(), std::default_random_engine{}); |
| 76 | |
| 77 | // Create temporary vectors to hold shuffled data |
| 78 | std::vector<matrix<int, 0, 1>> shuffled_samples(samples.size()); |
| 79 | std::vector<unsigned long> shuffled_labels(labels.size()); |
| 80 | |
| 81 | // Apply the shuffle |
| 82 | for (size_t i = 0; i < indices.size(); ++i) |
| 83 | { |
| 84 | shuffled_samples[i] = samples[indices[i]]; |
| 85 | shuffled_labels[i] = labels[indices[i]]; |
| 86 | } |
| 87 | |
| 88 | // Replace the original data with shuffled data |
| 89 | samples = std::move(shuffled_samples); |
| 90 | labels = std::move(shuffled_labels); |
| 91 | } |
| 92 | |
| 93 | // ---------------------------------------------------------------------------------------- |
| 94 |