# NumPy: Mathematical & Statistical Methods¶

Ha Khanh Nguyen (hknguyen)

## 1. Calculation Basic Statistics¶

### 1.1 2D Arrays¶

• Compute the sample mean:
• Compute the sum of all the elements:

• Functions like mean and sum take an optional axis argument that computes the statistic over the given axis, resulting in an array with one fewer dimension:

• General terminology:
• across the columns = for each row (axis=1)
• down the rows = for each column (axis=0)

### 1.2 3D Arrays¶

• We have the following three-dimensional array:

• The axes in 3-d array are numbered as followed:

### Exercises¶

• Compute the sum of each column in each layer

• Compute the smallest element in each layer

## 2. Sorting¶

• Like Python’s built-in list type, NumPy arrays can be sorted in-place with the sort method:
• You can sort each one-dimensional section of values in a multidimensional array inplace along an axis by passing the axis number to sort:

This lecture note is modified from Chapter 4 of Wes McKinney's Python for Data Analysis 2nd Ed.