[PYTHON] How to extract other than a specific index with Numpy

It is a method to extract other than a specific index from an array.

# 0~Randomly fetch 10 from the number 99
arr = np.random.randint(0, 100, 10)
# > array([74, 29,  6, 79, 76, 13,  3, 56, 25, 50])

#I want to exclude odd-numbered numbers from arr
odd = [1,3,5,7,9]

#Method ① List comprehension
index = [i for i in np.arange(len(arr)) if i not in odd]
arr_even = arr[index]

#Method ② True/Mask with False
index = np.ones(len(arr), dtype=bool)
index[odd] = False
arr_even = arr[index]

#Method ③ np.delete
arr_even = np.delete(arr, odd)

Method ③ is the most refreshing, but it doesn't change much in terms of time, so please use your favorite method.

Recommended Posts

How to extract other than a specific index with Numpy
Extract elements other than a specific index with Numpy
[Python] How to get a value with a key other than value with Enum
How to use a file other than .fabricrc as a configuration file
How to get a specific column name and index name in pandas DataFrame
How to read a CSV file with Python 2/3
How to send a message to LINE with curl
How to draw a 2-axis graph with pyplot
How to develop a cart app with Django
How to create a multi-platform app with kivy
How to count numbers in a specific range
How to return multiple indexes with index method
[Introduction to Python] How to get the index of data with a for statement
How to use numpy
How to operate NumPy
How to convert / restore a string with [] in python
[Python] How to draw a line graph with Matplotlib
How to create a submenu with the [Blender] plugin
How to get a logged-in user with Django's forms.py
How to convert a class object to a dictionary with SQLAlchemy
How to make a shooting game with toio (Part 1)
How to get more than 1000 data with SQLAlchemy + MySQLdb
How to extract null values and non-null values with pandas
How to extract non-missing value nan data with pandas
[Python] How to create a 2D histogram with Matplotlib
[Python] How to draw a scatter plot with Matplotlib
Apply conda's env to a specific directory with pyenv-vertualenv
How to extract non-missing value nan data with pandas
[Python] I want to add a static directory with Flask [I want to use something other than static]
How to deploy a web app made with Flask to Heroku
How to make a Cisco Webex Teams BOT with Flask
How to put a hyperlink to "file: // hogehoge" with sphinx-> pdf
How to install NPI + send a message to line with python
How to kill a process instantly with Python's Process Pool Executor
How to convert an array to a dictionary with Python [Application]
How to output a document in pdf format with Sphinx
How to extract any appointment in Google Calendar with Python
How to call a function
[python] A note when trying to use numpy with Cython
How to create a flow mesh around a cylinder with snappyHexMesh
How to print characters as a table with Python's print function
How to update with SQLAlchemy?
How to make a simple Flappy Bird game with pygame
How to hack a terminal
How to install mkl numpy
A story about how to deal with the CORS problem
How to build a python2.7 series development environment with Vagrant
How to Alter with SQLAlchemy?
How to separate strings with','
How to RDP with Fedora31
How to Delete with SQLAlchemy?
How to insert a specific process at the start and end of spider with scrapy
How to touch Jupyter Notebook without polluting the environment other than Pythonista, or how to touch Ruby with Jupyter Notebook without polluting the environment other than Rubyist
How to set a shortcut to switch full-width and half-width with IBus
How to batch start a python program created with Jupyter notebook
How to extract features of time series data with PySpark Basics
[Python] How to force a method of a subclass to do something specific
[Introduction to Python] How to split a character string with the split function
[Python 3.8 ~] How to define a recursive function smartly with a lambda expression
How to make a command to read the configuration file with pyramid
How to make a surveillance camera (Security Camera) with Opencv and Python