Lecture Note
University
University of California San DiegoCourse
DSC 207R | Python for Data SciencePages
2
Academic year
2023
anon
Views
8
Numpy, ndarray indexing Introduction: A well-liked Python package for scientific computing is called NumPy. Given that multi-dimensional arrays are at the core of many scientific computing activities, it offersstrong capabilities for working with them. Building on the fundamentals of slice indexing, we'llexamine advanced indexing methods for accessing and permuting data in ndarrays in thisarticle. Slice Indexing: We discovered that slice indexing may be used to retrieve subsets of strings and lists. We can extract sub regions from ndarrays using a similar indexing method. In order to work witha three by four ndarray for several of our examples, let's first import NumPy as np. You'll seethat we filled up the values in the matrix with numbers that correspond to the row andcolumn to help us demonstrate how they function. We'll extract the top two rows (rows 0 and 1) using Slice Indexing, and then we'll take the first and second middle columns. We anticipate receiving a two by two matrix in response torunning this. Advanced Indexing Techniques: Let's now discuss sophisticated indexing methods. Slice indexing has several restrictions but is useful for accessing contiguous blocks of data in an ndarray. Accessing and changingndarray elements is made easier with advanced indexing. Boolean Indexing: We can choose elements from an ndarray based on a boolean condition using boolean indexing, a sort of advanced indexing. For illustration, suppose we wish to choose onlyvalues larger than 5 from an ndarray containing values from 0 to 9. Boolean indexing can beused to accomplish this: import numpy as np a = np .arange ( 10 ) b = a [a > 5] print(b) This will output [6 7 8 9] , which are the elements in a that satisfy the condition a > 5 . We can use boolean indexing for more complex conditions as well.
Integer Indexing: Integer indexing is another type of advanced indexing that allows us to select elements in an ndarray based on their position. For example, let's say we have an ndarray withdimensions (3,4) and we want to select the elements at positions (0,1), (1,2), and (2,3) . We can do this using integer indexing: import numpy as np a = np.array( [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] ) b = a [[0,1,2],[1,2,3]] print (b) Understanding Slicing in NumPy Slicing is a powerful tool for working with arrays in NumPy. At its most basic level, slicing allows you to select a subset of elements from an array. In NumPy, you can slice an arrayusing a syntax similar to that used for Python lists. For example, suppose you have an arraycalled an_array : an_array = np.array( [[11, 12, 13, 14], [21, 22, 23, 24],[31, 32, 33, 34],[41, 42, 43, 44]] ) To select a subset of rows and columns from this array, you can use slice notation: a_slice = an_array [1:3, 1:3] In this case, the slice notation 1:3 means "select rows 1 through 2 and columns 1 through 2" . The resulting slice, a_slice , will be a 2x2 array containing the elements [22, 23], [32, 33] . It's vital to keep in mind that a slice of an array in NumPy is by default a view into the original array rather than a clone. This implies that altering a slice also affects the originalarray. But you may use the clone() method to duplicate a slice: a_slice_copy = a_slice. copy () By employing this technique, you can be certain that any changes you make to the replica won't impact the original array.
Numpy, Ndarray Indexing
Please or to post comments