NumPy (continued)

Key NumPy Functions

The following are functions from NumPy that we will be using a lot.

np.arange()

np.arange() is a function that creates arrays of numerically order sets of numbers. It can take in either 1, 2 , or 3 arguments. When it uses 3 arguments, the first is the starting number that is included in your array, the second is the ending number which is not included in the array, and the third is how far apart the numbers in your array will be.

### 3 Arguments
np.arange(beginning, end, space_between)

### 2 Arguments
### Assumes the space_between is 1.
np.arange(beginning, end)

### 1 Argument
### Assumes the beginning is 0 and the space_between is 1.
np.arange(end)

Let's go through some examples using 3 arguments.

>>> np.arange(0, 10, 1)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

### Notice that 10 is not included in our range.​
>>> np.arange(3, 45, 2)
array([ 3,  5,  7,  9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35,
        37, 39, 41, 43])

### The end value is never included.     
>>> np.arange(0, 15, 5)
array([ 0,  5, 10])

>>> np.arange(0, 16, 5)
array([ 0,  5, 10, 15])

The end argument in your range is not included, but the beginning value will be included. The end argument should always be one more than the number you want included in the array.

### 2 Arguments
### Always has a distance of 1 between the numbers in the array.
>>> np.arange(12, 20)
array([12, 13, 14, 15, 16, 17, 18, 19])

>>> np.arange(23, 39)
array([23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38])​​

### 1 Argument
### Always starts at 0, and ends one before the number inputted.
>>> np.arange(5)
array([0, 1, 2, 3, 4])

>>> np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

np.count_nonzero()

np.count_nonzero() is a function that will count the number of True values in an array.

All numbers except 0 are treated as True values. 0 is considered a Falsevalue.

You can insert numbers into the array or you can insert a boolean condition applied to an array. Here are some examples.

>>> one_zero = np.array([1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1])
>>> one_zero
array([1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1])

>>> np.count_nonzero(one_zero)
7

>>> twe = np.arange(12)
>>> twe​
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])

### Finds the number of items greater than 2 in the array.
>>> np.count_nonzero(twe > 2) 
9

### Finds the number of items less than 2 in the array.
>>> np.count_nonzero(twe < 2)
2

### Finds the number of items equal to 2 in the array.
>>> np.count_nonzero(twe == 2)
1

np.random.choice()

np.random.choice() is a function that will randomly select items from an array.

When it selects from an array, it needs at least 1 argument. The argument is the array you want to select from. However, the common second argument is the number of items you want selected from your array. If the second argument is not specified, the function returns a single element by default. Having the second argument will convert the item or items selected into an array. Some examples of its functionality are shown below:

>>> letters = np.array(["a", "s", "d", "f", "g", "h", "j", "k", "l"])
>>> letters
array(['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'], dtype='<U1')

>>> np.random.choice(letters)
'l'

>>> np.random.choice(letters, 1)
array(['a'], dtype='<U1')

>>> np.random.choice(letters, 5)
array(['l', 'd', 'd', 'k', 'l'], dtype='<U1')

You can select from an array of strings, integers, or floats to name a few. It is not limited to strings.

>>> five = np.arange(5) 
>>> five
array([0, 1, 2, 3, 4])

>>> np.random.choice(five)
2

>>> np.random.choice(five, 3)
array([0, 2, 1])

Likewise, a possible third argument is replace = ... . Replace is allows you to randomly select with or without replacement. In this case, you must either state replace = True for with replacement or replace = False for without replacement. If you draw with replacement it means you can have duplicates in your randomly selected array. If you draw without replacement, each item you select will be unique (only for that draw).

Here are some examples.

>>> np.random.choice(letters, 7, replace = True)
array(['d', 's', 'd', 'a', 'l', 'h', 'h'], dtype='<U1')

>>> np.random.choice(letters, 6, replace = False)
array(['g', 'h', 'k', 'a', 'j', 'f'], dtype='<U1')

The general form of the function is as follows:

np.random.choice(array_to_select_from, number_selected, replace = ...)

Computations / Array Arithmetic

A benefit that results from arrays having the same data type is that it allows us to apply addition, subtraction, multiplication, and division to entire arrays without fear. This can occur, because we will know whether we can use any of those computations just by looking at one item from our array since all items in the array have the same data type.

Here is an example of how we can change an array mathematically. The changes must be saved by defining a variable.

>>> t = np.arange(10)
>>> t
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

>>> tens = 10 * t
>> tens
array([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90])

>>> proportions = tens / 100
>>> proportions
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])

>>> zeros = t - t
>>> zeros
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

>>> double = t + t
>>> double
array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18])

>>> plus_five = t + 5
>>> plus_five
array([ 5,  6,  7,  8,  9, 10, 11, 12, 13, 14])

>>> minus_four = t - 4
>>> minus_four
array([-4, -3, -2, -1,  0,  1,  2,  3,  4,  5])

Summary

  • NumPy allows us to use arrays, which can be quicker and more efficient than lists.

  • Arrays are lists that hold only the same value types.

    • Remember that arrays are indexed starting at 0.

Last updated