Mathew Yeates schrieb:
> My head is about to explode.
> 
> I have an M by N array of floats. Associated with the columns are 
> character labels
> ['a','b','b','c','d','e','e','e']  note: already sorted so duplicates 
> are contiguous
> 
> I want to replace the 2 'b' columns with the sum of the 2 columns. 
> Similarly, replace the 3 'e' columns with the sum of the 3 'e' columns.
> 
> The resulting array still has M rows but less than N columns. Anyone? 
> Could be any harder than Sudoku.
> 


Hi,
I don't have time for this ;-) , but I learnt something useful along the
way...

import numpy as n
m = n.ones([2,6])
a = ['b', 'c', 'c', 'd', 'd', 'd']

startindices = set([a.index(x) for x in a])
out = n.empty([m.shape[0], 0])
for i in startindices:
    temp = n.mat(m[:, i : i + a.count(a[i])]).sum(axis = 1)
    out = n.hstack([out, temp])

print out

Not sure if axis = 1 is needed, but until the defaults have settled a
bit it can't hurt. You need python 2.4 for the built-in <set>, and <out>
will be a numpy matrix, use <asarray> if you don't like that. But here
it's really nice to work with matrices, because otherwise .sum() will
give you a 1-d array sometimes, and that will suddenly look like a row
to <hstack> (instead of a nice column vector) and wouldn't work --
that's why matrices are so great and everybody should be using them ;-)

hth,
sven


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Numpy-discussion mailing list
Numpy-discussion@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/numpy-discussion

Reply via email to