[Numpy-discussion] Crosstabulation

eat e.antero.tammi@gmail....
Mon Jul 19 14:30:27 CDT 2010


Ionut Sandric <sandricionut <at> yahoo.com> writes:

> Thank you Zack:
> 
> By raster data I mean classified slope gradient (derived from a dem), 
landuse-landcover, lithology etc. A
> crosstabulation analysis will give me a table with the common areas for each 
class from each raster and
> this will go into other analysis. I can do it with other softwares (like 
ArcGIS DEsktop etc), but I would
> like to have all with numpy or to build something on top of numpy
> 
> Thanks's again
> 
> Ionut
> 
> ----- Original Message -----
> From: "Zachary Pincus" <zachary.pincus <at> yale.edu>
> To: "Discussion of Numerical Python" <numpy-discussion <at> scipy.org>
> Sent: Wednesday, July 14, 2010 9:42:49 PM GMT +02:00 Athens, Beirut, 
Bucharest, Istanbul
> Subject: Re: [Numpy-discussion] Crosstabulation
> 
> Hi Ionut,
> 
> Check out the "tabular" package:
> http://parsemydata.com/tabular/index.html
> 
> It seems to be basically what you want... it does "pivot tables" (aka  
> crosstabulation), it's built on top of numpy, and has simple data IO  
> tools.
> 
> Also check out this discussion on "pivot tables" from the numpy list a  
> while ago:
> http://mail.scipy.org/pipermail/numpy-discussion/2007-August/028739.html
> 
> One question -- what do you mean by "raster data"? In my arena, that  
> usually means "images"... and I'm not sure what a crosstabulation on  
> image data would mean!
> 
> Zach
> 
> On Jul 14, 2010, at 2:28 PM, Ionut Sandric wrote:
> 
> >
> > Sorry, the first email was sent before to finish it...
> >
> >
> > Hi:
> >
> > I have two raster data and I would like to do a crosstabulation  
> > between them and export the results to a table in a text file. Is it  
> > possible to do it with NumPy? Does someone have an example?
> >
> > Thank you,
> >
> > Ionut
> >
> >
> >
> > _______________________________________________
> > 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
> 
Hi,

You may be able to adapt this simple script to your case.

import numpy as np

# generate some test data
def gen(b, e, m, n):
    return np.arange(b, e+ 1), np.random.randint(b, e+ 1, (m, n))
m, n= 15, 15
c1, d1= gen(0, 3, m, n); print d1
c2, d2= gen(3, 5, m, n); print d2

# perform actual x-tabulation
xtab= np.zeros((len(c1), len(c2)), np.int)
for i in xrange(len(c1)):
    tmp= d2[c1[i]== d1]
    for j in xrange(len(c2)):
        xtab[i, j]= np.sum(c2[j]== tmp)
print xtab, np.sum(xtab)== np.prod(d1.shape)


Anyway it's straightforward to extend it to nd x-tabulations ;-).


My 2 cents,
eat





More information about the NumPy-Discussion mailing list