Python Numpy Array – A Gentle Introduction to beginners

Numpy - Introduction to Numpy Array

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.

Ad
Deep Learning Specialization on Coursera

Importing Numpy Library

In [1]:
import numpy as np

Example 1: Creating a 1-D Numpy Array from Python List

In [2]:
#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
Out[2]:
array([1, 2, 3])
In [3]:
#Create Numpy Array without python list variable
numpy_1D_array = np.array([1,2,3])
numpy_1D_array
Out[3]:
array([1, 2, 3])

Example 2 – Creating 2-D Numpy Array from Python List

In [4]:
#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
Out[4]:
array([[1, 2, 3, 4],
       [5, 6, 7, 8]])
In [5]:
#Create Numpy Array without python list variable
numpy_2D_array = np.array([[1,2,3,4],[5,6,7,8]])
numpy_2D_array
Out[5]:
array([[1, 2, 3, 4],
       [5, 6, 7, 8]])

Example 3 – Creating 3-D Numpy Array from Python List

In [6]:
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
Out[6]:
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

In [7]:
#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
Out[7]:
array([1, 2, 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.

In [8]:
numpy_array = np.array([[1, 2, 4], [5, 8, 7]])
numpy_array.dtype
Out[8]:
dtype('int32')
In [9]:
numpy_array = np.array([[1.5, 2.7, 4.8], [5.3, 8.1, 7.0]])
numpy_array.dtype
Out[9]:
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.

In [10]:
#Example 1
numpy_array = np.array([[1,2,3,4], [5,6,7,8]])

print("The data type is-", numpy_array.dtype)
numpy_array
Out[10]:
The data type is- int32
array([[1, 2, 3, 4],
       [5, 6, 7, 8]])
In [11]:
#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
Out[11]:
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.

In [12]:
numpy_1D_array = np.array([1,2,3])
numpy_1D_array.size
Out[12]:
3
In [13]:
numpy_2D_array = np.array([[1,2,3,4],[5,6,7,8]])
numpy_2D_array.size
Out[13]:
8
In [14]:
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
Out[14]:
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.

In [15]:
numpy_array = np.array([[1,2,3,4], [5,6,7,1.0]])
print(numpy_array)
numpy_array.dtype
Out[15]:
[[1. 2. 3. 4.]
 [5. 6. 7. 1.]]
dtype('float64')

 

Click Here To Download This Interactive Jupyter Notebook

 

Like and Comment section (Community Members)

Create Your ML Profile!

Don't miss out to join exclusive Machine Learning community

Comments

No comments yet