Exercise B: NumPy#
2021-8-24
# import numpy and give it the alias np
B.1: 1d array#
# Define a numpy array A with three float elements using np.array()
# print the properties .ndim, .size and .shape of A
# Define another numpy array B with three float elements
# Take the sum of these arrays using '+'
# How does this differ from using '+' on lists?
# Create a numpy array z4 with four zeroes using np.zeros()
B.2: Indexing#
# print the last element of B
# print the two first elements in two different ways:
# a) using slicing A[i0:i1] (like lists)
# b) using array indexing A[idx] with idx=[i0,i1,i2,...] (not possible with lists)
B.3: Functions#
# take the sum of the elements in A by using np.sum() or A.sum()
# Find the mean and std in the same way
# sort the elements of A using np.sort()
B.4: NaN values#
# take the mean of A using np.mean()
# take the mean of a using np.nanmean()
B.5: Boolean arrays#
# create a boolean array idx showing which elements of B
# are larger than the B.mean()
# print idx
# print B[idx]
# use np.where to show the index of the True elements
B.6: 2D arrays#
# Create a 2D array a2D by using np.array() and a list of lists
# print the first row of a2D
# print the last row of a2D
# make a 1D array from a2D by using .flatten()