Mon 11 Apr 2016

## Numpy

### Array

An N-dimensional array.

The array is homogenous; inspect the type with `dtype()`.

row, columns, … are axis.

Number of axis is rank `ndim()`.

`size()` method returns dimensions.

#### Reshape

The `reshape(rows, columns)` command rearranges an array.

`ravel()` flattens an array into one dimension.

`resize(rows, columns)` works like reshape, but in place.

A dimension specified as -1 is automatically calculated.

#### Concatenation

`concatenate()` is the general form.

`vstack()` and `hstack()` work on particular axis.

`column_stack()` is a specialization of `vstack()` for making many 1D arrays into columns of a 2D array.

#### Splitting

`array_split()` is the general form.

`hsplit()`, `vsplit()` work on particular axis

#### Construction

Create 1D using `array([1, 2, 3])`.

Create 2D using `array([[1, 2], [3, 4]])`.

Or `zeros(num_rows, num_columns)`. `random.randn(num_rows, num_columns)` produces a random array of a given shape.

`fromfunction(f, shape)` calls a function repeatedly as f(x, y, z, …):

• shape is a tuple of integers determining the dimensions
• if f returns something other than a scalar, that will interact with shape to make some other weird shape

`arange(min, max, step)` returns an array range. `linspace(min, max, num_elements)` does the same.

`eye()` makes an identity array (1s on the diagonal, zeroes elsewhere).

#### View

The `view()` method makes a copy. Slicing (by index) triggers this.

`copy()` is a deep copy.

### Traversal

When iterating over arrays, we start with the first axis: `for row in arr:`

However, you can iterate over cells: `for cell in arr.flat:`

### Indexing

It's pretty much just like Python?

But you can also index by boolean arrays (on any particular dimension).

### Functions

A lot of functions are implemented as methods on the array object.

For aggregating functions, you can use the axis argument to control what dimension they aggregate over.