Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to flatten a numpy ndarray along axis?

I have three arrays: longitude(400,600),latitude(400,600),data(30,400,60); what I am trying to do is to extract value in the data array according to it's location(latitude and longitude).

Here is my code:

import numpy
import tables

hdf = "data.hdf5"
h5file = tables.openFile(hdf, mode = "r")

lon = numpy.array(h5file.root.Lonitude)
lat = numpy.array(h5file.root.Latitude)
arr = numpy.array(h5file.root.data)

lon = numpy.array(lon.flat)
lat = numpy.array(lat.flat)
arr = numpy.array(arr.flat)

lonlist=[]
latlist=[]
layer=[]
fre=[]

for i in range(0,len(lon)):
    for j in range(0,30):
        longi = lon[j]
        lati = lat[j]
        layers=[j]
        frequency= arr[i]

        lonlist.append(longi)
        latlist.append(lati)
        layer.append(layers)
        fre.append(frequency)

output = numpy.column_stack((lonlist,latlist,layer,fre))

The problem is that the "frequency" is not what I want.I want the data array to be flattened along axis-zero,so that the "frequency" would be the 30 values at one location.Is there such a function in numpy to flatten ndarray along a particular axis?

like image 737
oops Avatar asked Oct 28 '25 05:10

oops


1 Answers

You can try np.ravel(your_array), or your_array.shape=-1. The np.ravel function lets you use an optional argument order: choose C for a row-major order or F for a column-major order.

like image 128
Pierre GM Avatar answered Oct 31 '25 08:10

Pierre GM