How to Assign Values to a Numpy Array as a Function of Index

How to assign values to a numpy array as a function of index?

You can assign a range and take advantage of NumPy broadcasting:

A = np.zeros((256, 256))
A[:] = range(A.shape[1])
# or A[:] = np.arange(A.shape[1])

The method you choose will be dependent on the function you wish to apply.

Assign values to different index positions in Numpy array

You can use tuple indexing:

>>> import numpy as np
>>> a = np.zeros((4,2))
>>> vals = [4,3,2,1]
>>> pos = [(0,0),(1,1),(2,0),(3,1)]
>>> rows, cols = zip(*pos)
>>> a[rows, cols] = vals
>>> a
array([[ 4., 0.],
[ 0., 3.],
[ 2., 0.],
[ 0., 1.]])

Changing values in Numpy array according to index

This is a confusing question about views and copies in NumPy.
I found this question [Numpy: views vs copy by slicing] is similar to this one and this doc [Views versus copies in NumPy] mentioned by @Maltimore may explain.

NumPy Fancy Indexing returns a copy of numpy array instead of a view.

However, when set values to numpy array using fancy indexing, what python interpreter does is calling __setitem__ function. Take the code as an example.

In this line:

a[np.array([10,20,30,40,50])] = 1

What python actually does is

a.__setitem__(np.array([10,20,30,40,50]), 1)

i.e. there is not need to create neither a view or a copy because the method can be evaluated inplace (i.e. no new object creation is involved).

Break this line into the following code:

# (a[np.array([10,20,30,40,50])])[np.array([1,2,3])] = 1
a_copy = a[np.array([10,20,30,40,50])]
a_copy[np.array([1,2,3])] = 1

print(a[np.array([10,20,30,40,50])])
# [10, 20, 30, 40, 50]

print(a_copy)
# [10, 1, 1, 1, 50]

As a result, this line modifies the value of the copy, so the original numpy array is unchanged.

numpy function to set elements of array to a value given a list of indices

You can just give it a list of indices:

indices = [1, 4, 5, 6, 7]
zero = numpy.zeros(10)
zero[indices] = 42

What are the efficient ways to assign values to 2D numpy arrays as functions of indicies

You can use numpy.indices, which returns an array representing the indices of a grid; you'll just need to sum along the 0 axis:

>>> a = np.random.random((2,2))
>>> np.indices(a.shape).sum(axis=0) # array([[0, 1], [1, 2]])
>>> a = np.random.random((3,3))
>>> np.indices((3,3)).sum(axis=0) #array([[0, 1, 2], [1, 2, 3], [2, 3, 4]])

numpy array not changing value on assignment

You need to combine the different pairs of []:

x[some_list, 1] = 100

Output:

>>> x
array([[ 1, 2],
[ 3, 100],
[ 4, 100]])

Get indices of element of one array using indices in another array

I have a solution similar to that of Andras based on np.argmax and np.arange. Instead of "indexing the index" I propose to add a piecewise offset to the result of np.argmax:

import numpy as np
a = np.array([[[7, 9],
[19, 18]],
[[24, 5],
[18, 11]]])
off = np.arange(0, a.size, a.shape[2]).reshape(a.shape[0], a.shape[1])
>>> off
array([[0, 2],
[4, 6]])

This results in:

>>> a.argmax(-1) + off
array([[1, 2],
[4, 6]])

Or as a one-liner:

>>> a.argmax(-1) + np.arange(0, a.size, a.shape[2]).reshape(a.shape[0], a.shape[1])
array([[1, 2],
[4, 6]])


Related Topics



Leave a reply



Submit