HyperCube
HyperCube

Reputation: 3950

Row, column assignment without for-loop

I wrote a small script to assign values to a numpy array by knowing their row and column coordinates:

gridarray = np.zeros([3,3])
gridarray_counts = np.zeros([3,3])

cols = np.random.random_integers(0,2,15)
rows = np.random.random_integers(0,2,15)
data = np.random.random_integers(0,9,15)

for nn in np.arange(len(data)):
    gridarray[rows[nn],cols[nn]] += data[nn]
    gridarray_counts[rows[nn],cols[nn]] += 1

In fact, then I know how many values are stored in the same grid cell and what the sum is of them. However, performing this on arrays of lengths 100000+ it is getting quite slow. Is there another way without using a for-loop?

Is an approach similar to this possible? I know this is not working yet.

gridarray[rows,cols] += data
gridarray_counts[rows,cols] += 1

Upvotes: 3

Views: 265

Answers (2)

Bi Rico
Bi Rico

Reputation: 25823

I would use bincount for this, but for now bincount only takes 1darrays so you'll need to write your own ndbincout, something like:

def ndbincount(x, weights=None, shape=None):
    if shape is None:
        shape = x.max(1) + 1

    x = np.ravel_multi_index(x, shape)
    out = np.bincount(x, weights, minlength=np.prod(shape))
    out.shape = shape
    return out

Then you can do:

gridarray = np.zeros([3,3])

cols = np.random.random_integers(0,2,15)
rows = np.random.random_integers(0,2,15)
data = np.random.random_integers(0,9,15)

x = np.vstack([rows, cols])
temp = ndbincount(x, data, gridarray.shape)
gridarray = gridarray + temp
gridarray_counts = ndbincount(x, shape=gridarray.shape)

Upvotes: 2

Bitwise
Bitwise

Reputation: 7805

You can do this directly:

gridarray[(rows,cols)]+=data
gridarray_counts[(rows,cols)]+=1

Upvotes: 0

Related Questions