While you’ve shown that you can calculate the average yourself, it becomes time-consuming as the size of your dataset increases — imagine adding all of the numbers in a dataset with 10,000 observations.
.mean() function can do the work of adding and dividing for you. In the example below, we use
np.average() to calculate the average of a dataset with ten values.
example_array = np.array([24, 16, 30, 10, 12, 28, 38, 2, 4, 36]) example_average = np.average(example_array) print(example_average)
The code above calculates the average of
example_array and saves the value to
example_average. The resulting average of this array is
Use NumPy to calculate the average value of the
author_ages array. Save the result to
Does the average age of the authors surprise you? If so, how? Is it older, or younger than you expected?