Created
February 15, 2019 18:20
-
-
Save Robofied/0e5334b8d34cec37f23c0fbaabf8b2ea to your computer and use it in GitHub Desktop.
Numpy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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