3

I have a 3D array and I would like to obtain a 2D image along X-Y with the maximum value of z at each point and save it as a numpy array.

import numpy as num
matrix=num.load('3d')
nx,ny,nz=num.shape(matrix)
CXY=num.zeros([ny, nx])
    for i in range(ny):
        for j in range(nx):
            CXY[i,j]=num.max(matrix[j,i,:])

The problem is to save the obtained matrix. I would like to save it with numpy.save but I always get an empty array. Does anyone have suggestions to properly save the obtained array?

I just used num.save:

num.save('max', CXY[i,j])

2
  • 1
    Firstly, matrix.max(axis=2) does what you are doing in the for loops. Second, you should show us your attempt to save the data, so we can tell you why it didn't work. Commented Sep 9, 2014 at 16:04
  • There you see: you should be saving CXJ, not CXJ[i,j]. Commented Sep 9, 2014 at 16:33

1 Answer 1

17

I guess that you're looking for the numpy.savetxt which saves in a human readable format instead of the numpy.save which saves as a binary format.

import numpy as np
matrix=np.random.random((10,10,42))
nx,ny,nz=np.shape(matrix)
CXY=np.zeros([ny, nx])
for i in range(ny):
    for j in range(nx):
        CXY[i,j]=np.max(matrix[j,i,:])

#Binary data
np.save('maximums.npy', CXY)

#Human readable data
np.savetxt('maximums.txt', CXY)

This code saves the array first as a binary file and then as a file you can open in a regular text editor.

Sign up to request clarification or add additional context in comments.

1 Comment

It's mention worthy that np.save stores things as pickle files if not other specified which might make you very unhappy if you go from Python 2 up to Python 3 (see docs).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.