![]() ![]() This works.but it's a little scary, as I see little guarantee it'll continue to work - it doesn't look like the sort of thing that's guaranteed to survive across numpy version, for example. One other thought I had was this: def shuffle_in_unison_scary(a, b): Is there a better way to go about this? Faster execution and lower memory usage are my primary goals, but elegant code would be nice, too. However, this feels clunky, inefficient, and slow, and it requires making a copy of the arrays - I'd rather shuffle them in-place, since they'll be quite large. Permutation = (len(a))įor old_index, new_index in enumerate(permutation):įor example: > a = numpy.asarray(,, ]) Shuffled_b = numpy.empty(b.shape, dtype=b.dtype) Shuffled_a = numpy.empty(a.shape, dtype=a.dtype) This code works, and illustrates my goals: def shuffle_in_unison(a, b): shuffle them in unison with respect to their leading indices. I want to shuffle each of them, such that corresponding elements continue to correspond - i.e. Train = df.sample(frac=0.I have two numpy arrays of different shapes, but with the same length (leading dimension). #%% Method 4: using pandas dataframe to splitĭf = pd.read_csv(file_path, header=None) # Some csv file (I used some file with 3 columns) Idx = np.random.permutation(arr.shape) # can also use random.shuffle #%% Method 3: shuffle indicies without a for loop #%% Method 2: shuffle the indecies and then shuffle and apply to X and Y ![]() X_train, x_test, y_train, y_test = X, X, Y, Y #%% Method 1: shuffle the whole matrix arr and then split The code for the 4 different methods I timed: import numpy as np Method 3 won by far with the shortest time, after that method 1, and method 2 and 4 discovered to be really inefficient.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |