Numpy: Multi-dimensional Arrays and Matrices

NumPy is the fundamental package for scientific computing with Python. NumPy stands for Numerical Python. It consists of multidimensional array objects and the collection of functions for processing those arrays.

Using NumPy, mathematical and logical operations on arrays can be performed. That is why it is beneficial in Data Science and Machine Learning.

What is NumPy?

NumPy enriches Python’s programming language with dominant data structures, implementing multidimensional arrays and matrices. These data structures guarantee efficient calculations with matrices and arrays.

Install NumPy

If you have installed software like Anaconda, then you have already installed the NumPy library.

 Although, a lightweight alternative is to install NumPy using a popular Python package installer, pip.

Go to your terminal and run the following command.

pip3 install numpy

If that gives you permission or IO errors, try using sudo.

sudo pip3 install numpy

Now, you have successfully installed the NumPy package.

NumPy is often used with packages like SciPy (Scientific Python) and Matplotlib (plotting library). This common combination is widely used as the replacement for MatLab, the popular platform for technical computing. 

However, Python’s alternative to MatLab is now seen as a more modern and complete programming language.

NumPy – Ndarray Object

The most important object defined in NumPy is the N-dimensional array type called ndarray

It describes the collection of elements of the same type. Items in the collection can be accessed using the zero-based index. 

Any item extracted from the ndarray object (by slicing) is represented by a Python object of one of the array scalar types. See the below example of Ndarray.

# app.py

import numpy as np 
data = np.array([21,22,23]) 
print(data)

The output of the above example is below.

NumPy - Ndarray Object

Shape of Array

You can check the shape of the array with the object shape preceded by the array’s name. In the same way, you can check the type with dtypes.

# app.py

import numpy as np 
data = np.array([21,22,23]) 
print(data.shape)
print(data.dtype)

In the above example, we are printing the shape of an array and the dtype of an array.

Let’s see the output below.

Python NumPy Tutorial | Getting Started With NumPy

Two Dimension Array in NumPy

You can add more dimensions with a “, “coma, which has to be within the bracket [].

# app.py

import numpy as np 
data = np.array([
                (1, 2, 3), 
                (4, 5, 6), 
            ])
print(data.shape)
print(data.dtype)

In the above example, we have taken three rows and two columns. So the output looks like this.

Output

Two Dimension Array in NumPy

Python NumPy Array v/s List

We use a Python numpy array instead of a list for the three reasons below.

  1. Less Memory
  2. Fast
  3. Convenient

Python NumPy Operations

Let’s see one by one operation.

ndim

You can find an array’s dimension, whether a two-dimensional array or a single-dimensional one. See the below example.

# app.py

import numpy as np 
data = np.array([
                (1, 2, 3), 
                (4, 5, 6), 
            ])
print(data.ndim)

See the output.

Python NumPy Operations

itemsize

We can calculate the byte size of each element using itemsize. 

See the below example.

# app.py

import numpy as np 
data = np.array([
                (1, 2, 3), 
                (4, 5, 6), 
            ])
print(data.itemsize)

dtype

You can find a data type of the elements stored in the array.

So, if you want to know the data type of the particular item, you can use the ‘dtype’ function, which will print the data type along with the size.

# app.py

import numpy as np 
data = np.array([
                (1, 2, 3), 
                (4, 5, 6), 
            ])
print(data.dtype)

Output

dtype in NumPy

reshape

We can use the reshape function when we need to change the number of rows and columns, which gives a new view of an object.

# app.py

import numpy as np 
data = np.array([
                (1, 2, 3), 
                (4, 5, 6), 
            ])
print('before reshaped: ',data)
resphaped_data = data.reshape(3, 2)
print('after reshaped: ',resphaped_data)

See the below output.

reshape in NumPy

There are lots of other operations that you can perform using NumPy.

That’s it for this tutorial.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.