- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Use an index array to construct a new array from a set of choices in Numpy
A new array from the set of choices is constructed using the numpy.ma.choose() method in Python Numpy. Given an array of integers and a list of n choice arrays, this method will create a new array that merges each of the choice arrays. Where a value in index is i, the new array will have the value that choices[i] contains in the same place.
The choices parameter is the choice arrays. The index array and all of the choices should be broadcastable to the same shape.
The mode parameter specifies how out-of-bounds indices will behave −
- 'raise' : raise an error
- 'wrap' : wrap around
- 'clip' : clip to the range
Steps
At first, import the required library −
import numpy as np
Set choices array −
arr_choices = np.array([[5, 10, 15, 20, 25], [50, 55, 60, 65, 70], [100, 105, 110, 115, 120], [150, 155, 160, 165, 170], [200, 205, 210, 215, 220]])
Create a new array −
arr = np.array([2, 3, 4, 1, 0])
Displaying the array −
print("Array...
",arr)
Displaying the choices array −
print("
Choices Array...
",arr_choices)
A new array from the set of choices is constructed using the choose() method −
arrRes = np.ma.choose(arr, arr_choices)
The first element will be the first element of the third (2+1) "array" in choices. The second element will be the second element of the fourth (3+1) choice array. The third element will be the third element of the fifth (4+1) choice array, etc −
print("
New Array from set of choices...
",arrRes)
Example
import numpy as np # set choices array arr_choices = np.array([[5, 10, 15, 20, 25], [50, 55, 60, 65, 70], [100, 105, 110, 115, 120], [150, 155, 160, 165, 170], [200, 205, 210, 215, 220]]) # Create a new array arr = np.array([2, 3, 4, 1, 0]) # Displaying the array print("Array...
",arr) # Displaying the choices array print("
Choices Array...
",arr_choices) # A new array from the set of choices is constructed using the choose() method arrRes = np.ma.choose(arr, arr_choices) # The first element will be the first element of the third (2+1) "array" in choices, # The second element will be the second element of the fourth (3+1) choice array # The third element will be the third element of the fifth (4+1) choice array, etc. print("
New Array from set of choices...
",arrRes)
Output
Array... [2 3 4 1 0] Choices Array... [[ 5 10 15 20 25] [ 50 55 60 65 70] [100 105 110 115 120] [150 155 160 165 170] [200 205 210 215 220]] New Array from set of choices... [100 155 210 65 25]
- Related Articles
- Use an index array to construct a new array from a set of choices with clip mode in Numpy
- Use an index array to construct a new array from a set of choices with wrap mode in Numpy
- Create a new array from the masked array and return a new reference in Numpy
- Construct a record array from a wide-variety of objects in Numpy
- New Array from Index Range Swift
- Construct an array from XOR of all elements of array except element at same index in C++
- Move axes of a Numpy array to new positions
- Construct an array by executing a function over each coordinate in Numpy
- Create a record array from a (flat) list of array and fetch specific values based on index in Numpy
- Create a boolean mask from an array in Numpy
- Create a record array from a (flat) list of array and set a valid datatype for all in Numpy
- Left-justify elements of an array and set the characters to use for padding in Numpy
- Right-justify elements of an array and set the characters to use for padding in Numpy
- Create a record array from a (flat) list of array in Numpy
- Construct an array from GCDs of consecutive elements in given array in C++
