numpy.where() Python
The numpy.where() funkcija atgriež elementu indeksus ievades masīvā, kur ir izpildīts noteiktais nosacījums.
Sintakse : numpy.where(nosacījums[, x, y])
Parametri:
stāvoklis: Ja patiess, ienes x, pretējā gadījumā ienesīgums y.
x, y: Vērtības, no kurām izvēlēties. x, y un nosacījumam ir jābūt pārraidāmam noteiktā formā.Atgriež:
ārā: [ndarray vai ndarrays korte] Ja ir norādīti gan x, gan y, izvades masīvā ir elementi x, kur nosacījums ir True, un elementi no y citur.Ja ir dots tikai nosacījums, atgriež korteču condition.nonzero(), indeksus, kur nosacījums ir True.
Kods #1:
# Python program explaining> # where() function> > import> numpy as np> > np.where([[> True> ,> False> ], [> True> ,> True> ]],> > [[> 1> ,> 2> ], [> 3> ,> 4> ]], [[> 5> ,> 6> ], [> 7> ,> 8> ]])> |
Izvade:
array([[1, 6], [3, 4]])
Kods #2:
# Python program explaining> # where() function> > import> numpy as np> > # a is an array of integers.> a> => np.array([[> 1> ,> 2> ,> 3> ], [> 4> ,> 5> ,> 6> ]])> > print> (a)> > print> (> 'Indices of elements <4'> )> > b> => np.where(a <> 4> )> print> (b)> > print> (> 'Elements which are <4'> )> print> (a[b])> |
Izvade:
[[1 2 3] [4 5 6]] Indices of elements <4 (array([0, 0, 0], dtype=int64), array([0, 1, 2], dtype=int64)) Elements which are <4 array([1, 2, 3])