How to get indices of np.amin of 3d np array? - python

Given the following code:
import numpy as np
x = np.array([[[1, 2],
[3, 4]],
[[5, 6],
[7, 8]],
[[3, 1],
[1, 5]]])
x_min = np.amin(x, axis=0)
print(x_min)
The output (x_min) is
[[1 1]
[1 4]]
Now I want to get the indices of dimension 0 of array x for the results of x_min array, it should be:
[[0 2]
[2 0]]
Which function can I use to get this indices?

Try np.argmin: np.argmin(x, axis=0)

Related

Finding a row in 2d Numpy array with values x,y,z in

I have
import numpy as np
np_array = np.array([[0,4],[0,5],[3,5],[6,8],[9,1],[6,1]])
rows=np.where(np_array[:,0]==6)and np.where(np_array[:,1]==1)
print(np_array[rows])
Expected answer
[6,1]
Answer from code
[[9 1]
[6 1]]
What I would like is the index of where [6,1] is.
I must be missing something.
Without where, just using indexing.
import numpy as np
np_array = np.array([[0, 4], [0, 5], [3, 5], [6, 8], [9, 1], [6, 1]])
rows = (np_array[:, 0] == 6) & (np_array[:, 1] == 1)
print(np_array[rows][0])
You can get the indices with np.where(rows).

How to extract the upper values from the whole 3D array in python

Is it possible to extract the upper values from the whole 3D array?
A simple example of a 3D array is below:
import numpy as np
a = np.array([[[7, 4, 2], [5, 0, 4], [0, 0, 5]],
[[7, 6, 1], [3, 9, 5], [0, 8, 7]],
[[8, 10, 3], [1, 2, 15], [9, 0, 1]]])
You can use the numpy building-matrices functions like numpy.triu (triangle-upper) or numpy.tril (triangle-lower) to return a copy of a matrix with the elements above or below the k-th diagonal zeroed.
If, on the other hand, you are only interested in the values ​​above or below the diagonal (without having a copy of the matrix), you can simply use numpy.triu_indices and numpy.tril_indices, as follows:
upper_index = np.triu_indices(n=3, k=1)
where n is the size of the arrays for which the returned indices will be valid, and k the diagonal offset.
and return the indices for the triangle. The returned tuple contains two arrays, each with the indices along one dimension of the array:
(array([0, 0, 1], dtype=int64), array([1, 2, 2], dtype=int64))
now you can use the indexes obtained as indexes of your array and you will get:
a[upper_index]
and gives:
array([[5, 0, 4],
[0, 0, 5],
[0, 8, 7]])
Similarly you can find the part under the diagonal using numpy.tril_indices.
IUUC, You could use triu_indices:
result = a[np.triu_indices(3)]
print(result)
Output
[[7 4 2]
[5 0 4]
[0 0 5]
[3 9 5]
[0 8 7]
[9 0 1]]
If you want those strictly above the diagonal, you can pass an offset value:
result = a[np.triu_indices(3, 1)]
print(result)
Output
[[5 0 4]
[0 0 5]
[0 8 7]]

how to get column, row number based on value in matrix from numpy

How to return column and row number based on value in matrix?
import numpy as np
matrix = np.random.randint(9, size=(3,3))
#matrix
#[[2 3 3]
# [6 2 4]
# [1 8 4]]
something like
matrix.where(2,[]) => (0,0), (1,1)
matrix.where(4,[]) => (1,2), (2,2)
Use np.argwhere function
import numpy as np
matrix = np.random.randint(9, size=(3,3))
print(matrix)
# array([[2, 1, 3],
# [7, 2, 4],
# [1, 7, 7]])
np.argwhere(matrix == 7)
Output:
array([[1, 0],
[2, 1],
[2, 2]], dtype=int64)
Use np.where function.
matrix = np.random.randint(9, size=(3,3))
np.where(matrix == 2)

How to search in a sorted 2d matrix

i have got list of x y coordinates:
import numpy as np
a=np.array([[2,1],[1,3],[1,5],[2,3],[3,5]])
that i've sorted with
a=np.sort(a,axis=0)
print a
>[[1 3] [1 5] [2 1] [2 3] [3 5]]
i'd like to perform a search :
a.searchsorted([2,1])
>Value error : object too deep for desired array
Any ideas how to do that ?
This gona work may be , if I got what you asking :
>>> a = [[1, 3], [1, 5], [2, 1], [2, 3], [3, 5]]
>>> [2, 1] in a
True

Sort a numpy array like a table

I have a list
[[0, 3], [5, 1], [2, 1], [4, 5]]
which I have made into an array using numpy.array:
[[0 3]
[5 1]
[2 1]
[4 5]]
How do I sort this like a table? In particular, I want to sort by the second column in ascending order and then resolve any ties by having the first column sorted in ascending order. Thus I desire:
[[2 1]
[5 1]
[0 3]
[4 5]]
Any help would be greatly appreciated!
See http://docs.scipy.org/doc/numpy/reference/generated/numpy.lexsort.html#numpy.lexsort
Specifically in your case,
import numpy as np
x = np.array([[0,3],[5,1],[2,1],[4,5]])
x[np.lexsort((x[:,0],x[:,1]))]
outputs
array([[2,1],[5,1],[0,3],[4,5]])
You can use numpy.lexsort():
>>> a = numpy.array([[0, 3], [5, 1], [2, 1], [4, 5]])
>>> a[numpy.lexsort(a.T)]
array([[2, 1],
[5, 1],
[0, 3],
[4, 5]])
Another way of doing this - slice out the bits of data you want, get the sort indices using argsort, then use the result of that to slice your original array:
a = np.array([[0, 3], [5, 1], [2, 1], [4, 5]])
subarray = a[:,1] # 3,1,1,5
indices = np.argsort(subarray) # Returns array([1,2,0,3])
result = a[indices]
Or, all in one go:
a[np.argsort(a[:,1])]
If you want to sort using a single column only (e.g., second column), you can do something like:
from operator import itemgetter
a = [[0, 3], [5, 1], [2, 1], [4, 5]]
a_sorted = sorted(a, key=itemgetter(1))
If there are more than one key, then use numpy.lexsort() as pointed out in the other answers.

Categories

Resources