Python Numpy
Numpy is a python library that offers Linear Algebra functionalities. It is a very important library in the Python machine learning stack. This is because all the high-level machine learning and data analysis python libraries use Numpy as their building block. Numpy does all linear algebra operation using a data structure known as Numpy Array.
Numpy Array : numpy.array()
Numpy uses n-dimensional array objects called ndarray a.k.a numpy array, to carry out linear algebra computations.
Numpy array can be formed by passing Python Tuples or Lists to array() function of numpy. This function returns an object of ndarray.
Advantage of Numpy Array
For any computation between python lists, you will have to loop through their elements to perform the operation. This is costly in terms of performance. But numpy array carries out vector operations which is much faster. Due to this great performance, numpy array is used in machine learning algorithm implementation in Python.
Let us see some practical examples of the Numpy Array.
Importing Numpy Library
import numpy as np
Example 1: Creating a 1-D Numpy Array from Python List
#Create a Python List and assign it to a variable
py_list = [1,2,3]
#Create the Numpy Array using list variable
numpy_1D_array = np.array(py_list)
numpy_1D_array
array([1, 2, 3])
#Create Numpy Array without python list variable
numpy_1D_array = np.array([1,2,3])
numpy_1D_array
array([1, 2, 3])
Example 2 – Creating 2-D Numpy Array from Python List
#Create a python list containging two list
py_list_of_lists = [[1,2,3,4],[5,6,7,8]]
#Create the Numpy Array using list variable
numpy_2D_array = np.array(py_list_of_lists)
numpy_2D_array
array([[1, 2, 3, 4], [5, 6, 7, 8]])
#Create Numpy Array without python list variable
numpy_2D_array = np.array([[1,2,3,4],[5,6,7,8]])
numpy_2D_array
array([[1, 2, 3, 4], [5, 6, 7, 8]])
Example 3 – Creating 3-D Numpy Array from Python List
numpy_3D_array = np.array([[[1,2,3,4],[5,6,7,8]] , [[1,2,3,4],[5,6,7,8]], [[1,2,3,4],[5,6,7,8]]])
numpy_3D_array
array([[[1, 2, 3, 4], [5, 6, 7, 8]], [[1, 2, 3, 4], [5, 6, 7, 8]], [[1, 2, 3, 4], [5, 6, 7, 8]]])
Example 4 – Creating 1-D Numpy Array from Python Tuple
#Create a Python Tuple and assign it to a variable
py_tuple = (1,2,3)
#Create the Numpy Array using list variable
numpy_array = np.array(py_tuple)
numpy_array
array([1, 2, 3])
[adrotate banner=”3″]
Data Type : ndarray.dtype
The data type of the numpy array can be determined by using dtype method of ndarray object. Lets us see the two examples below.
numpy_array = np.array([[1, 2, 4], [5, 8, 7]])
numpy_array.dtype
dtype('int32')
numpy_array = np.array([[1.5, 2.7, 4.8], [5.3, 8.1, 7.0]])
numpy_array.dtype
dtype('float64')
Creating Numpy Array with specific Data Type
At times, you would like to explicitly specify the data type of the numpy array. In the first example below, since all elements are integers, the data type on numpy array is int32 by default.
But we can explicitly pass the data type as argument dtype in array() function. This can be seen in the 2nd example.
#Example 1
numpy_array = np.array([[1,2,3,4], [5,6,7,8]])
print("The data type is-", numpy_array.dtype)
numpy_array
The data type is- int32
array([[1, 2, 3, 4], [5, 6, 7, 8]])
#Example 2
numpy_array = np.array([[1,2,3,4], [5,6,7,8]], dtype = 'float64')
print("The data type is-", numpy_array.dtype)
numpy_array
The data type is- float64
array([[1., 2., 3., 4.], [5., 6., 7., 8.]])
Numpy Array Size : ndarray.size
The size of the numpy array can be determined by using the size method of the ndarray object. ndarray. size returns the number of elements inside the numpy array.
Lets us see the examples below.
numpy_1D_array = np.array([1,2,3])
numpy_1D_array.size
3
numpy_2D_array = np.array([[1,2,3,4],[5,6,7,8]])
numpy_2D_array.size
8
numpy_3D_array = np.array([[[1,2,3,4],[5,6,7,8]] , [[1,2,3,4],[5,6,7,8]], [[1,2,3,4],[5,6,7,8]]])
numpy_3D_array.size
24
Numpy Array is Homogeneous
The numpy array is homogeneous in nature, which means that elements of the numpy array are of the same data type.
In our example below, we have silently written the last element as 1.0 and because of this, the data type of numpy array becomes float, even though all other elements were integer.
numpy_array = np.array([[1,2,3,4], [5,6,7,1.0]])
print(numpy_array)
numpy_array.dtype
[[1. 2. 3. 4.] [5. 6. 7. 1.]]
dtype('float64')
Click Here To Download This Interactive Jupyter Notebook