array([ 0.8967576 , 0.99196818, 0.6687194 ])
The way the axis is specified here can be confusing to users coming from other languages.
The axis
keyword specifies the dimension of the array that will be collapsed, rather than the dimension that will be returned.
So specifying axis=0
means that the first axis will be collapsed: for two-dimensional arrays, this means that values within each column will be aggregated.
Other aggregation functions
NumPy provides many other aggregation functions, but we won't discuss them in detail here.
Additionally, most aggregates have a
NaN
-safe counterpart that computes the result while ignoring missing values, which are marked by the special IEEE floating-point
NaN
value (for a fuller discussion of missing data, see
Handling Missing Data).
Some of these
NaN
-safe functions were not added until NumPy 1.8, so they will not be available in older NumPy versions.
The following table provides a list of useful aggregation functions available in NumPy:
Function Name | NaN-safe Version | Description |
---|
np.sum | np.nansum | Compute sum of elements |
np.prod | np.nanprod | Compute product of elements |
np.mean | np.nanmean | Compute mean of elements |
np.std | np.nanstd | Compute standard deviation |
np.var | np.nanvar | Compute variance |
np.min | np.nanmin | Find minimum value |
np.max | np.nanmax | Find maximum value |
np.argmin | np.nanargmin | Find index of minimum value |
np.argmax | np.nanargmax | Find index of maximum value |
np.median | np.nanmedian | Compute median of elements |
np.percentile | np.nanpercentile | Compute rank-based statistics of elements |
np.any | N/A | Evaluate whether any elements are true |
np.all | N/A | Evaluate whether all elements are true |
We will see these aggregates often throughout the rest of the book.
Example: What is the Average Height of US Presidents?
Aggregates available in NumPy can be extremely useful for summarizing a set of values.
As a simple example, let's consider the heights of all US presidents.
This data is available in the file president_heights.csv, which is a simple comma-separated list of labels and values: