NumPy arrays
NumPy's main object is a homogeneous multidimensional array. An array is essentially a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. The index in NumPy arrays is zero-based, so the first element is the 0th element; the second element is the 1st element, and so on. In NumPy, dimensions are called axes and the number of axes, or dimensions, is called the rank or dimension of the array. To import NumPy into our Jupyter Notebook, we use the numpy as np
convention import.
Creating arrays in NumPy
There are the following two methods to create arrays in Python:
- Creating arrays from lists
- Using the built-in functions that NumPy provides
Creating arrays from lists
To create a NumPy array from a list, we use the np.array
function. The lists that we saw in our earlier examples, such as distances
, times
, product_quantities
, and prices
will be transformed into arrays using the np.array
function. To do so, we can run the following lines of...