Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Numpy
## Searching
import numpy as np
## 1. argmax()
a = np.arange(6).reshape(2,3)
print(a)
#[Output]:
#[[0 1 2]
# [3 4 5]]
## argsort() for flattened array
np.argmax(a)
#[Output]:
#5
## agrsort() along first axis
np.argmax(a, axis=0)
#[Output]:
#array([1, 1, 1], dtype=int64)
## 2. argwhere()
## It is returning the indices of elements which are satisfying the condition.
np.argwhere(a>=2)
#[Output]:
#array([[0, 2],
# [1, 0],
# [1, 1],
# [1, 2]], dtype=int64)
## Counting
## count_nonzero()
np.count_nonzero(np.array([[0,1],[1,1],[2,0]]))
#[Output]:
#[[0 1]
# [1 1]
# [2 0]]
#4
np.count_nonzero(np.array([[0,1],[1,1],[2,0]]), axis=0)
#[Output]:
#array([2, 2], dtype=int64)
np.count_nonzero(np.array([[0,1],[1,1],[2,0]]), axis=1)
#[Output]:
#array([1, 2, 1], dtype=int64)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.