A Comprehensive guide; simple data manipulation with NumPy

Abubakar Labaran Salisu
5 min readMar 17, 2023
NumPy

If you’re new to programing and want to learn how to work with numerical data in python, NumPy is an essential library to master.

Introduction

NumPy stands for ‘Numerical Python’. It is a package for data analysis and scientific computing with Python. NumPy uses a multidimensional array object, and has functions and tools for working with these arrays. The powerful n-dimensional array in NumPy speeds-up data processing. NumPy can be easily interfaced with other Python packages and provides tools for integrating with other programming languages like C, C++ etc

What is NumPy?

NumPy is a library for the python programing language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.

Pre-requisites

The only things that you need for installing NumPy (on windows) are:

1. Python

2. Pip or Conda (depending upon user preference)

Installing NumPy

For Conda users:

If you want the installation to be done through Conda, you can use the below command.

conda install –c anaconda numpy

For PIP users:

Users who prefer to use pip can use the below command to install NumPy.

pip install numpy

Now that you have installed NumPy successfully in your system.

How to import NumPy

To access NumPy and it functions import it in your python code like this.

Import numpy as np

We shorten the imported name to np for better readability of code using NumPy.

Arrays

An array is a data type used to store multiple values using a single identifier (variable name). An array contains an ordered collection of data elements where each element is of the same type and can be referenced by its index (position).

NumPy Arrays

NumPy arrays are used to store lists of numerical data, vectors and matrices. The NumPy library has a large set of routines (built-in functions) for creating, manipulating, and transforming NumPy arrays. Python language also has an array data structure, but it is not as versatile, efficient and useful as the NumPy array. The NumPy array is officially called Nd array but commonly known as array.

Difference Between List and Array

Below in tabular form are the differences between List and Array

Create a NumPy ndarray

NumPy is used to work with arrays. The array object in NumPy is called ndarray. We can create a NumPy ndarray object by using the array() function.

import numpy as np

arr = np.array([1, 2, 3, 4, 5])

print(arr)

print(type(arr))

NumPy Array Indexing

Array indexing is the same as accessing an array element. You can access an array element by referring to its index number. The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.

import numpy as np

arr = np.array([1, 2, 3, 4])

print(arr[0])

NumPy Array Slicing

Slicing in python means taking elements from one given index to another given index. We pass slice instead of index like this: [start:end]. We can also define the step, like this: [start:end:step]. If we don’t pass start its considered 0, If we don’t pass end its considered length of array in that dimension, If we don’t pass step its considered 1.

import numpy as np

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

print(arr[1:5])

NumPy Arithmetic Operations

Input arrays for performing arithmetic operations such as add(), subtract(), multiply(), and divide() must be either of the same shape or should conform to array broadcasting rules.

import numpy as np 
a = np.arange(9, dtype = np.float_).reshape(3,3)

print 'First array:'
print a
print '\n'

print 'Second array:'
b = np.array([10,10,10])
print b

print ‘\n’ Input arrays for performing arithmetic operations such as add(), subtract(), multiply(), and divide() must be either of the same shape or should conform to array broadcasting rules.

print 'Add the two arrays:' 
print np.add(a,b)
print '\n'

print 'Subtract the two arrays:'
print np.subtract(a,b)
print '\n'

print 'Multiply the two arrays:'
print np.multiply(a,b)
print '\n'

print 'Divide the two arrays:'
print np.divide(a,b)

NumPy Linear Algebra

NumPy provides a powerful module called numpy.linalg that allows you to perform linear algebra operations on arrays. This module includes functions for matrix operations, determinants, inverses, eigenvalues and eigenvectors, and more.

work with NumPy’s linear algebra capabilities, you’ll need to create NumPy arrays. NumPy arrays are similar to Python lists, but they can have multiple dimensions and are optimized for numerical operations.

The Linear Algebra module of NumPy offers various methods to apply linear algebra on any numpy array.
One can find:

  • rank, determinant, trace, etc. of an array.
  • eigen values of matrices
  • matrix and vector products (dot, inner, outer,etc. product), matrix exponentiation
  • solve linear or tensor equations and much more!
import numpy as np

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

print("Rank of A:", np.linalg.matrix_rank(A))

print("\nTrace of A:", np.trace(A))

print("\nDeterminant of A:", np.linalg.det(A))

print("\nInverse of A:\n", np.linalg.inv(A))

print("\nMatrix A raised to power 3:\n",
np.linalg.matrix_power(A, 3))

Let's see the Output:

Rank of A: 3

Trace of A: 11

Determinant of A: -306.0

Inverse of A:
[[ 0.17647059 -0.00326797 -0.02287582]
[ 0.05882353 -0.13071895 0.08496732]
[-0.11764706 0.1503268 0.05228758]]

Matrix A raised to power 3:
[[336 162 228]
[406 162 469]
[698 702 905]]

Conclusion

Learning the basics of NumPy is essential for any beginner in the field of data science and machine learning. With its powerful array manipulation capabilities and easy-to-use functions, NumPy has become a go-to library for numerical operations in Python. In this article, we have provided a beginner’s guide to NumPy, covering its key features, such as array creation, indexing, slicing, and NumPy Arithmetic Operations. By mastering these fundamental concepts, readers can begin to explore more complex operations and applications of NumPy in their data analysis projects. As with any new skill, practice is key, so we encourage readers to experiment with NumPy and continue their learning journey through online resources and tutorials. With these tools at their disposal, beginners can become proficient in NumPy and advance their data science skills.

--

--

Abubakar Labaran Salisu

Undergraduate student in faculty of science, Aliko Dangote university of science and technology, II Creative Designer II Fellow at Arewa Data Science Academy