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
4
  • 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')) Commented Oct 1, 2019 at 10:06
  • Possible duplicate of How to fix 'Object arrays cannot be loaded when allow_pickle=False' for imdb.load_data() function? Commented Oct 1, 2019 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. Commented Oct 1, 2019 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, Commented Dec 11, 2019 at 16:27

1 Answer 1

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]])
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for trying to help! When i do this i get returned None in the new program
@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.
@AlexanderSchamroth-Green i.e. Please share a piece of code we can Copy/Paste and which raises the error/shows the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.