Using particular axis of an array - SOLVED#

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.

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.

def largest_mean(arr1):
  mean_vals = np.mean(arr1,axis=1)
  return np.max(mean_vals)
input_matrix = np.array([[1, 2, 3],[5, 6, 7]])
largest_mean(input_matrix)
6.0