Skip to content

Instantly share code, notes, and snippets.

@Robofied
Created February 15, 2019 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Robofied/0e5334b8d34cec37f23c0fbaabf8b2ea to your computer and use it in GitHub Desktop.
Save Robofied/0e5334b8d34cec37f23c0fbaabf8b2ea to your computer and use it in GitHub Desktop.
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