Home > OS >  How to mix / intersperse two .npy files?
How to mix / intersperse two .npy files?

Time:12-13

I have two .npy files that both contain a ndarray with shape (1_000_000, 833) (1M inputs for a neural network with 833 input neurons), however the exact shape should not matter except that it is the same among the two files.

I want to create two new .npy files that are taken from both files, one after the other. Let's say the first file contains [1, 2, 3, 4, 5, 6] and the second one [a, b, c, d, e, f], then the new first file should contain [1, a, 2, b, 3, c] and the new second one [4, d, 5, e, 6, f]- the size of the files and the contents should remain the same, only its arrangement (and arrangement among the files) should change.

How could I achieve this behavior?

CodePudding user response:

You can use vstack to concatenate the arrays in 2D, then ravel in Fortran order:

A = np.array([1, 2, 3, 4, 5, 6])
B = np.array(['a', 'b', 'c', 'd', 'e', 'f'])

C = np.vstack([A, B]).ravel(order='F')

Or:

C = np.empty(A.shape[0] B.shape[0], dtype=B.dtype)
C[::2] = A
C[1::2] = B

Output:

array(['1', 'a', '2', 'b', '3', 'c', '4', 'd', '5', 'e', '6', 'f'],
      dtype='<U21')
multiple arrays:
l = [np.array([1, 2, 3, 4, 5, 6]) 10*i for i in range(50)]

# option 1
out = np.vstack(l).ravel(order='F')

# option 2
out = np.empty(sum(map(len, l)))
for i, a in enumerate(l):
    out[i::len(l)] = a

CodePudding user response:

You can achieve this behavior, by using the numpy.concatenate() function. This function takes two numpy arrays as input and returns a new array that is the result of the concatenation of the two. For example, given two numpy arrays A and B:

A = np.array([1, 2, 3, 4, 5, 6])
B = np.array([a, b, c, d, e, f])

C = np.concatenate([A, B])

This will give you the resulting array C that contains the values [1, a, 2, b, 3, c, 4, d, 5, e, 6, f].

You can then save this array to a new numpy file using the numpy.save() function. For example,

np.save('new_file.npy', C)

This will save the array C to a new file called 'new_file.npy'. You can then repeat this process for the second file, using the same concatenation and save functions.

  • Related