# Introduction to NumPy and NumPy Arrays

November 24, 2020 2020-12-03 21:18## Introduction to NumPy and NumPy Arrays

This chapter is an introduction to NumPy which is an open-source library for performing numerical computation using Python. It is used for working with arrays and performing array operations.

NumPy is used in various domains such as Linear Algebra, Fourier Transforms, etc. In fact, NumPy stands for Numerical Python. In Data Science, NumPy is highly used for manipulating numerical data (example: pixel arrays of an image).

**Install**ing NumPy using pip

NumPy can be installed using the python package manager pip by simply running the following command:

$ pip install numpy

It is a general convention to import NumPy as np in our python code. The following block of code illustrates how to import NumPy and check its version:

# Library import conventions import numpy as np #Check version of numpy installed print(np.__version__) # Printed version:1.18.1

#### Introduction to NumPy Arrays

A **NumPy array** is a grid of values, similar to a Python List, where all data are of the same type (integer, float). If you are familiar with Linear Algebra, a NumPy array can simply be thought of as a matrix having homogenous values. The following examples illustrate what NumPy Arrays looks like:

- $latex \left[[ 1, \ \ 2]\right]$
- $latex \left[[ 1], \ \ [ 2]\right]$
- $latex \left[[ 1, \ \ 2] ,\ [ 3, \ \ 4]\right]$

A NumPy array can simply be created from a Python list by passing the list into the numpy.array() function as:

# Importing numpy import numpy as np # Defining a python list l = [1, 2, 3, 4] # Creating a numpy array from the list arr = np.array(l) # Printing the array print(arr)

[1 2 3 4]

Some of the main key features of a NumPy array are: `shape`

, `dimension`

and `size`

.

#### Shape of a NumPy Array

The shape of a NumPy array is a Python Tuple that denotes the number of elements in each axis of the array.

For example, the array [1, 2, 3, 4] has only one axis with 4 elements. Thus, the shape of this array would be the tuple (4,). Similarly, the array [[ 1], [ 2]] has two axes, one horizontal and one vertical. Thus the shape of this array would be the tuple (2, 1).

The shape() function of NumPy can be used to check the shape of arrays:

# Importing numpy import numpy as np # Defining numpy arrays from python lists arr1 = np.array([1, 2, 3, 4]) arr2 = np.array([[1], [2]]) # Printing the shape of numpy arrays print("Shape of arr1: ", arr1.shape) print("Shape of arr2: ", arr2.shape)

Shape of arr1: (4,) Shape of arr2: (2, 1)

#### Dimension of a NumPy Array

In NumPy, the number of Axes in an array is called the dimension of the array. A multi-dimensional NumPy array is also referred to as an n-d array, a shorthand name for N-dimensional array.

For example, the array [1, 2, 3, 4, 5] has only one axis, so it is a 1-D array. Likewise, the array[[ 1], [ 2]] has two axes, thus it is a 2-D array.

The ndim() function of NumPy can be used to check the dimension of arrays:

# Importing numpy import numpy as np # Defining numpy arrays from python lists arr1 = np.array([1, 2, 3, 4]) arr2 = np.array([[1], [2]]) # Printing the dimension of numpy arrays print("Dimension of arr1: ", arr1.ndim) print("Dimension of arr2: ", arr2.ndim)

Dimension of arr1: 1 Dimension of arr2: 2

#### Size of a NumPy Array

Size of a NumPy array is simply the number of elements in the array. For example, the array [1, 2, 3, 4] has 4 elements, so its size is 4.

The size() function can be used to get the size of an array:

# Importing numpy import numpy as np # Defining numpy arrays from python lists arr1 = np.array([1, 2, 3, 4]) arr2 = np.array([[1], [2]]) # Printing the size of numpy arrays print("Size of arr1: ", arr1.size) print("Size of arr2: ", arr2.size)

Size of arr1: 4 Size of arr2: 2

There you have it! You have installed NumPy on your machine and also learned about the fundamentals of NumPy arrays.

Head onto the next chapter on ‘Creating Numpy Arrays‘ which discusses different ways of creating NumPy arrays.