Hope this helps Use a one dimensional array for r and use it as the column index. For row indexes you can simply use a range: code :
In [25]: r=np.random.randint(0, 7, 10)
In [26]: x=np.zeros( (10,7) )
In [27]: x[np.arange(10), r] = 1
In [28]: x
Out[28]:
array([[0., 1., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 1.],
[0., 0., 0., 0., 0., 0., 1.],
[0., 1., 0., 0., 0., 0., 0.],
[0., 0., 1., 0., 0., 0., 0.],
[0., 0., 0., 1., 0., 0., 0.],
[0., 0., 0., 0., 1., 0., 0.],
[0., 0., 1., 0., 0., 0., 0.],
[0., 0., 1., 0., 0., 0., 0.],
[0., 1., 0., 0., 0., 0., 0.]])
Share :

How do `ndarray.flags['OWNDATA']`, `ndarray.base`, `id(ndarray)` and `ndarray.__array_interface__['data']` differ?
By : MrR23
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Most of your confusion comes from the fact that test2 is test and test3 is a view of test. ndarray.base code :
>>> import numpy
>>> x = numpy.arange(5)
>>> y = x[1:]
>>> z = x[:1]
>>> x.__array_interface__['data']
(39692208L, False)
>>> y.__array_interface__['data']
(39692212L, False)
>>> z.__array_interface__['data']
(39692208L, False)

Why using an array as an index changes the shape of a multidimensional ndarray?
By : Lift
Date : March 29 2020, 07:55 AM
it helps some times As @hpaulj mentioned in the comments, this behaviour is because of mixing basic slicing and advanced indexing: code :
a = np.arange(120).reshape(4,5,3,2)
b = a[:,[1,2,3,4,0],:,0]

ndarray rowwise index of values greater than array
By : Kristaps Leitėns
Date : March 29 2020, 07:55 AM
wish help you to fix your issue You could start by checking which row indices are lower or equal than n and use argmax to get the first True for each row. For the rows where all columns are False, we can use np.where to set them to np.nan for instance: code :
M = np.array([[1,2,3,4,5],
[6,7,8,9,1],
[2,5,7,8,3]])
n = np.array([[3],[7],[100]])
le = n<=M
# array([[False, False, True, True, True],
# [False, True, True, True, False],
# [False, False, False, False, False]])
lea = le.argmax(1)
has_any = le[np.arange(len(le)), lea]
np.where(has_any, lea, np.nan)
# array([ 2., 1., nan])

Any numpy/torch style to set value given an index ndarray and a flag ndarray?
By : user3277447
Date : March 29 2020, 07:55 AM
Does that help You may sort flags according to the indices to create a mask, then use the mask as a mux. Here is an example code: code :
indices = np.array([[2, 1, 3, 0], [1, 0, 3, 2]])
flags = np.array([[False, False, False, True], [False, False, True, True]])
tensor = np.array([[2.8, 0.5, 1.2, 0.9], [3.1, 2.8, 1.3, 2.5]])
indices_sorted = indices.argsort(axis=1)
mask = np.take_along_axis(flags, indices_sorted, axis=1)
result = tensor * (1  mask) + 1e30 * mask

Filling the diagonals of square matrices inside a 3D ndarray with values given by a 2D ndarray
By : user3285844
Date : March 29 2020, 07:55 AM
To fix this issue Given a 3D ndarray z with shape (k,n,n), is it possbile without using iteration to fill the diagonals of the k nxn matrices with values given by a 2D ndarray v with shape (k,n)? , Here's for generic ndim arrays  code :
diag_view = np.einsum('...ii>...i',z)
diag_view[:] = v
n = v.shape[1]
z.reshape(1,n**2)[:,::n+1] = v.reshape(1,n)
# or z.reshape(z.shape[:2]+(1,))[...,::n+1] = v
m = np.eye(n, dtype=bool) # n = v.shape[1] from earlier
z[...,m] = v

