[Numpy-discussion] vectorizing
Brian Blais
bblais@bryant....
Fri Jun 5 13:07:42 CDT 2009
Hello,
I have a vectorizing problem that I don't see an obvious way to
solve. What I have is a vector like:
obs=array([1,2,3,4,3,2,1,2,1,2,1,5,4,3,2])
and a matrix
T=zeros((6,6))
and what I want in T is a count of all of the transitions in obs,
e.g. T[1,2]=3 because the sequence 1-2 happens 3 times, T[3,4]=1
because the sequence 3-4 only happens once, etc... I can do it
unvectorized like:
for o1,o2 in zip(obs[:-1],obs[1:]):
T[o1,o2]+=1
which gives the correct answer from above, which is:
array([[ 0., 0., 0., 0., 0., 0.],
[ 0., 0., 3., 0., 0., 1.],
[ 0., 3., 0., 1., 0., 0.],
[ 0., 0., 2., 0., 1., 0.],
[ 0., 0., 0., 2., 0., 0.],
[ 0., 0., 0., 0., 1., 0.]])
but I thought there would be a better way. I tried:
o1=obs[:-1]
o2=obs[1:]
T[o1,o2]+=1
but this doesn't give a count, it just yields 1's at the transition
points, like:
array([[ 0., 0., 0., 0., 0., 0.],
[ 0., 0., 1., 0., 0., 1.],
[ 0., 1., 0., 1., 0., 0.],
[ 0., 0., 1., 0., 1., 0.],
[ 0., 0., 0., 1., 0., 0.],
[ 0., 0., 0., 0., 1., 0.]])
Is there a clever way to do this? I could write a quick Cython
solution, but I wanted to keep this as an all-numpy implementation if
I can.
thanks,
Brian Blais
--
Brian Blais
bblais@bryant.edu
http://web.bryant.edu/~bblais
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090605/9a4e3e88/attachment.html
More information about the Numpy-discussion
mailing list