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:
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
EasyA gym counted visitors for four weeks. In visits each row is a week and each column a weekday, named in days. Print:
Per weekday:each weekday's mean.Per week:each week's mean.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: FriTry 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
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Data and Plots
2NumPy Arrays
Creating ArraysShape and SizeRanges of NumbersIndexing and SlicingRecap: Temperature Grid3Array Math
Math on Whole ArraysSummary StatisticsStatistics by AxisBroadcastingBoolean MasksRecap: Exam Scores9Grouping and Cleaning
Counting ValuesGrouping DataSeveral SummariesMissing ValuesRecap: Survey ResultsPractice on your own: Online Python compiler