Home  >  Article  >  How to shuffle rows of 2D array in Java?

How to shuffle rows of 2D array in Java?

PHPz
PHPzforward
2024-02-09 16:15:20866browse

How to scramble the rows of a two-dimensional array in Java? This is a problem that many developers often encounter. Shuffling the rows of a two-dimensional array can be achieved by using the shuffle() method in the Collections class. This method can randomly shuffle the order of elements in the collection. First, we need to convert the two-dimensional array into List form, and then use the shuffle() method to randomly sort the list. Finally, converting the list back into a two-dimensional array completes the row shuffling. This method is simple and easy to implement, can effectively scramble the rows of two-dimensional arrays, and provides developers with a more flexible way to process data.

Question content

I wrote some code to read the csv file and do some data preprocessing, then it should shuffle the rows of the 2d array instead of the columns, although the order is not Change.

The problem I'm having is that it does scramble the rows and columns of the 2d array, but I don't know how to fix it.

import java.io.*;
import java.nio.file.paths;
import java.util.*;

public class randomforest
{
  public static void shuffle2darray()
    {
        int numrows = allfeatures.length;
        list<integer> indices = new arraylist<>();

        // populate the list with indices
        for (int i = 0; i < numrows; i++) {
            indices.add(i);
        }

        // shuffle the list of indices
        collections.shuffle(indices);

        // create a copy of the original array
        int[][] shuffledarray = new int[numrows][allfeatures[0].length];

        // use shuffled indices to rearrange the rows of the original array
        for (int i = 0; i < numrows; i++) {
            shuffledarray[i] = allfeatures[indices.get(i)];
        }

        // update the original array with the shuffled values
        allfeatures = shuffledarray;
    }

    public static void main(string[] args) throws ioexception
    {
        randomforest rf = new randomforest("se-dermatitis dataset.csv");

        rf.getdatarows();
        rf.markfilters();
        rf.removeattributes();

        int count = 1;
        int counter = 1;

        int totalpredacc = 0;

        int allfeaturessize = allfeatures.length;
        int foldsize = allfeaturessize / 10;

        double[] threshold = {0.4, 0.5, 0.6, 0.7};

        shuffle2darray();
    }
}

There are other methods, as you can see, but only this shuffle2darray() method will work fine.

So that I can randomly split the data into 10 parts so that I can do 10 parts cross validation

I did try putting print statements into the code and tried checking the 2d array before and after trying to shuffle, and then checking the csv file to make sure the rows were in the correct order.

This is a small example before filtering:

cpi_9606.ensp00000000233 cpi_9606.ensp00000000412 cpi_9606.ensp00000000442 cpi_9606.ensp00000001008 cpi_9606.ensp00000001146 cpi_9606.ensp00000002165 cpi_9606.ensp00000002829 cpi_9606.ensp00000003084 cpi_9606.ensp00000003100
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 180

After filtering and shuffling the columns:

cpi_9606.ENSP00000000442 cpi_9606.ENSP00000001008 cpi_9606.ENSP00000003084 cpi_9606.ENSP00000003100 cpi_9606.ENSP00000005178 cpi_9606.ENSP00000011292 cpi_9606.ENSP00000011653 cpi_9606.ENSP00000012443 cpi_9606.ENSP00000013034 cpi_9606.ENSP00000014930
0 900 0 0 0 900 0 0 0
900 0 0 0 0 0 928 900 338
322 236 0 0 0 0 0 0 0

Solution

Your code snippet fails to contain a key element: the array you want to sort.

So, a wild shot in the dark, I guess it looks like this:

int[][] allfeatures = ....;

To shuffle, it's a liner. You can discard all code you own:

collections.shuffle(arrays.aslist(allfeatures));

So, why does this work?

Because there is no such thing as a two-dimensional array. java doesn't have them at all.

The int[][] allfeatures you have is not a 2D array. It is a one-dimensional array whose components are of type int array. So arrays.aslist(allfeatures) is great because you have an array of objects (not primitives) - each object is an array of int . The expression is of type listdfcfb097077124ccf9617875ee28bb24.

arrays.aslist Returns a list still backed by the underlying array. Therefore, .set() works, but .add() does not (because an array can change its elements, but cannot grow or shrink). collections.shuffle can shuffle any list, and only use .set and .get to do this: arrays.aslist supported The operation is very good.

The upshot of all this is that this will rescale allfeatures itself.

If you want to keep allfeatures unchanged, please clone first:

int[][] clone = allFeatures.clone();
Collections.shuffle(Arrays.asList(clone));

The above is the detailed content of How to shuffle rows of 2D array in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete