Menu
CoddyTech

Statistics by Axis

Part of the Data and Plots section of Coddy's Machine Learning journey. Lesson 12 of 60.

Two cafes counted their coffees for three days: one row per cafe, one column per day. An axis picks the direction to add in:

The cups table, 2 cafes by 3 days. Arrows run down each column to the day totals 80, 85 and 60, and across each row to the cafe totals 99 and 126

You pass it by name:

cups = np.array([[30, 45, 24],
                 [50, 40, 36]])
print(cups.sum(axis=0))
print(cups.sum(axis=1))

This prints:

[80 85 60]
[ 99 126]

The axis you name disappears: axis=0 folds the rows away, leaving one result per column. In ML data, that is one number per feature.

.argmax() says where the largest value is: cups.sum(axis=0).argmax() is 1, so Tuesday.

challenge icon

Challenge

Easy

A gym counted visitors for four weeks. In visits each row is a week and each column a weekday, named in days. Print:

  1. Per weekday: each weekday's mean.
  2. Per week: each week's mean.
  3. Busiest day: the name of the weekday with the highest mean.

Round the means to 2 decimals. For a gym with two weeks of data:

Per weekday: [42.  51.  46.  63.5 70.5]
Per week: [53.6 55.6]
Busiest day: Fri

Try it yourself

import numpy as np

days = ["Mon", "Tue", "Wed", "Thu", "Fri"]
visits = np.array([[52, 61, 48, 70, 66],
                   [58, 65, 51, 74, 69],
                   [49, 60, 47, 69, 71],
                   [55, 57, 50, 72, 60]])

# Write your code below
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Data and Plots

Practice on your own: Online Python compiler