[Tutor] location of points in a covariance matrix
Bob Gailer
bgailer at alum.rpi.edu
Wed Jul 25 16:15:42 CEST 2007
Beanan O Loughlin wrote:
> Hi all, I'm a meteorology postgrad working with python for the first time.
>
> I have found the location minimum in a large covariance matrix. this
> value corresponds to the covariance of two points on a latitude,
> longitude grid.
>
> I need to find a method to locate these two points on the lat,lon grid.
Usually multi-dimensional arrays are stored in "row-major" order. So the
subscripts of an array of shape 2,3,4 (planes, rows. columns) would look
like:
1,1,1 1,1,2 1,1,3 1,1 4 1,2,1 1,2,2 1,2,3 1,2,4 1,3,1 1,3,2
1,3,3 1,3,4 2,1,1 2,1,2 2,1,3 2,1 4 2,2,1 2,2,2 2,2,3 2,2,4
2,3,1 2,3,2 2,3,3 2,3,4
When you reshape it to 2,12 the elements remain "in place", and the
subscripts now are:
1,1 1,2 1,3 1,4 1,5 1,6 1,7 1,8 1,9 1,10 1,11 1,12 2,1 2,2
2,3 2,4 2,5 2,6 2,7 2,8 2,9 2,10 2,11 2,12
Is that enough of a hint?
>
> this is the code i have used, where 'se' is a 3-D array of data
>
>
> >>> nt,nlat,nlon = shape(se)
> >>>nt,nlat,nlon # 3-D array of data taken
> 1464 times, over 41 latitudes and 58 longitudes
> (1464, 41, 58)
> >>>
> >>>
> >>>m=reshape(se,(nt,nlat*nlon)) # reshape to (time,latitude
> longitude data point) where 2378 = 41*58
> >>>
> >>>shape(m)
> (1464,2378)
> >>>
> >>>
> >>>covmat=cov(m) # calculate covariance matrix
> >>>
> >>>shape(covmat)
> (2378,2378)
>
> >>>def min(R):
> U = triu(R) #just use one half of
> the diagonal matrix
> n = U.shape[0]
> U.flat[::n+1] = 1000000000.0 #give the diagonal elements a
> large value so they wont be selected
> k = argmin(U.flat) #find the min value of
> the flattened array
> i, j = divmod(k,n) #calculate the index of
> the minimum data
> return i, j, R[i,j]
>
> >>>
> >>> min(covmat)
> (7, 1914, -2.3016361721151051)
>
> so the minimum is found at (7,1914) in the covariance matrix and has a
> value of - 2.3
>
> This min point corresponds to the covariance between two 'lat,lon'
> data points in my (41,58) sample grid.
>
> Is there a way i can move back from my (2378,2378) covariance matrix
> to see where these two points are located on the (41, 58) grid?
>
> Thank you very much in advance
>
> B.
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
--
Bob Gailer
510-978-4454 Oakland, CA
919-636-4239 Chapel Hill, NC
More information about the Tutor
mailing list