Introduction to NumPy and its array manipulation capabilities

NumPy is a powerful library in Python for performing numerical computations and array manipulation. It stands for 'Numerical Python' and provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays efficiently.

In this article, we will explore the basics of NumPy and understand its array manipulation capabilities.

Installing NumPy

Before we dive into NumPy, let's make sure it is installed in our Python environment. Open your terminal and run the following command:

pip install numpy

Importing NumPy

Once NumPy is installed, we can import it into our Python script using the following line of code:

import numpy as np

It is a convention to import NumPy as "np" for ease of use.

NumPy Arrays

The core functionality of NumPy revolves around its array object called ndarray. An ndarray is a multi-dimensional container for homogeneous data, meaning all elements in the array have the same data type. It can be created in various ways, such as from a Python list, using functions like zeros() and ones(), or by reading data from a file.

Here is an example of creating a simple NumPy array:

import numpy as np

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

Output: [1, 2, 3, 4, 5]

You can access individual elements of the array using square brackets, just like regular Python lists. NumPy arrays also support advanced indexing techniques such as slicing and masking.

NumPy arrays can be easily manipulated and perform operations efficiently on large data sets due to their underlying C implementation. Some of the commonly used array manipulation capabilities provided by NumPy include:

1. Shape Manipulation

NumPy arrays have a property called shape that indicates the dimensions of the array, such as the number of rows and columns. The shape of an array can be changed using various methods like reshape() and resize().

import numpy as np

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

Output: (2, 3)

2. Array Arithmetic Operations

NumPy allows performing arithmetic operations on arrays, such as addition, subtraction, multiplication, and division. These operations can be performed element-wise or using broadcasting.

import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

print(arr1 + arr2)
print(arr1 * arr2)

Output: [5, 7, 9] [4, 10, 18]

3. Array Aggregations

NumPy provides built-in functions to perform aggregations and statistical calculations on arrays, such as mean, sum, minimum, maximum, etc.

import numpy as np

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


Output: 3.0 15 1 5

4. Array Concatenation and Splitting

NumPy allows concatenating multiple arrays together using functions like concatenate() and hstack(). Similarly, arrays can be split into smaller arrays using functions like split() and vsplit().

import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

print(np.concatenate((arr1, arr2)))
print(np.vstack((arr1, arr2)))

Output: [1, 2, 3, 4, 5, 6] [[1, 2, 3] [4, 5, 6]]

These are just a few of the many array manipulation capabilities provided by NumPy. The library is extensively used in various domains such as data science, machine learning, and scientific computing due to its efficiency and flexibility.


NumPy is a fundamental library that forms the backbone of many scientific and numerical computations in Python. It provides a powerful array object and various array manipulation capabilities for efficient calculations and data processing. Understanding the basics of NumPy, such as creating arrays and performing array operations, is essential for any data scientist or Python developer.

noob to master © copyleft