[PYTHON] [For beginners] I want to get the index of an element that satisfies a certain conditional expression

background

It may be normal for a professional pythonist, It takes time for me to understand If I could understand it, I was impressed by its strength. .. .. Because. .. ..

ndarray [conditional expression]

Determine whether each element meets or does not meet the condition in the matrix, The resulting True / False is output as a matrix with the same shape.

Example

arr = np.array([[0,1,2,3],[0,2,4,6]])
print(arr<3)
# [[ True  True  True False]
#  [ True  True False False]]

numpy.nonzero It gets the index of the non-zero element of the matrix and outputs it as an x, y separate ndarray array.


#Example definition
arr_int = np.array([[3,5,0],[0,4,0]])
arr_bool = np.array([[True,True,False],[False,True, False]])

# np.Use of nonzero
nonzero_int_row, nonzero_int_column = np.nonzero(arr_int)
nonzero_bool_row, nonzero_bool_column = np.nonzero(arr_bool)

#The value of each variable
# row: array([0, 0, 1])
# column: array([0, 1, 1]))

In this example, arr [0,0], arr [0,1], arr [1,1] are non-zero, so this is the result.

Combine!

arr = np.array([[0,1,2,3],[0,2,4,6]])
arr_bool = arr<3
nonzero_row, nonzero_column = np.nonzero(arr_bool)
# row: [0 0 0 1 1]
# column: [0 1 2 0 1]

comment

I got a comment! It means that np.where has the same function and is convenient! Why didn't the site I was looking at use this ...

Recommended Posts

[For beginners] I want to get the index of an element that satisfies a certain conditional expression
[For beginners] I want to explain the number of learning times in an easy-to-understand manner.
[AtCoder for beginners] A story about the amount of calculation that you want to know very roughly
The story of IPv6 address that I want to keep at a minimum
I want to add silence to the beginning of a wav file for 1 second
[Introduction to Python] How to get the index of data with a for statement
The story of Linux that I want to teach myself half a year ago
I want to get the operation information of yahoo route
Keras I want to get the output of any layer !!
I want to create a Dockerfile for the time being.
I want to get the name of the function / method being executed
I tried to get the index of the list using the enumerate function
Python techniques for those who want to get rid of beginners
A story that is a little addicted to the authority of the directory specified by expdp (for beginners)
I measured 6 methods to get the index of the maximum value (minimum value) of the list
I want to sort a list in the order of other lists
I want to color a part of an Excel string in Python
I want to identify the alert email. --Is that x a wildcard? ---
I want to leave an arbitrary command in the command history of Shell
Python: I want to measure the processing time of a function neatly
I want to get the path of the directory where the running file is stored.
I want to create an API that returns a model with a recursive relationship in the Django REST Framework
I want to write an element to a file with numpy and check it.
I made an appdo command to execute a command in the context of the app
I want to set a life cycle in the task definition of ECS
I want to get League of Legends data ③
I want to get League of Legends data ②
I want to see a list of WebDAV files in the Requests module
I want to create a web application that uses League of Legends data ①
Use twitter API to get the number of tweets related to a certain keyword
I want to store the result of% time, %% time, etc. in an object (variable)
Python script to get a list of input examples for the AtCoder contest
I want to customize the appearance of zabbix
I want to get League of Legends data ①
Library for "I want to do that" of data science on Jupyter Notebook
After hitting the Qiita API with Python to get a list of articles for beginners, we will visit the god articles
[Python] I want to use only index when looping a list with a for statement
I want to take a screenshot of the site on Docker using any font
I want to create a lunch database [EP1] Django study for the first time
I want to create a lunch database [EP1-4] Django study for the first time
[Memorandum] ① Get and save tweets ~ I want to identify the news tweets that are spread ~
I want to grep the execution result of strace
I tried the MNIST tutorial for beginners of tensorflow.
I want to fully understand the basics of Bokeh
I want to install a package of Php Redis
Python: Get a list of methods for an object
I want to increase the security of ssh connections
I made a program in Python that changes the 1-minute data of FX to an arbitrary time frame (1 hour frame, etc.)
I made a tool to get the answer links of OpenAI Gym all at once
Get the value of a specific key up to the specified index in the dictionary list in Python
[Python] I want to make a 3D scatter plot of the epicenter with Cartopy + Matplotlib!
I want to find a stock that will rise 5 minutes after the Nikkei Stock Average rises
I want to find the intersection of a Bezier curve and a straight line (Bezier Clipping method)
I tried to create a Python script to get the value of a cell in Microsoft Excel
I want to output a beautifully customized heat map of the correlation matrix. matplotlib edition
[TensorFlow] I want to master the indexing for Ragged Tensor
I tried to get the location information of Odakyu Bus
I want to move selenium for the time being [for mac]
[Python] I want to get a common set between numpy
I want to start a lot of processes from python
I want to use only the normalization process of SudachiPy