[Numpy-discussion] find location of maximum values

Olivier Delalleau shish at keba.be
Mon Jan 9 22:39:52 EST 2012


Do you mean that listval[0] is systematically equal to 0, or is it
something else?

-=- Olivier

2012/1/9 questions anon <questions.anon at gmail.com>

> thank you, I seem to have made some progress (with lots of help)!!
> I still seem to be having trouble with the time. Because it is hourly data
> for a whole month I assume that is where my problem lies.
> When I run the following code I alwayes receive the first timestamp of the
> file. Not sure how to get around this:
>
>             tmax=TSFC.max(axis=0)
>             maxindex=tmax.argmax()
>             maxtemp=tmax.ravel()[maxindex] #or maxtemp=TSFC.max()
>             print maxindex, maxtemp
>             val=N.unravel_index(maxindex, TSFC.shape)
>             listval=list(val)
>             print listval
>             timelocation=TIME[listval[0]]
>             latlocation=LAT[listval[1]]
>             lonlocation=LON[listval[2]]
>             print latlocation, lonlocation
>
>             cdftime=utime('seconds since 1970-01-01 00:00:00')
>             ncfiletime=cdftime.num2date(timelocation)
>             print ncfiletime
>
>
>
> On Tue, Jan 10, 2012 at 10:22 AM, Benjamin Root <ben.root at ou.edu> wrote:
>
>>
>>
>> On Monday, January 9, 2012, questions anon <questions.anon at gmail.com>
>> wrote:
>> > thanks for the responses.
>> > Unfortunately they are not matching shapes
>> >>>> print TSFC.shape, TIME.shape, LAT.shape, LON.shape
>> > (721, 106, 193) (721,) (106,) (193,)
>> >
>> > So I still receive index out of bounds error:
>> >>>>tmax=TSFC.max(axis=0)
>> > numpy array of max values for the month
>> >>>>maxindex=tmax.argmax()
>> > 2928
>> >>>>maxtemp=tmax.ravel()[maxindex] #or maxtemp=TSFC.max()
>> > 35.5 (degrees celcius)
>> >
>> >>>>latloc=LAT[tmax.argmax()]
>> > IndexError: index out of bounds
>> >
>> > lonloc=LON[tmax.argmax()]
>> > timeloc=TIME[tmax.argmax()]
>> >
>> >
>> > Any other ideas for this type of situation?
>> > thanks
>>
>> Right, we realize they are not the same shape.  When you use argmax on
>> the temperature data, take that index number and use unravel_index(index,
>> TSFC.shape) to get a three-element tuple, each being the index in the TIME,
>> LAT, LON arrays, respectively.
>>
>> Cheers,
>> Ben Root
>>
>>
>> >
>> > On Wed, Jan 4, 2012 at 10:29 PM, Derek Homeier <
>> derek at astro.physik.uni-goettingen.de> wrote:
>> >>
>> >> On 04.01.2012, at 5:10AM, questions anon wrote:
>> >>
>> >> > Thanks for your responses but I am still having difficuties with
>> this problem. Using argmax gives me one very large value and I am not sure
>> what it is.
>> >> > There shouldn't be any issues with the shape. The latitude and
>> longitude are the same shape always (covering a state) and the temperature
>> (TSFC) data are hourly for a whole month.
>> >>
>> >> There will be an issue if not TSFC.shape == TIME.shape == LAT.shape ==
>> LON.shape
>> >>
>> >> One needs more information on the structure of these data to say
>> anything definite,
>> >> but if e.g. your TSFC data have a time and a location dimension,
>> argmax will
>> >> per default return the index for the flattened array (see the argmax
>> documentation
>> >> for details, and how to use the axis keyword to get a different
>> output).
>> >> This might be the very large value you mention, and if your location
>> data have fewer
>> >> dimensions, the index will easily be out of range. As Ben wrote, you'd
>> need extra work to
>> >> find the maximum location, depending on what maximum you are actually
>> looking for.
>> >>
>> >> As a speculative example, let's assume you have the temperature data
>> in an
>> >> array(ntime, nloc) and the position data in array(nloc). Then
>> >>
>> >> TSFC.argmax(axis=1)
>> >>
>> >> would give you the index for the hottest place for each hour of the
>> month
>> >> (i.e. actually an array of ntime indices, and pointer to so many
>> different locations).
>> >>
>> >> To locate the maximum temperature for the entire month, your best way
>> would probably
>> >> be to first extract the array of (monthly) maximum temperatures in
>> each location as
>> >>
>> >> tmax = TSFC.max(axis=0)
>> >>
>> >> which would have (in this example) the shape (nloc,), so you could
>> directly use it to index
>> >>
>> >> LAT[tmax.argmax()]   etc.
>> >>
>> >> Cheers,
>> >>                                                Derek
>> >>
>> >> _______________________________________________
>> >> NumPy-Discussion mailing list
>> >> NumPy-Discussion at scipy.org
>> >> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>> >
>> >
>>
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion at scipy.org
>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>>
>>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20120109/4bed4563/attachment.html>


More information about the NumPy-Discussion mailing list