Given 2 lists of arrays (or 2 3D arrays) is there a smarter way in numpy, besides a loop, to get the multiplication of the first array of the first list times the first array of the second list and so on? I have a feeling I am overlooking the obvious. This is my current implementation:
import numpy as np
r = []
for i in range(np.shape(rz)[2]):
r.append(ry[..., i] # rz[..., i])
r = np.array(r)
Assuming that the last dimension is the same, numpy.einsum should do the trick:
import numpy as np
np.einsum('ijk,jmk-> imk', ry, rz)
import numpy as np
A = np.array([[3, 6, 7], [5, -3, 0]])
B = np.array([[1, 1], [2, 1], [3, -3]])
C = A.dot(B)
print(C)
Output:
[[ 36 -12] [ -1 2]]
Related
I have a 2D numpy array:
[[1,2,3,4,5],[2,4,5,6,7],[0,9,3,2,4]]
I also have a second 1D array:
[2,3,4]
I want to replace all occurences of the elements of the second array with 0
So eventually, my second array should look like
[[1,0,0,0,5],[0,0,5,6,7],[0,9,0,0,0]]
is there a way in python/numpy I can do this without using a loop.
I already checked at np.where, but the condition there is only for example where element = 1 value, and not multiple.
Thanks a lot !
Use numpy.isin.
>>> import numpy as np
>>> a = np.array([[1,2,3,4,5],[2,4,5,6,7],[0,9,3,2,4]])
>>> b = np.array([2,3,4])
>>> a[np.isin(a, b)] = 0
>>> a
array([[1, 0, 0, 0, 5],
[0, 0, 5, 6, 7],
[0, 9, 0, 0, 0]])
Take a look at the following code in MATLAB:
a = [1,2; 5,6]
b = [-1,1; -1,1]
d = a(b(:)>0)
Now d will be the 2x1 array,[2;6]. This is because array b has positive entry only at the positions (1,2) and (2,2), and the third line of the code is extracting elements of a in those positions.
Is there an equivalent method in Python that does this? I searched numpy documentation but could not find any. In my actual code, I have multiple large, multidimensional arrays from which I would want to extract elements based on the elements of other arrays. Of course, this can be done with nested for loops but it would be much better if there is a nicer way like MATLAB does.
Assuming a and b are numpy arrays use:
d = a[b > 0]
In numpy, indexing is done with the [] operator.
Without using any libraries:
a = [[1, 2], [5, 6]]
b = [[-1, 1], [-1, 1]]
d = [
a_xy
for a_x, b_x in zip(a, b)
for a_xy, b_xy in zip(a_x, b_x)
if b_xy > 0
]
Using numpy:
import numpy as np
a = np.array([[1, 2], [5, 6]])
b = np.array([[-1, 1], [-1, 1]])
d = a[b > 0]
I have a list of numbers which I wish to add a second column such that the array becomes 2D like in the example below:
a = [1,1,1,1,1]
b = [2,2,2,2,2]
should become:
c = [[1,2],[1,2],[1,2],[1,2],[1,2]]
I am not sure how to do this using numpy?
I would just stack them and then transpose the resulting array with .T:
import numpy as np
a = np.array([1, 1, 1, 1, 1])
b = np.array([2, 2, 2, 2, 2])
c = np.stack((a, b)).T
Use numpy built-in functions:
import numpy as np
c = np.vstack((np.array(a),np.array(b))).T.tolist()
np.vstack stacks arrays vertically. .T transposes the array and tolist() converts it back to a list.
Another similar way to do it, is to add a dimensions using [:,None] and then you can horizontally stack them without the need to transpose:
c = np.hstack((np.array(a)[:,None],np.array(b)[:,None])).tolist())
output:
[[1, 2], [1, 2], [1, 2], [1, 2], [1, 2]]
Is there a smart way to find the index of a list of values in a matrix using numpy? We can always do it in iterative way, but is there any quick and fast way available?
We have a matrix as:
[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
and a list:
[3,4,7,11]
What we need here is the index of 3,4,7 and 11 in corresponding rows of the matrix, i.e. [2,0,0,1].
It is obviously a simple code to write in iterative approach, but we are looking for any ready implementation.
Thanks in advance.
For your example this works:
In [17]: import numpy as np
In [18]: a = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
In [19]: l = [3,4,7,11]
In [20]: np.where(a == np.array(l)[:, None])[1]
Out[20]: array([2, 0, 0, 1])
Here is a duplicate-safe version of #Akavall's solution. If there are multiple occurrences of a value the first index is returned:
a = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
b = [3,4,7,11]
# introduce a repeated value
a[1][1] = a[1][0]
a
# [[1, 2, 3], [4, 4, 6], [7, 8, 9], [10, 11, 12]]
i, j = np.where(a == np.c_[b])
j[i.searchsorted(range(len(b)))]
# array([2, 0, 0, 1])
You can use numpy.argwhere to get you desired indices
import numpy as np
arr = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
search_list = [3,4,7,11]
index_arr = [np.argwhere(arr==x).flatten()[1] for x in search_list]
#index_arr [2, 0, 0, 1]
Is there a way to return the indices of k-minimum values along an axis of a numpy array without using loops?
import numpy as np
x = np.array([[5, 2, 3],[1, 9, 2]]) # example data
k = 2 # return the indices of the 2 smallest values
np.argsort(x, axis=1)[:,0:k] # by row
array([[1, 2],
[0, 2]])