Create a numpy array when indexes of (fixed) elements are given - python

I have a numpy array of indexes e.g. [1,3,12]. I want to create another array from this such that at these indexes, I get a non-zero elements e.g. 1. So in this case, with input [1,3,12], I should get [0,1,0,1,0,0,0,0,0,0,0,0,1]. I can do it in a for loop, is there a short numpy function to achieve this?

With numpy you can index with lists directly:
a = [1,3,12]
vector = numpy.zeros(shape=max(a) + 1)
vector[a] = 1

Related

Splitting numpy multidimensional array based on indices stored in another array or list

I have a numpy multidimensional array with shape = (12,2,3,3)
import numpy as np
arr = np.arange(12*2*3*3).reshape((12,2,3,3))
I need to select those elements based on the 2nd dimension where the dindices are stored in another list
indices = [0,1,0,0,1,1,0,1,1,0,1,1]
in one array, and the rest in another array.
the output in either case should be in another array of shape (12,3,3)
arr2 = np.empty((arr.shape[0],*arr.shape[-2:]))
I could do it using a for loop
for i, ii in enumerate(indices):
arr2[i] = arr[i, indices[ii],...]
However, I am searching for a one liner.
When I try indexing using the list as indices
test = arr[:,indices,...]
I get test of shape (12,12,3,3) instead of (12,3,3). Could you help me please?
You can use np.arange for indexing the first dimension:
test = arr[np.arange(arr.shape[0]),indices,...]
or just the python range function:
test = arr[range(arr.shape[0]),indices,...]

How do I remove all elements from a numpy array that are equal to zero in another array?

How do I remove all elements from a numpy array that are equal to zero in another array?
I want to use the indices of one numpy array mask where mask==0 to delete the elements of another same-shaped array array.
I tried something like np.delete(array,mask==0) but that just gave me an error.
use np.where
array = array[np.where(another_array != 0)]

Numpy - Compare elements in two 2D arrays and replace values

I have a specific requirement for this problem. I need it to be simple and fast.
My problem:
I have two 2D arrays and I need to replace values in 1. array by values in 2. array according to condition. That is if element in x,y position in 1. array is smaller than element in x,y position in 2. array, then replace element in 1. array by element in 2. array.
what I tried and is not working:
import numpy as np
arr = np.random.randint(3,size=(2, 2))
arr2 = np.random.randint(3,size=(2, 2))
print(arr)
print(arr2)
arr[arr<arr2]=arr2 # Doesnt work.
This raises TypeError:
TypeError: NumPy boolean array indexing assignment requires a 0 or 1-dimensional input, input has 2 dimensions.
I can see, that it would be possible to iterate through columns or rows, but I believe it can be done without iteration.
Thanks in advance

Sorting arrays by their last argument

I want to sort a numpy array of arrays based on their last entry.
For example, say we have this array of arrays:
a=np.array([np.array([1,2,5]),np.array([1,3,0]),np.array([1,4,-17])])
I want to return the array sorted this way:
np.array([np.array([1,4,-17]),np.array([1,3,0]) ,np.array([1,2,5]) ])
I.e. as -17 <= 0 <= 5, last array becomes the first one, and so on. Elements within each array must not be altered.
I suppose numpy has a builtin but I haven't been able to find it.

Fastest way to get elements from a numpy array and create a new numpy array

I have numpy array called data of dimensions 150x4
I want to create a new numpy array called mean of dimensions 3x4 by choosing random elements from data.
My current implementation is:
cols = (data.shape[1])
K=3
mean = np.zeros((K,cols))
for row in range(K):
index = np.random.randint(data.shape[0])
for col in range(cols):
mean[row][col] = data[index][col]
Is there a faster way to do the same?
You can specify the number of random integers in numpy.randint (third argument). Also, you should be familiar with numpy.array's index notations. Here, you can access all the elements in one row by : specifier.
mean = data[np.random.randint(0,len(data),3),:]

Categories