Monday, July 20, 2020

Array Indexing and Array Slicing

Array Indexing and Array Slicing
Shape and Reshape
Iterating



Multidimensional arrays read right to left on indexing. Negative indexing is allowed, however, when you do negative indexing you start at -1, not zero.


Remember that slicing does not include the rightmost index.


NumPy lets you do stepping through arrays. Make sure to use two colons if using the optional step parameter.

A shortcut for grabbing every other element from the entire array 

[::2]

More on this stepping at the end of this post.


2-D arrays

import numpy as np

baskets = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])

print('4th element on 2nd dimension: ', baskets[1, 3])

# result is:

4th element on 2nd dimension: 9


Some special things about two-dimensional arrays:

The first argument can actually be a slice across dimensions; then for each dimension you can further index elements in that dimension. This means it will be returned to you as a smaller but still 2-dimensional array.


N-dimensional array

If you print the results you'll see each dimension has its own [] and then all ­the lists are in a mother list of sorts.

Example 3-D array


[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]

with the 1st dimension given by what is inside the blue braces, the 2nd dimension by what is inside the green braces and the 3rd dimension by what is inside the red braces. There are 2 pairs of green braces inside the single blue braces, so the 1st dimension is 2. There are 2 pairs of red braces inside each pair of green braces, so the 2nd dimension is 2.  There are 3 elements inside each pair of red braces, so the 3rd dimension is 3. 

In general, the total number of elements returned is the size of the slice of each dimension multiplied by each other.

In the example above, that would be 2 * 2 * 3 = 12, count them.

Another Example

print(some_array[1:3, 3:6, 2:8])

will print 2 * 3 * 6 = 36 elements, remembering that the leftmost accessing index on each dimension is NOT included.



NumPy has additional data types on top of the Python standard data types and they are represented by single characters. Apparently, they are 32 bit by default; see below for the list of NumPy data types.


i integer M datetime
b boolean         O object
u unsigned integer S string
f float U unicode string
c complex float V void
m timedelta


The .astype() member ­­­function of a NumPy object takes an optional dtype (some single character in single quotes) to communicate that you want the defined variable to have a certain NumPy data type. Numpy can also distinguish between 32 and 64-bit.


For i u f S and U you can do that bit precision specification as well as specify the number of bytes for those NumPy data types. Or if you don't want to do it that way, there is the .astype() member function of a NumPy object to convert to a different data type; or set the datatype from the outset if it's a new variable being created (with dtype = ' '). There is also a .dtype object property which will reveal the data type without changing it.


The .astype() member function, in addition to recognizing NumPy data types also accepts Python data types. So for example you could say dtype = float or you could say dtype = 'f'.


Be careful if you're converting back and forth between python data types and the equivalent NumPy data types as you might introduce rounding errors - see below:

import numpy as np

arr = np.array([1.1, 2.1, 3.1], dtype=float)

print(arr)
print(arr.dtype)

newarr = arr.astype('f')

print(newarr)
print(newarr.dtype)

newarr = arr.astype(float)

print(newarr)
print(newarr.dtype)


# results in:

[ 1.1  2.1  3.1]
float64
[ 1.10000002  2.0999999   3.0999999 ]
float32
[ 1.1  2.1  3.1]
float64


Using the .astype() when going from 'f' to 'i' will always implicitly do a floor() operation and round down.

You can tell Python to walk through an array by skipping certain elements in a regular interval pattern, as known as stepping.


import numpy as np

bus_stops = np.array(["Main St", "Oak Street", "Pine Street", "Maple Lane", "Harbor side", "Elm Street", "Depot"])

print(bus_stops [1:5:2])        # start : stop : interval

# results in:

['Oak Street' 'Maple Lane' 'Elm Street']

# because index 1 is Main St and step is 2 so skip 1 element and take the next one which is Maple Lane, skip one and next is Elm Street, skip one, then the end of array is reached. So the formula is if step is n, skip n-1 elements.




No comments:

Post a Comment