Using a particular axis of an array#

Many numpy functions comes with an extra argument axis this allow you to perform them only with respect to a specified axis. The numbering of axis starts with 0. So a two dimensional array has two axis, 0 and 1.

(Arrays are not just limited to two dimentions however, they may have 3 or more, though this becomes somewhat difficult for us to visualise. You may see examples of 3D arrays to store complicated datasets during your studies.)

import numpy as np #Run this cell or none of the np commands will work
example_array = np.array([[1, 2, 3], [5, 6, 7]])
print("The regular sum is")
print(example_array.sum())
print("The sum with respect to axis=0 is")
print(example_array.sum(axis=0))
print("The sum with respect to axis=1 is")
print(example_array.sum(axis=1))
The regular sum is
24
The sum with respect to axis=0 is
[ 6  8 10]
The sum with respect to axis=1 is
[ 6 18]
array1 = np.array([[1,2], [3, 4]])
array2 = np.array([[5, 6]])
print(np.concatenate((array1, array2), axis=0))
[[1 2]
 [3 4]
 [5 6]]

Exercise 7.1#

Write a function that has one input

  • a two dimensional numpy array.

You function should calculate the mean of every row of the input array, and return the largest mean.