Getting started with NumPy
The NumPy library revolves around its multidimensional array object, numpy.ndarray
. NumPy arrays are collections of elements of the same data type; this fundamental restriction allows NumPy to pack the data in a way that allows for high-performance mathematical operations.
Creating arrays
You can create NumPy arrays using the numpy.array
function. It takes a list-like object (or another array) as input and, optionally, a string expressing its data type. You can interactively test array creation using an IPython shell, as follows:
import numpy as np a = np.array([0, 1, 2])
Every NumPy array has an associated data type that can be accessed using the dtype
attribute. If we inspect the a
array, we find that its dtype
is int64
, which stands for 64-bit integer:
a.dtype # Result: # dtype('int64')
We may decide to convert those integer numbers to float
type. To do this, we can either pass the dtype
argument at array initialization or cast the array...