0

I am trying to save an array in one program and open it in another, my array is

[[   0. 2815. 3286. 4060. 2877. 2236.]
 [2798.    0.  471. 1245. 1586. 1931.]
 [3165.  367.    0. 1006. 1556. 1902.]
 [3724. 1268. 1739.    0.  551.  896.]
 [3344. 1573. 1575. 2030.    0.  515.]
 [2925. 1923. 1925. 2380.  641.    0.]]

to save it i am using:

def saveArray(array):

    import numpy as np
    np.save('postCodeArray', array)

Then to open it I am using

def callFunction():

    import numpy as np

    array = np.load('postCodeArray.npy')  

    print(array)

I get this error

" File "C:\Users\wf5931\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\lib\format.py", line 696, in read_array raise ValueError("Object arrays cannot be loaded when "

ValueError: Object arrays cannot be loaded when allow_pickle=False"

Please help!

  • when i tried it with a similar np.zeros((4,4)) matrix it seemed to work. V confused
ASG
  • 51
  • 3
  • 6
  • 1
    Are you sure the variable `array` in your code is an array and not a list? If it's an array, check it's dtype (`array.dtype`), if it's an `object` array convert it to `float` or `int` (`array.astype('float32')`) – Sayandip Dutta Oct 01 '19 at 10:06
  • Possible duplicate of [How to fix 'Object arrays cannot be loaded when allow\_pickle=False' for imdb.load\_data() function?](https://stackoverflow.com/questions/55890813/how-to-fix-object-arrays-cannot-be-loaded-when-allow-pickle-false-for-imdb-loa) – zvone Oct 01 '19 at 10:09
  • It's likely that your array is object dtype. `np.zeros` is a float dtype. Object dtype arrays have to use `pickle` to save the object elements. In newer `numpy` versions, you have to explicitly `allow_pickle` when loading such an array. This is meant to improve security. – hpaulj Oct 01 '19 at 16:46
  • I had the same problem with numpy 1.17. This is not an explanation of the error's root cause nor a solution, but rather a workaround - downgrade numpy to 1.16.2. e.g., 'pip install -U numpy=1.16.2' or 'conda install numpy=1.16.2' fixed the problem for me, – Serendipity Dec 11 '19 at 16:27

1 Answers1

3

From the documentation of numpy.save(file, arr, allow_pickle=True, fix_imports=True), and from the error message you got, try this:

def saveArray(array):

    import numpy as np
    np.save('postCodeArray', array, allow_pickle=True)

Same with the load, documentation, numpy.load(file, mmap_mode=None, allow_pickle=False, fix_imports=True, encoding='ASCII')

def callFunction():

    import numpy as np
    array = np.load('postCodeArray.npy', allow_pickle=True)  
    print(array)

Works with Python 3.7 and Numpy 1.16.1.

Edit: Array used.

A = np.asarray([[   0, 2815, 3286, 4060, 2877, 2236],
                [2798,    0,  471, 1245, 1586, 1931],
                [3165,  367,    0, 1006, 1556, 1902],
                [3724, 1268, 1739,    0,  551,  896],
                [3344, 1573, 1575, 2030,    0,  515],
                [2925, 1923, 1925, 2380,  641,    0]])
Mathieu
  • 5,410
  • 6
  • 28
  • 55
  • Thanks for trying to help! When i do this i get returned None in the new program – ASG Oct 01 '19 at 13:21
  • @AlexanderSchamroth-Green Share what you did completely... when I ran it, it does save and load the array. I added the array I used. + there are no returns in what you shared, only a print. – Mathieu Oct 01 '19 at 13:26
  • @AlexanderSchamroth-Green i.e. Please share a piece of code we can Copy/Paste and which raises the error/shows the problem. – Mathieu Oct 01 '19 at 13:28