NumPy stands for Numerical Python. It’s an open-source Python library used in many engineering fields, especially data science and Artificial Intelligence (AI). Numpy is also used for working with arrays, which we will be covering in this Python tutorial.
You could use Python lists to work with your numeric data instead, but lists are slower than arrays and less efficient. Using Numpy is up to 100x faster than using lists. Unlike lists, NumPy arrays only store items of the same data type. For this reason, they are stored in contiguous memory locations, making them faster to access.
Read: How to Sort Lists in Python.
Benefits of NumPy Arrays
There are a number of benefits to using NumPy arrays, other than the fact that they are faster than Python lists. For starters, they require less memory to store data than lists do. This means they help optimize your Pythonic code more. Mathematical operations are also easier to perform on NumPy arrays, thanks to the nature of their N-dimensional properties.
One example of this is the fact that numeric and mathematical operators work the same on NumPy arrays as they do in regular mathematical calculations. If you multiply a NumPy array, the values in the array actually get multiplied; this is not the case with regular Python arrays.
Read: Python Math Operators: A Complete Guide.
Defining Arrays in NumPy
In this section, we will discuss how to create and define arrays using NumPy. First, ensure that you have NumPy installed. If you do not, then you can install NumPy by using the code below in your terminal or in Jupyter Notebook:
pip install numpy
You can now import the numpy library in your Jupyter Notebook project (or other Python project) and begin to create arrays. To import numpy, use the following code:
import numpy as np
You can also use a Python file, but using Jupyter Notebook is easier.
To create an array, you’ll need to pass a list to NumPy’s array() method, as shown in the following code:
my_list1= [2, 4, 6, 8] array1 = np.array(my_list) # create array print (array1) # output array elements
The array created (array1) has integer values. To check the datatype of NumPy array elements, developers can use the dtype property, as shown in the following code example:
my_list1.dtype # output for this statement is: dtype('int64')
It is also possible to pass more than one list to the array method. See the example below, which passes two lists to the array() method.
my_list2 = [1,3,5,7] my_list3 = [4,7,3,9] my_lists= [my_list2,my_list3] array2 = np.array(my_lists) print (array2)
Dimensions and Arrays in NumPy
A dimension is a value that defines the number of indexes you need to specify to select an array element. Note: the code in the last example demonstrated a multidimensional array.
The first array (array1) was a one-dimensional array (1D). The second array (array2) was a two-dimensional array (2D). An array with one dimension is called a vector and the one with two dimensions is called a matrix.
You can also have three-dimensional (3D) and so on. Arrays with 3D or more are normally called Tensors.
To get the size of an array along each dimension, Python developers can use the shape property:
The above statement will output 2, 4, meaning that your array is a 2 × 4 matrix. 2 × 4 implies two rows by four columns.
Special Arrays in NumPy
There are some special array types that NumPy offers. You can have an array of ones or even zeros. Use the ones() and zero() methods, respectively, to create these types of special arrays. You’ll also need to provide the number of items for both of these as an argument, as shown in the following example:
Notice that the results printed are floating-point values or floats. This is the default type for numerical values. In case you would like to specify the data type, developers can use the dtype property. Here is how that looks in Python code:
It is also possible to create an empty array. In this sense, it won’t be you to explicitly define what elements your array should contain. Rather, the random content in memory will be used to initialize the array for you.
You can use empty() to declare such an array:
You can also define an array by specifying the range. In this case, you’ll need to put the range (n) in the arrange() method. The array will contain the elements ranging from 0 to (n-1) in linear increments of 1:
At a certain point, you may need to have a custom increment and starting value. Use the syntax below to achieve this:
# the syntax is arange( start, stop, step) arange (3, 27, 4) # result is array([ 3, 7, 11, 15, 19, 23])
Another method you can use to specify linear values for an array within a particular interval is linspace(). Here is some code showing this in use:
np.linspace(10, 25, num = 10) # the result is array([ 10. , 13.75, 17.5, 21.25, 25. ])
Python Lists versus NumPy Arrays
However fast they may be, NumPy arrays are not a one-size-fits-all solution; they aren’t always faster than Python lists. If they were, then the Python community would have already done away with lists.
A good example of where lists are faster than NumPy arrays is when it comes to appending data. The list implementation of appending data is so many times faster than that of NumPy arrays. Actually, algorithm analysis using the big-O notation shows that NumPy’s append method is O(n), while that of lists is O(1).