logo

numpy.where() Python

NumPy modulis suteikia funkciją numpy.where() elementams pasirinkti pagal sąlygą. Jis grąžina elementus, pasirinktus iš a arba b, priklausomai nuo sąlygos.

Pvz., jei visi argumentai -> sąlyga, a & b perduodami numpy.where(), tada jis grąžins elementus, pasirinktus iš a & b, atsižvelgiant į reikšmes bool masyve, kurias suteikia sąlyga.

Jei pateikiama tik sąlyga, ši funkcija yra funkcijos np.asarray (sąlyga).nonzero() santrumpa. Nors ne nuliui, pirmenybė turėtų būti teikiama tiesiogiai, nes ji tinkamai elgiasi poklasiuose.

Sintaksė:

 numpy.where(condition[, x, y]) 

Parametrai:

Tai yra šie parametrai numpy.where() funkcijoje:

paprasta java programa

sąlyga: array_like, bool

Jei šis parametras nustatytas į True, išeiga x, kitu atveju derlius y.

x, y: array_like:

istorija java

Šis parametras apibrėžia reikšmes, iš kurių galima pasirinkti. X, y ir sąlyga turi būti transliuojamos tam tikra forma.

Grąžinimai:

Ši funkcija grąžina masyvą su elementais iš x, kur sąlyga yra True, ir elementus iš y kitur.

1 pavyzdys: np.where()

 import numpy as np a=np.arange(12) b=np.where(a<6,a,5*a) b < pre> <p> <strong>In the above code</strong> </p> <ul> <li>We have imported numpy with alias name np.</li> <li>We have created an array &apos;a&apos; using np.arange() function.</li> <li>We have declared the variable &apos;b&apos; and assigned the returned value of np.where() function.</li> <li>We have passed the array &apos;a&apos; in the function.</li> <li>Lastly, we tried to print the value of b.</li> </ul> <p>In the output, the values ranging from 0 to 5 remain the same as per the condition, and the other values have been multiplied with 5.</p> <p> <strong>Output:</strong> </p> <pre> array([ 0, 1, 2, 3, 4, 5, 30, 35, 40, 45, 50, 55]) </pre> <h3>Example 2: For multidimensional array</h3> <pre> import numpy as np a=np.arange(12) b=np.where([[True, False], [True, True]],[[1, 2], [3, 4]],[[9, 8], [7, 6]]) b </pre> <p> <strong>Output:</strong> </p> <pre> array([[1, 8], [3, 4]]) </pre> <h3>Example 3: Broadcasting x, y, and condition</h3> <pre> import numpy as np x, y = np.ogrid[:3, :4] a=np.where(x &gt; y, x, 10 + y) a </pre> <p> <strong>Output:</strong> </p> <pre> array([[10, 11, 12, 13], [ 1, 11, 12, 13], [ 2, 2, 12, 13]]) </pre> <p> <strong>In the above code</strong> </p> <ul> <li>We have imported numpy with alias name np.</li> <li>We have created an array &apos;a&apos; using np.arange() function. </li> <li>We declared the variable &apos;b&apos; and assigned the returned value of np.where() function.</li> <li>We have passed a multidimensional array of boolean as a condition and x and y as an integer arrays.</li> <li>Lastly, we tried to print the value of b.</li> </ul> <p>In the output, the x value has been compared to y value if it satisfied the condition, then it will be printed x value otherwise, it will print y value, which has passed as an argument in the where() function.</p> <h3>Example 4: Broadcasting specific value</h3> <pre> x=np.array([[0,1,2],[0,2,5],[0,4,8]]) y=np.where(x<4,x,-2) y < pre> <p> <strong>Output:</strong> </p> <pre> array([[ 0, 1, 2], [ 0, 2, -2], [ 0, -2, -2]]) </pre> <hr></4,x,-2)></pre></6,a,5*a)>

2 pavyzdys: Daugiamatis masyvas

 import numpy as np a=np.arange(12) b=np.where([[True, False], [True, True]],[[1, 2], [3, 4]],[[9, 8], [7, 6]]) b 

Išvestis:

 array([[1, 8], [3, 4]]) 

3 pavyzdys: transliavimas x, y ir sąlyga

 import numpy as np x, y = np.ogrid[:3, :4] a=np.where(x &gt; y, x, 10 + y) a 

Išvestis:

 array([[10, 11, 12, 13], [ 1, 11, 12, 13], [ 2, 2, 12, 13]]) 

Aukščiau pateiktame kode

  • Mes importavome numpy su slapyvardžiu np.
  • Sukūrėme masyvą „a“ naudodami funkciją np.arange().
  • Mes paskelbėme kintamąjį „b“ ir priskyrėme grąžintą funkcijos np.where() reikšmę.
  • Perdavėme daugiamatį loginės reikšmės masyvą kaip sąlygą ir x bei y kaip sveikųjų skaičių masyvus.
  • Galiausiai bandėme atspausdinti b reikšmę.

Išvestyje x reikšmė buvo palyginta su y reikšme, jei ji tenkino sąlygą, tada ji bus atspausdinta x reikšmė priešingu atveju, ji atspausdins y reikšmę, kuri perduota kaip argumentas funkcijoje where().

4 pavyzdys: konkreti transliavimo vertė

 x=np.array([[0,1,2],[0,2,5],[0,4,8]]) y=np.where(x<4,x,-2) y < pre> <p> <strong>Output:</strong> </p> <pre> array([[ 0, 1, 2], [ 0, 2, -2], [ 0, -2, -2]]) </pre> <hr></4,x,-2)>