How to use numpy.where
This article is to share with you about how to use numpy.where. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
A simple description of the function:
First of all, the dimensions of the condition, x and y multi-dimensional arrays must be broadcasted. Then return a multi-dimensional array whose value is determined according to condition. If the value of condition is True, then take the value from x, otherwise take the value from y.
The lab code shows:
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32Type "help", "copyright", "credits" or "license ()" for more information. > > import numpy as np > > a = np.arange (10) > > aarray ([0,1,2,3,4,5,6,7,8,9]) > > np.where (a)
< 5, a, 20200910*a)array([ 0, 1, 2, 3, 4, 101004550, 121205460, 141406370, 161607280, 181808190])>> > np.where (a
< 5, a, 10*a)array([ 0, 1, 2, 3, 4, 50, 60, 70, 80, 90])>> aarray ([0,1,2,3,4,5,6,7,8,9]) > > np.where ([[True, False], [True, True]], [[1,2], [3,4]], [[9,8], [7,6]]) array ([1,8], [3,4]) > > x, y = np.ogrid [: 3,: 4] > > xarray ([[0], [1]) [2]) > yarray ([[0, 1, 2, 3]]) > np.where (x
< y, x, 10 + y) # both x and 10+y are broadcastarray([[10, 0, 0, 0], [10, 11, 1, 1], [10, 11, 12, 2]])>> a = np.array ([[0,1,2], [0,2,4], [0,3,6]) > aarray ([[0,1,2], [0,2,4], [0,3,6]]) > np.where (a)
< 4, a, -1) # -1 is broadcastarray([[ 0, 1, 2], [ 0, 2, -1], [ 0, 3, -1]])>Thank you for reading! This is the end of the article on "how to use numpy.where". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!