Array Indexing and Slicing¶
Array Indexing¶
This also follows zero based indexing like python lists
1D Array Indexing¶
One-dimensional arrays can be indexed, sliced and iterated over, much like lists and other Python sequences. The (start:stop:step) notation for slicing is used.
1D array at index
i
Returns the
ith
element of an arraySyntax:
array[i]
2D Array Indexing¶
2D array at index
[i][j]
Returns the
[i][j]
element of an arraySyntax:
array[i][j]
Note
First,
A2[0]
= [0, 1, 3], which is the first row of arrayA2
Second,
A2[0]
select the first element of first row.
Consider an array students, it contains the test scores in two courses of the students against their names
Array Slicing¶
1D Array Slicing¶
Fancy Indexing - Integer Arrays¶
NumPy arrays can be indexed with slices, but also with boolean or integer arrays (masks). It means passing an array of indices to access multiple array elements at once. This method is called fancy indexing. It creates copies not views.
Suppose we want to access three different elements. We could do it like this:
Alternatively, we can pass a single list or array of indices to obtain the same result:
When using fancy indexing, the shape of the result reflects the shape of the index arrays rather than the shape of the array being indexed
We can also give indexes for more than one dimension. The arrays of indices for each dimension must have the same shape.
We will now select the corner elements of this array¶
Notice that the first value in the result is food[0,0], next is food[0,3] , food[2,0] and lastly food[2,3]
Modifying Values with Fancy Indexing¶
Just as fancy indexing can be used to access parts of an array, it can also be used to modify parts of an array.
We can use any assignment-type operator for this. Consider following example:
Fancy Indexing - Boolean Arrays¶
When we index arrays with arrays of (integer) indices we are providing the list of indices to pick. With boolean indices the approach is different; we explicitly choose which items in the array we want and which ones we don’t.
Frequently this type of indexing is used to select the elements of an array that satisfy some condition
Now we find the elements that are greater than 9. This will return a numpy array of the same shape as our original array.
We use this array to select elements in a corresponding to ‘true’ values in the boolean array.
We can do all of the above in a single concise statement
Structured Arrays¶
Structured arrays or record arrays are useful when you perform computations, and at the same time you could keep closely related data together. Structured arrays provide efficient storage for compound, heterogeneous data.
NumPy also provides powerful capabilities to create arrays of records, as multiple data types live in one NumPy array. However, one principle in NumPy that still needs to be honored is that the data type in each field (think of this as a column in the records) needs to be homogeneous.
Imagine that we have several categories of data on a number of students say, name, roll number, and test scores.
There’s nothing here that tells us that the three arrays are related; it would be more natural if we could use a single structure to store all of this data.
Define the np array with the names of the ‘columns’ and the data format for each¶
U10 represents a 10-character Unicode string
i4 is short for int32 (i for int, 4 for 4 bytes)
f8 is shorthand for float64
np.zeros() for a string sets it to an empty string¶
Now that we’ve created an empty container array, we can fill the array with our lists of values
The handy thing with structured arrays is that you can now refer to values either by index or by name
If you index student_data at position 1 you get a structure:
Get the name attribute from the last row¶
Get names where score is above 85¶
Note that if you’d like to do any operations that are any more complicated than these, you should probably consider the Pandas package with provides a powerful data structure called data frames.