Python detailed analysis of np.where() code application

WBOY
Release: 2022-08-24 09:11:30
forward
2329 people have browsed it

[Related recommendations: Python3 video tutorial]

np.where has two usages:

A kind of np.where(condition, x, y), that is, condition is the condition. When the condition is met, the output is x, if the condition is not met, the output is y. Directly enter the code:

a = np.arange(10)
//array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
print(np.where(a > 5, 1, -1))
//array([-1, -1, -1, -1, -1, -1,  1,  1,  1,  1])
Copy after login

Above It is quite easy to understand, but the example on the official website is not easy to understand, as shown below:

np.where([[True,False], [True,True]],   
			 [[1,2], [3,4]],
             [[9,8], [7,6]])
// 输出 array([[1, 8], [3, 4]])
Copy after login

It can be understood in this way. The bool value in the first line represents the condition, which means whether to take the value. First, look at [ True, False], that is, the first True value means that the first row takes the value 1 in [1, 2] in the first row, instead of taking the 9 below, False means not taking the value in [1, 2] in the first row 2, and take the 8 in the second row [9, 8]. The following is the same as [3, 4].
For the convenience of understanding, let’s give another example:

a = 10
>>> np.where([[a > 5,a < 5], [a == 10,a == 7]],
             [["chosen","not chosen"], ["chosen","not chosen"]],
             [["not chosen","chosen"], ["not chosen","chosen"]])

//array([[&#39;chosen&#39;, &#39;chosen&#39;], [&#39;chosen&#39;, &#39;chosen&#39;]], dtype=&#39;<U10&#39;)
Copy after login

The first row a> 5True, then take the first value of the first row, a<5 take the second value of the second row, the same applies below.

After understanding the first method, let’s look at np.where The second method:

That is, np.where(condition), only the condition (condition), without x and y, then output the coordinates of the element that satisfies the condition (that is, non-0) (equivalent to numpy.nonzero) . The coordinates here are given in the form of a tuple. Usually, the output tuple contains several arrays, corresponding to the coordinates of each dimension of the elements that meet the conditions.

>>> a = np.array([2,4,6,8,10])
>>> np.where(a > 5)			
//(array([2, 3, 4]),)   返回索引值
>>> a[np.where(a > 5)]  			
//array([ 6,  8, 10]) 返回元素值,即a[索引]
Copy after login

Give a code example that I encountered:

a = array([[0., 1.],
  	       [0., 1.],
  	       [0., 1.],
  	       [0., 1.],
  	       [0., 1.],
  	       [0., 1.],
  	       [0., 1.],
  	       [0., 1.],
  	       [1., 0.],
  	       [0., 1.],
  	       [0., 1.],
  	       [0., 1.],
  	       [1., 0.],
  	       [1., 0.],
  	       [0., 1.],
  	       [0., 1.],
  	       [1., 0.],
  	       [0., 1.],
  	       [1., 0.],
  	       [0., 1.]])
np.where(a == 1)
//(array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
//        17, 18, 19], dtype=int64),
// array([1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1],
//       dtype=int64))
Copy after login

The two array components returned indicate which row and which value is 1, so the first one in the result The first array array represents the row index, and the second array array represents the column index, which is the broken silver index of 1.

Attachment: np.where() multi-condition usage

1.np.where (condition,x,y) When there are three parameters in where, the first parameter represents the condition. When the condition is true, the where method returns x. When the condition is not true, where returns y

2.np.where( condition) When there is only one parameter in where, that parameter represents the condition. When the condition is true, where returns the coordinates of each element that meets the condition condition in the form of a tuple

3. More The condition is condition, & means and, | means or. For example, a = np.where((0Note that x, y must maintain the same size as a.

For example:

import numpy as np

data = np.array([[0, 2, 0],
                           [3, 1, 2],
                           [0, 4, 0]])
new_data = np.where((data>= 0) & (data<=2), np.ones_like(data), np.zeros_like(data))
print(new_data)
Copy after login

Result:

[[1 1 1]
[0 1 1]
[1 0 1]]

It can be seen that as long as each element in data satisfies data>=0 and data<=2, np.ones_like(data)the value of the corresponding coordinates will be returned if it is satisfied, no If satisfied, the value of np.zeros_like(data) corresponding coordinates will be returned. Of course, x and y can be changed to other values, as long as they have the same size as the conditions.

【Related recommendations: Python3 video tutorial

The above is the detailed content of Python detailed analysis of np.where() code application. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:jb51.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template