Arrays and numpy Practice#

Part 1#

import numpy as np

Creating and Inspecting arrays#

  1. Convert the following list into a numpy array

chlor_a_list = [0.3, 1.2, 0.8, 0.8, 1.1, 0.2, 0.4]
  1. Get the following values from the chlor_a array you made in the last problem:

  • The first value

  • The last value

  1. What is the data type of the chlor_a array you made?

  1. Use code to figure out how many items are in your array

Multiple dimensions#

  1. What is the shape of the following array?

population_sparrows = np.array([[43, 24, 53, 24], [21, 32, 42, 32], [76, 23, 14, 12]])
  1. Use the len() function and the .shape property to calculate the number of dimensions of the population_sparrows array

  1. Return the same result as problem 6 in a different way, using the .ndims property

  1. Get the value for the item in the last row and the last column of the population_sparrows array

  1. Get a 4-number array that is a subset of number from the population_sparrows array using the slice operator :

Math and aggregations#

For the next few problems consider that the population_sparrows array represents the populations of sparrows at 12 different reserach locations.

  1. Let’s say our sparrow population grew, and the population of every location doubled. Multiple all the values in the array by 2. Make sure array is updated with the new values.

  1. Later in the season a group of biologists adds a few sparrows to each population. The number of sparrows they added to each location is represented by the following array:

indiviuals_added = np.array([[4, 4, 5, 4], [1, 2, 2, 3], [6, 3, 4, 2]])

Calculate the updated population values with the new additions. Update the variable value.

  1. Calculate the sum of the sparrows at all the locations

  1. Calculate the sum of sparrows over axis 1

  1. Consider the following array. If you ran an aggregation (Ex. .max()) and specified an axis, over what axis would you get 3 numbers as a result?

example_array = np.array([[4, 4, 5, 4], [1, 2, 2, 3], [6, 3, 4, 2]])

Part 2#

Question 1#

reflectances = np.random.randint(0, high=100, size=(5, 30, 40))

A) Get a subset of at least 15 values from the middle of the reflectances array.

B) Write a chunk of code to get the value in the center of the array. Make sure the code works for arrays of different sizes, so calculate the center index values using the properties of the array.

Question 2#

A) Add 10 to every value in the reflectances array which has an index of 2 in the axis=0 position.

Question 3#

A) What will be the shapes of the following arrays? First take a guess, then run the code.

array1 = np.array([1, 2, 3])
array2 = np.array([[1], [2], [3]])

B) Starting with an array of all zeros, what how will the output look different adding together starting_array + array1 vs. starting_array + array2? Make your guess first, then run the code to compare to your expectation.

starting_array = np.zeros((3,3))
array1 = np.array([1, 2, 3])
array2 = np.array([[1], [2], [3]])

Question 4#

A) Going back to our reflectances array, find the mean and standard deviation of all the values in the array.

reflectances = np.random.randint(0, high=100, size=(5, 30, 40))

B) What is the maximum value of the 2D array at index 1 of axis 0?

Question 5#

A) The .astype() method is a method that changes the datatype of the values in the array. It takes one argument - the new data type (which you type in without quotations).

Use the .astype() method on the reflectances array to change the data type to float.

# Data types don't need quotations, they are one of the few words you can just type
int
int

B) We have seen the use of axis as a kwarg in the .max() function. If you need to you can use multiple kwargs, which you seperate by commas.

The keepdims kwarg maintains the output value within the original axis they were calculated over. So if you took the sum of an array over axis 1, you would recieve the output array with a vertically stacked output. keepdims takes a boolean input - True if you would like to keep the dimensions and False if not.

Take the max of the reflectances array, using the axis kwarg with value 1 and the keepdims kwarg set to True. Try it again with the kwarg set to False. Take the shape of both outputs and notice how they change.

Note: The “keepdims” concept is a bit conceptually abstract and I couldn’t find a good visual illustration for it right now. If you leave this problem feeling a little iffy on “keepdims” that is totally fine and normal. The more important concept to be comfortable with is using multiple kwargs in a function/method.

Question 6#

example = np.array([[6, 10, 5, 9], [6, 9, 9, 11], [12, 14, 6, 3]])

A) Get a list of unique items in the example array.

Google help: “numpy unique values in an array”, or this stackoverflow.

B) Pick one of the values in the array and determine how many times that value occurs in the array.

Google help: “numpy number of occurances of a value” or this stackoverflow.

Question 7#

NaNs are an important data point when working with real data - rarely do you have a totally complete dataset.

You can make individual nans with np.NaN:

np.NaN
nan

Look at the docs for the function np.full() and create a new array of shape (4, 5, 6) filled with nan values.