Answers for "numpy where"

11

np.where

>>> a = np.arange(10)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.where(a < 5, a, 10*a)
array([ 0,  1,  2,  3,  4, 50, 60, 70, 80, 90])
Posted by: Guest on April-17-2020
8

numpy where

import numpy as np

# Return elements chosen from x or y depending on condition.
a = np.arange(10) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
single_where = np.where((a<5),-1,a) # [-1, -1, -1, -1, -1,  5,  6,  7,  8,  9]
multiple_where = np.where((a<5),-1,np.where((a>5),0,a)) # [-1, -1, -1, -1, -1,  5,  0,  0,  0,  0]
Posted by: Guest on June-19-2020

Python Answers by Framework

Browse Popular Code Answers by Language