This is the 5th article in the ‘Data Science with Python’ series. In this article, we will learn about Arrays and the NumPy library of Python.
Table of Contents
- Introduction of NumPy in Python
- NumPy Installation
- NumPy Array
- Numerical Operations in NumPy
- Final Thoughts
Introduction of NumPy in Python
NumPy stands for “Numerical Python”. NumPy is the open-source core library for scientific calculation. This library contains mathematical functions for basic linear algebra functions, Fourier transforms, and advanced random number capabilities for various machine learning requirements. It provides features of the high-performance multidimensional array object. The library consists of two-dimensional array objects and a group of routines for the process of arrays.
No matter how the data is, the first step is to make it analyzable and transform it into arrays.
Data can come from various sources and formats, including documents, images, sound clips, and numerical measurements. Despite this apparent heterogeneity, Arrays help us to think of all data as arrays of numbers.
NumPy is used for :
- Mathematical and logical operations on arrays
- It also has functions that can work in linear algebra, Fourier transform, and matrices domain.
- NumPy is commonly used with packages: SciPy and Matplotlib. Currently, this combination is more widely used as a replacement for MatLab, a well-known technical computing platform.
NumPy was created by Travis Oliphant in 2005.
NumPy Installation
To install and use NumPy, we first need to install a Python environment on our system. To install Python IDE refer to our first article ‘Data Science with Python: Introduction‘ where we have explained this process in detail. After successfully installing IDE, open the command prompt and use pip to install NumPy.
pip install numpy |
Numpy Arrays
Arrays are a pattern of horizontal and vertical lines of data created with the help of a list in Python. Python array provides efficient data storage, and NumPy adds to this efficient operations on data.
Below is the method to create a Numpy array:
- Let’s import NumPy the standard way, under the alias np:
#import numpy as np |
- Creating Arrays from Python Lists: Arrays can be made in 2 different ways.
- First, we’ll use np.array to create arrays from Python lists:
Python Code: # Creating an integer array print(arr_1) print(type(arr_1)) Output: [13, 43, 12, 5, 3] <class ‘numpy.ndarray’> |
2. Creating Arrays from Scratch: It is advised to create arrays from scratch for larger arrays.
Python Code: # Create a length-5 integer array filled with zeros print(arr_2) Output: [0, 0, 0, 0, 0] |
Types of Arrays
Arrays are divided into 3 types – one-dimensional (1D) called vectors, two-dimensional arrays(2D) called matrix, and three-dimensional arrays(3D) called tensors. Let’s discuss them one by one.
- One-Dimensional Array (1D)- Vector
- Two-Dimensional Array (2D)- Matrix
- Three-Dimensional Array (3D)- Tensor
One-Dimensional Array
One-dimensional arrays are called vectors. Let’s see the Python code to create an array.
Python Code: # importing numpy module print(“Numpy Array in python :”,sample_array1) Output: Numpy Array in Python: [11 22 33 44] |
This is called a linear list.
Two-Dimensional Array
Data in multidimensional arrays are stored in tabular form also called as matrix. Let’s see the Python code to create a matrix.
Python Code: # importing numpy module print(“Numpy multi-dimensional array in Python\n”,sample_array11) Output: Numpy multi-dimensional array in Python |
Three-Dimensional Array
The three-dimensional arrays are called tensors. Let’s create a 3D array(tensor) using Python.
Python Code: #3-D array print(“3d tensor \n”, arr_3d) print(“Tensor shape: “,arr_3d.shape) Output: 3d tensor [[6 7 1] |
The above 3-D array is of dimensions 2 rows and 3 columns and depth 2.
Specialized Numpy Arrays
This is a very interesting part of arrays in Python. As the name suggests, these arrays have special features to print the data values.
Random NumPy Array
The numpy. random.rand() function is used to create an array of specified shapes and fill it with random values. The random function is a module that is present in the NumPy library.
This module consists of the functions which are used for generating random numbers. This module contains some simple random data generation methods, some permutation and distribution functions, and random generator functions.
Python Code: arr_3 = np.random.rand(5) #Creating an array with random values Output: 1D Random Array : |
Np.zeros()
The numpy.zeros() function is one of the most important and interesting functions by Python arrays. This function is used to generate an array of only zeros.
The numpy.zeros() function provides us with a new array of given shapes and types, which is filled with zeros.
The 3 examples below will help to understand this function with different additions subsequently.
Example 1: numpy.zeros() without dtype and order
Python Code: # Create a length-8 integer array filled with zeros without giving data type and order print(“Zero’s Array\n”, arr_4) Output: Zero’s Array [0. 0. 0. 0. 0. 0. 0. 0.] |
Example 2: numpy.zeros() without order
Python Code: # Create a length-8 integer array filled with zeros without giving an order print(“Zero’s Array\n”, arr_5) Output: Zero’s Array [0 0 0 0 0 0 0 0] |
Example 3: numpy.zeros() with shape
Python Code: # Create a length-5 integer array filled with a defined shape print(“Zero’s Matrix\n”, arr_6) Output: Zero’s Matrix [[0. 0.] |
The matrix is created with 3 rows and 2 columns.
Np.ones():
The numpy.ones() is a function in Python that returns a new array of given shapes and types, with ones. It is exactly similar to np.zeros() functions, the only difference is that it prints an array of ones.
Python Code: a = np.ones(2, dtype = int) print(“Matrix a : \n”, a)b = np.ones([2, 2], dtype = int) print(“\nMatrix b : \n”, b)c = np.ones([3, 3]) print(“\nMatrix c : \n”, c)Output:Matrix a : [1 1]Matrix b : [[1 1] [1 1]]Matrix c : [[1. 1. 1.] [1. 1. 1.] [1. 1. 1.]] |
Np.eye():
The eye() function of the NumPy array is used to create a 2-D array with ones in the diagonal and zeros in the rest.
Python Code: arr_7=np.eye(2) Output: Identity Matrix [[1. 0.] |
An identity matrix of 2×2 is created.
Numerical Operations in NumPy
In Machine Learning mathematics, a 1-D array is called a vector. A 2-D array is called a matrix and an n-dim array i.e. 3-d, 4-d so no is called a tensor.
Basic Array Operations
Let’s try some basic array operations with numpy.
Accessing the Array Index
Indexing in a numpy array starts from 0. For example, if we want to access the 3rd row then 2 will be the index. Let’s understand with the help of an example.
Python Code: arr_8=np.random.rand(3,4) print(“\n Accessing an element of the matrix: “,arr_8[2,3]) # print 3rd row 4th element of matrix Output: Random Matrix Accessing an element of the matrix: 0.53386599 |
Matrix Slicing
Slice a matrix to get a part of it using Python code.
Python Code: sliced_arr = arr_8[:2, :2] print (“Array with first 2 rows and first two columns:\n”, sliced_arr)sliced2_arr = arr_8[:2,::2] print (“Array with first 2 rows and alternate columns(0 and 2):\n”, sliced2_arr) Output: Array with first 2 rows and first two columns: Array with first 2 rows and alternate columns(0 and 2): |
Math Operations in Numpy
Various math operations can be performed on a Numpy array. There are many built-in functions for that in Numpy as well. Let’s see some examples:
Elementwise Addition
Python Code: x = np.array([[1,2],[3,4]], dtype=np.float64) print(“The elementwise sum of the matrix \n”,np.add(x, y)) #Elementwise sum Output: The elementwise sum of the matrix |
Elementwise Subtraction
Python Code: print(“The elementwise subtraction of the matrix \n”,np.subtract(x, y)) # Elementwise difference of x and y Output: The elementwise subtraction of the matrix |
Elementwise Multiplication
Python Code: print(“The elementwise product of the matrix \n”,np.multiply(x, y)) # Elementwise product Output: The elementwise multiplication of the matrix |
Elementwise Division
Python Code: print(“The elementwise division of the matrix \n”,np.divide(x, y)) #Elementwise division Output: The elementwise division of the matrix |
Elementwise Squareroot
Python Code: print(“The elementwise square root of the matrix \n”,np.sqrt(x)) # Elementwise square root Output: The elementwise sum of the matrix |
Matrix Multiplication
Python Code: x = np.array([[1,2],[3,4]], dtype=np.float64) print(“The matrix multipication of two matrix \n”,np.dot(x, y)) # Matrix multiplication Output: The matrix multiplication of two matrix |
The matrix multiplication of 2×2 and 2×3 matrices results in a matrix of dimension 2×3 as shown above.
Matrix Transpose
Python Code: print(“The transpose of matrix ‘y’ is \n”,np.transpose(y)) # Matrix Transpose Output: The transpose of matrix ‘y’ is |
After the transpose of a matrix, a 2×3 matrix will have a 3×2 dimension, as shown above.
We have studied various matrix operations in Foundations of Data Science. We have successfully run all these operations using numpy.
End Note
Numpy is an important library for learning the basics of Data Science. In this article, we tried to explain every point in depth with proper Python code examples. Try these codes by yourself and practice various NumPy operations.
Stay Tuned!!
Learn the basics of Data Science with our Data Science with Python series.
Keep learning and keep implementing!!
Good Article
Great summary