Numeric arrays with named axes?
Duncan Smith
buzzard at urubu.freeserve.co.uk
Thu Oct 17 19:22:55 EDT 2002
More information about the Python-list mailing list
Thu Oct 17 19:22:55 EDT 2002
- Previous message (by thread): Numeric arrays with named axes?
- Next message (by thread): Numeric arrays with named axes?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
"Robbie Sedgewick" <bobsedge at yahoo.com> wrote in message news:d375fcee.0210162255.4d515db1 at posting.google.com... > I've been writing a lot of Numeric code lately that require arrays > with many dimensions. I think I currently have 5 dimensional array. > It is starting to get somewhat confusing which axis is which > especially with the different slices of the arrays floating arround. > > I'm thinking what is needed is some wrapper around Numeric arrays that > allow me to refer to the axes of the array by name. For example: > > >>> x = namedarray([[1, 2], [3, 4]], axis_names=["cow", "dog"]) > >>> x[{"cow":1}] > namedarray([3, 4] axis_names=["dog"]) > >>> sum( x[{"cow"}:1] ) > 7 > > Has anyone done anything like this? I started to write a wrapper > class, but it is somewhat complicated to figure out all the different > ways that numeric functions change the shape of the array. > > Anyone got any better idea how to do this? > > Thanks, > Robbie Presumably there are a limited number of things you'd want to do with the resulting object, so a class could be the answer. I started to learn Python by writing such a class (for various probability / contingency table related stuff). eg. >>> from disc import * >>> import Numeric >>> cow = Variable(['friesan', 'charolet'], 'cow') >>> dog = Variable(['whippet', 'terrier'], 'dog') >>> values = Numeric.array([[0, 2], [3, 5]]) >>> t = NumTable(values, [cow, dog]) >>> t array([[0, 2], [3, 5]]) ['cow', 'dog'] >>> t / cow #marginalisation array([3, 7]) ['dog'] >>> t.condition(dog, 0) #conditioning array([0, 3]) ['cow'] >>> t.normalise() #normalisation array([[ 0. , 0.2], [ 0.3, 0.5]]) ['cow', 'dog'] >>> sheep = Variable(['tup', 'ewe'], 'sheep') >>> t2 = NumTable(Numeric.array([6, 7]), [sheep]) >>> t2 array([6, 7]) ['sheep'] >>> t * t2 #pointwise multiplication array([[[ 0, 0], [12, 14]], [[18, 21], [30, 35]]]) ['cow', 'dog', 'sheep'] >>> t2 * t array([[[ 0, 12], [18, 30]], [[ 0, 14], [21, 35]]]) ['sheep', 'cow', 'dog'] #i.e. the same table with a different >>> t3 = t2 * t #ordering of the axes >>> t3.variables[0].levels ['tup', 'ewe'] >>> t3.variables[0].name 'sheep' >>> #etc. I assume you'll be doing something different, so your code will be different. But it's certainly doable. Duncan
- Previous message (by thread): Numeric arrays with named axes?
- Next message (by thread): Numeric arrays with named axes?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list