Introduction to Python for Engineers and Scientists by Sandeep Nagar
Author:Sandeep Nagar
Language: eng
Format: epub
Publisher: Apress, Berkeley, CA
6.12 Masking
Arrays can be indexed using the method of masking. Masking is a way to define the indexes as a separate object and then generate a new array from the original array using the mask as a rule. There are two ways that arrays can be masked: fancy indexing and indexing using boolean values. It is important to note that masking methods generate copies instead of views. The two methods are discussed in the following subsections.
6.12.1 Fancy Indexing
numpy offers quite unique indexing facilities. One of them is fancy indexing where an array of indexes can be used to generate an array of elements:
1 >>>a = numpy.arange(1000)**3# Generated cubes of first 1000 cubes
2 >>>i = numpy.array(numpy.arange(10)) # Generated an array of first 10 numbers starting from 0 upto 9
3 >>> i
4 array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
5 >>>a[i] # those elements of array 'a' which has indices of elemental values if array 'i'
6 array([ 0, 1, 8, 27, 64, 125, 216, 343, 512, 729])
7 >>>j = numpy.array(numpy.arange(0,50,10)) # j is an array of numbers from 0 to 50 with steps of 10
8 >>>j
9 array([ 0, 10, 20, 30, 40])
10 >>>a[j] # a[j] is the array of cubes indexed with array j
11 array([ 0, 1000, 8000, 27000, 64000])
12 >>>k = numpy.array( [ [ 1, 2], [ 11, 12 ] ] ) # k is a two dimensional array of indexes 1,2,11,12
13 >>>a[k] # a[k] is array made up of elements placed at indexes given by k
14 array([[ 1, 8],
15 [1331, 1728]])
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Hello! Python by Anthony Briggs(9914)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9795)
The Mikado Method by Ola Ellnestam Daniel Brolund(9777)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8296)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7778)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Windows APT Warfare by Sheng-Hao Ma(6823)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6553)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6419)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6413)
Kotlin in Action by Dmitry Jemerov(5062)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4316)
Functional Programming in JavaScript by Mantyla Dan(4038)
Solidity Programming Essentials by Ritesh Modi(3994)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3785)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3729)
The Ultimate iOS Interview Playbook by Avi Tsadok(3704)
