0

I have this 4D array (numpy.ndarray) that I need to save in a way that its format does not change as I save it (since it should remain unchanged), and then reuse it in my Google Colab file. I have tried saving it in different formats and when I upload it and preview it within my code, the previous format is no longer preserved even when I save it in the .npy format. I have also tried importing the date using the raw link from my GitHub repository or uploading it from my local device, but still no chance. I would appreciate your comments regarding the issue!

Further elaboration:

Here is the code that I use to generate my 4D array:

num1=100000
size = (num1, 8, 4, 4)
prob_0 = 0.3 # 30% of zeros
prob_1 = 1 - prob_0 # 70% of ones
P = np.random.choice([0, 1], size=size, p=[prob_0, prob_1])

I need to save this P as a file and use it in Google Colab. I have to save it in order to maintain the initial data and not come up with a different 4D array every time I run my code. This data (i.e. P) must remain unchanged.

  • You can try `numpy` file format – ThePyGuy Jun 10 '21 at 16:31
  • Does this answer your question? [How to save and load numpy.array() data properly?](https://stackoverflow.com/questions/28439701/how-to-save-and-load-numpy-array-data-properly) – ThePyGuy Jun 10 '21 at 16:33
  • Saving/loading 4D numpy array works for me. Can you provide more details, is this simple 4D array and what do you mean by format is not preserved? – crayxt Jun 10 '21 at 16:33
  • You need to be more specific about how you save the array. Test the array - shape and dtype before saving. Then test a load on the same machine. Then move on to test on the uploaded. If there is a difference, describe it exactly. A general "if doesn't work" question won't elicit good answers. Normally the default `np.save/load` pair works. – hpaulj Jun 10 '21 at 16:41
  • @crayxt Thank you for your comment. I have updated my question with further info. – Liam Spring Jun 10 '21 at 17:35
  • @hpaulj Thank you for your comment. I just added some further info to my question. – Liam Spring Jun 10 '21 at 17:36
  • @Don'tAccept It works with 1D or 2D arrays, but not in my case. – Liam Spring Jun 10 '21 at 17:37

1 Answers1

1

Usual np.save, np.load works

>>> P.shape
(100000, 8, 4, 4)
>>> np.save("P.npy", P)
>>> P2 = np.load("P.npy")
>>> P2.shape
(100000, 8, 4, 4)
>>> np.allclose(P, P2)
True
crayxt
  • 2,367
  • 2
  • 12
  • 17