NumPy Tutorial | Basic of NumPy Arrays

NumPy is a library in Python that is used for working with arrays. The NumPy stands for ‘Numerical Python’ which makes it pretty much clear that it is designed to deal with numerical data in Python. It is homogenous, which means can be used to store a single data type. Learn Data Types in Python

Why NumPy when we already have Lists?

In Python, we have lists that can serve the needs of dealing with numerical data, storing and manipulating it. Then why NumPy?

While dealing with a tremendous amount of numerical data (of the order of M), Lists prove slower in performance as compared to a NumPy array. In fact, a NumPy array is almost 50x faster than the Python List.

The reason for a NumPy array to be comparatively faster is that all the data of the array is stored at one continuous place in memory, unlike Lists whose data are stored in memory at different positions. So accessing and manipulating the data of a NumPy array is comparatively faster than that of a List.

Also, the NumPy array is designed in a way to work more efficiently with the latest CPU architectures.

  • It is 50x faster than the list
  • It consumes less memory
  • The NumPy array is known as ndarray

NumPy Array vs List

NumPy ArraysLists
Can only store one data type (Homogenous)Can store multiple data types (Heterogenous)
Consumes less memory and hence fasterComparatively consumes more memory and hence slow
Comparatively less easy to modifyEasier to modify

Working with NumPy

Installing Numpy

If you have Python installed in your local system, you can simply install NumPy through the terminal by using the code:

pip install numpy

To install NumPy on Notebooks like Jupyter or Google Collab, use the code:

!pip install numpy

Check the NumPy version

Once, NumPy is installed in your environment, you can check its version:

np.__version__      #-> Google Colab
numpy --version     #-> Local

Importing NumPy into the project

It’s important to import the NumPy module to start using it using the code:

import numpy as np

Creating a NumPy array and showing it

1-Dimensional NumPy array:

arr_one = np.array([1,2,3,4,5,6])
print(arr_one)
Output: [1 2 3 4 5 6]

Checking the dimension:

arr_one = np.array([1,2,3,4,5,6])
print(arr_one.ndim)
Output: 1

2-Dimensional NumPy array:

arr_two = np.array(
                    [
                      [1,2,3,4],
                      [5,6,7,8]
                     ]
                   )
print(arr_two)
Output: [[1 2 3 4]
[5 6 7 8]]

Checking the dimension:

arr_two = np.array(
                    [
                      [1,2,3,4],
                      [5,6,7,8]
                     ]
                   )

print(arr_two.ndim)
2

3-Dimensional NumPy array:

arr_three = np.array(
                    [[
                      [1,2,3,4],
                      [5,6,7,8]
                     ],
                   [
                      [1,2,3,4],
                      [5,6,7,8]
                     ]]
                   )

print(arr_three)
[[[1 2 3 4]
[5 6 7 8]]

[[1 2 3 4]
[5 6 7 8]]]

Checking the dimension:

arr_three = np.array(
                    [[
                      [1,2,3,4],
                      [5,6,7,8]
                     ],
                   [
                      [1,2,3,4],
                      [5,6,7,8]
                     ]]
                   )
print(arr_three.ndim)
Output: 3

Finding No. of rows, No. of Columns and No. of Elements

We can find the number of Rows and Columns of a NumPy array. Similarly, we can find the Size and Length of the array.

b = np.array([[1,2,3,4,5],
              [6,7,8,9,10]])

b.shape
b.size
len(b)
Output: (2,5)
10
2

Accessing the elements

We can access the whole element of the NumPy array or a particular element. Whether it’s a 1-D array, a 2-D array, or a 3-D array, the process is similar. Let’s see different ways of doing this in the same piece of code:

# 1-D array
arr_one = np.array([1,2,3,4,5])
arr_one
arr_one[:]
arr_one[3]
Output: array([1, 2, 3, 4, 5])
array([1, 2, 3, 4, 5])
4
# 2-D array
arr_two = np.array([[1,2,3,4],
                    [5,6,7,8]])
arr_two
arr_two[1,2]
arr_two[0,0:3]
Output: array([[1, 2, 3, 4],
[5, 6, 7, 8]])

7
array([1, 2, 3])

Operations with NumPy array

We can do several mathematical operations with a NumPy array like addition, subtraction, multiplication, division, etc.

arr = np.array([[1,2,3,4],
               [5,6,7,8]])
arr + 10
arr - 10
arr * 10
arr / 10
Output: array([[33, 33, 33, 14],
[15, 16, 80, 18]])

array([[13, 13, 13, -6],
[-5, -4, 60, -2]])

array([[230, 230, 230, 40],
[ 50, 60, 700, 80]])

array([[2.3, 2.3, 2.3, 0.4],
[0.5, 0.6, 7. , 0.8]])

Reshaping a NumPy array

We can convert the dimension of a NumPy array from 1-D to 2-D to 3-D or vice versa. This feature is quite important when dealing with Machine Learning programming.

a = np.array([1,2,3,4,5,6,7,8,9,10,11,12])
# This 1-D array

# Converting from 1-d to 2-D
a.reshape(2,6)

# Converting from 1D- to 3-D
a.reshape(3,2,2)
Output: array([[ 1, 2, 3, 4, 5, 6],
[ 7, 8, 9, 10, 11, 12]])

array([[[ 1, 2],
[ 3, 4]],
[[ 5, 6],
[ 7, 8]],
[[ 9, 10],
[11, 12]]])

Iterating the elements of a NumPy array

a = np.array([1,2,3,4,5,6])
for i in a:
  print(i)
Output: 1 2 3 4 5 6

Leave a Comment

Your email address will not be published. Required fields are marked *