Python detailed analysis of np.where() code application
[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])
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]])
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([['chosen', 'chosen'], ['chosen', 'chosen']], dtype='<U10')
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[索引]
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))
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)
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".
