Up vote 1 down vote favorite share g+ share fb share tw.
Assume three arrays in numpy: a = np. Zeros(5) be = np. Array(3,3,3,0,0) c = np.
Array(1,5,10,50,100) be can now be used as an index for a and c. For example: In 142: cb Out142: array(50, 50, 50, 1, 1) Is there any way to add up the values connected to the duplicate indexes with this kind of slicing? With ab = c Only the last values are stored: array( 100.
, 0. , 0. , 10.
, 0. ) I would like something like this: ab += c which would give array( 150. , 0.
, 0. , 16. , 0.
) I'm mapping very large vectors onto 2D matrices and would really like to avoid loops... python matrix numpy scipy slicing link|improve this question asked Feb 14 at 18:22brorfred655.
You could do something like: def sum_unique(label, weight): order = np. Lexsort(label. T) label = labelorder weight = weightorder unique = np.
Ones(len(label), 'bool') unique:-1 = (label1:! = label:-1). Any(-1) totals = weight.cumsum() totals = totalsunique totals1: = totals1: - totals:-1 return labelunique, totals And use it like this: In 110: coord = np.random.
Randint(0, 3, (10, 2)) In 111: coord Out111: array(0, 2, 0, 2, 2, 1, 1, 2, 1, 0, 0, 2, 0, 0, 2, 1, 1, 2, 1, 2) In 112: weights = np. Ones(10) In 113: uniq_coord, sums = sum_unique(coord, weights) In 114: uniq_coord Out114: array(0, 0, 1, 0, 2, 1, 0, 2, 1, 2) In 115: sums Out115: array( 1. , 1.
, 2. , 3. , 3.
) In 116: a = np. Zeros((3,3)) In 117: x, y = uniq_coord. T In 118: ax, y = sums In 119: a Out119: array( 1.
, 0. , 3. , 1.
, 0. , 3. , 0.
, 2. , 0. ) I just thought of this, it might be easier: In 120: flat_coord = np.
Ravel_multi_index(coord. T, (3,3)) In 121: sums = np. Bincount(flat_coord, weights) In 122: a = np.
Zeros((3,3)) In 123: a. Flat:len(sums) = sums In 124: a Out124: array( 1. , 0.
, 3. , 1. , 0.
, 3. , 0. , 2.
, 0. ).
Thanks, this works great! – brorfred Feb 14 at 23:07.
The += operator for NumPy arrays simply doesn't work the way you are hoping, and I'm not aware of a away of making it work that way. As a work-around I suggest using numpy.bincount(): >>> numpy. Bincount(b, c) array( 150.
, 0. , 0. , 16.
) Just append zeros as needed.
Thanks for the answer! I had now idea that bincount existed- it will be been very useful for other implementations. Would it be possible to use this approach for 2D arrays as well?
My real world problem consists of three 10^7 element vectors (x-pos, y-pos, value) that I'm mapping to a 2D array. – brorfred Feb 14 at 20:01 @brorfred: You can reinterpret your array as a 1D array without copying using its reshape() method and then apply bincount(). – Sven Marnach Feb 14 at 20:30.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.