NumPy is founded around its multidimensional array object, numpy.ndarray
. NumPy arrays are a collection of elements of the same data type; this fundamental restriction allows NumPy to pack the data in an efficient way. By storing the data in this way NumPy can handle arithmetic and mathematical operations at high speed.
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 the array creation using an IPython shell as follows:
In [1]: import numpy as np In [2]: a = np.array([0, 1, 2])
Every NumPy array has a data type that can be accessed by the dtype
attribute, as shown in the following code. In the following code example, dtype
is a 64-bit integer:
In [3]: a.dtype Out[3]: dtype('int64')
If we want those numbers to be treated as a float
type of variable, we can either pass the dtype
argument in the np.array
function...