NumPy in Python: A Comprehensive Guide (Easy)

posted 11 min read

Numpy provides effective array management and mathematical operations, making it essential for scientific computing and data processing. It offers a strong basis for numerical activities, making calculations faster and data handling more effective.
As the primary data structure, arrays form the foundation of NumPy. They make it possible to store and manipulate huge datasets effectively, which makes numerical calculations and data analysis activities easier.

NumPy arrays are faster, use less memory, and have more mathematical functionality than Python lists Scientific computing and data analysis depend on them because they provide faster calculation, use less memory, and support multidimensional data.

In this article, we'll explore why NumPy stands as a fundamental library in Python's scientific computing landscape. NumPy's efficiency and simplicity make it indispensable for numerical tasks. Let's delve into its key advantages.

Table of Contents: 

Installing NumPy

You can install NumPy by running the following commands in your terminal:

1. Installing with pip

pip install numpy

2. Installing with Anaconda

When using the Anaconda distribution, NumPy is automatically included. You may use conda to update it:

conda install numpy

After installation, you can see its version with the following command:

import numpy as np
Tip: Ensure NumPy version compatibility when using online code snippets due to potential syntax or function differences.

NumPy Arrays

The foundational data structures in the NumPy library for Python are called numpy arrays, or numpy.ndarray. Their representation of multi-dimensional arrays of identical components makes them an effective tool for data management and numerical computation.
You can carry out vectorized operations, indexing, slicing, and broadcasting using NumPy arrays, allowing for quick and clear code for an array of numerical applications.

1. Creating Arrays

There are several ways to initialise arrays with varied shapes and initial values when creating NumPy arrays. Here are a few typical methods for constructing NumPy arrays:

A. np.array()

You can create an array from a Python list using np.array():

import numpy as np
my_list = [1, 2, 3, 4, 5]
my_array = np.array(my_list)

B. np.zeros()

You can create an array filled with zeros:

import numpy as np
# Create a 3x3 array filled with zeros
zeros_array = np.zeros((3, 3))

C. np.arange()

You can create an array with a range of values using np.arange():

import numpy as np
# Start at 0, stop at 10 (exclusive), step by 2
range_array = np.arange(0, 10, 2)

D. np.linspace()

You can create an array with a specified number of evenly spaced values using np.linspace():

import numpy as np
# Start at 0, end at 5 (inclusive), with 10 points
linspace_array = np.linspace(0, 5, 10)

2. Array Dimension and Shapes

Complex datasets can be efficiently stored in NumPy thanks to arrays' multidimensional data representation capabilities. Axes are terms commonly used to describe the idea of dimensions. 

import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.shape)  # Output: (2, 3) - 2 rows, 3 columns

3. Accessing and Manipulating Array Elements

A NumPy array's individual items or subsets of elements can be accessed and modified using techniques like indexing, slicing, and altering, which provide exact control and manipulation of data.

import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
# Accessing individual elements
print(arr[0, 1])  # Output: 2
# Slicing
print(arr[:, 1])  # Output: [2 5]
print(arr[1, :2])  # Output: [4 5]

4. Array Operations

NumPy offers a number of functions for manipulating arrays through aggregation, element-wise arithmetic, and other tasks.

A. Element-Wise Arithmetic

In element-wise arithmetic, corresponding elements of two arrays are subjected to mathematical operations. NumPy supports all mathematical operations.

import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# Element-wise addition
print(arr1 + arr2)  # Output: [5 7 9]
# Element-wise multiplication
print(arr1 * arr2)  # Output: [ 4 10 18]

B. Aggregation Functions

NumPy's aggregation functions let you create summary statistics or aggregate data throughout the whole array or along certain axes. The aggregation functions np.sum(), np.mean(), np.max(), and np.min() are often used.

import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
# Sum of all elements
print(np.sum(arr))  # Output: 21
# Mean of all elements
print(np.mean(arr))  # Output: 3.5
# Maximum value
print(np.max(arr))  # Output: 6

NumPy Data Types

The data type (dtype) property of NumPy arrays specifies the kind of items that make up the array. Common data types are boo, complex, float, and int.

import numpy as np 
arr = np.array([1, 2, 3])
print(arr.dtype)  # Output: int64

Array Manipulation

A range of array manipulation methods are available in NumPy, allowing you to split, reshape, concatenate, and execute other operations on arrays.

1. Reshaping Arrays

NumPy arrays can be reshaped by altering their dimensions or form. This function is helpful for arranging data in various ways to accommodate particular calculations or analysis.

import numpy as np 
arr = np.arange(12)
# Reshape to a 3x4 array
reshaped_arr = np.reshape(arr, (3, 4))

2. Concatenating Arrays

Concatenating arrays is the process of joining several arrays together along a designated axis to form a single array. This process is useful for organizing data for additional analysis or combining data from several sources.

import numpy as np 
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6]])
# Concatenate along rows
concatenated_arr = np.concatenate((arr1, arr2), axis=0)

3. Adding and Removing Elements

Adding elements to a NumPy array involves appending new elements to the existing array, while removing elements entails deleting specific elements from the array. You can Append Elements to an array using 'np.append()'.

import numpy as np 
arr = np.array([1, 2, 3])
new_arr = np.append(arr, [4, 5])
print(new_arr)  # Output: [1 2 3 4 5]

You can delete elements from an array using 'np.delete()'.

import numpy as np 
arr = np.array([1, 2, 3, 4, 5])
# Delete elements at index 1 and 3
new_arr = np.delete(arr, [1, 3])
print(new_arr)  # Output: [1 3 5]

Indexing and Slicing

NumPy supports advanced indexing techniques and multidimensional indexing and slicing.

1. Advanced Indexing

NumPy's advanced indexing enables the use of arrays as indices or boolean masks to access or edit elements of an array. This offers strong and adaptable methods for picking particular components or subarrays from an array.

A. Boolean Indexing

Using boolean indexing, array items are chosen in accordance with a boolean condition, and the ones that satisfy the criteria are indicated by a mask. This makes it possible to quickly choose data according to predetermined standards.

import numpy as np
arr = np.array([1, 2, 3, 4, 5])
# Boolean indexing
mask = arr > 2
print(arr[mask])  # Output: [3 4 5]

B. Fancy Indexing

Accessing elements of an array using arrays of indices is known as Fancy Indexing.

import numpy as np
arr = np.array([1, 2, 3, 4, 5])
# Fancy indexing
indices = [0, 2, 4]
print(arr[indices])  # Output: [1 3 5]

2. Multidimensional Indexing and Slicing

When working with complicated datasets represented as multidimensional arrays, multidimensional indexing and slicing offer important features. 

import numpy as np 
arr = np.array([[1, 2, 3], [4, 5, 6]])
# Multidimensional indexing
print(arr[0, 1])  # Output: 2
# Multidimensional slicing
print(arr[:, 1])  # Output: [2 5]
Q: Can NumPy be used for image processing?
A: Yes, NumPy is commonly used for image processing tasks due to its efficient array manipulation capabilities.

Direct Approach and Indirect Approach for NumPy

Direct Approach

In the direct approach, you manipulate arrays directly using NumPy functions and methods. This involves creating arrays using NumPy functions like as 'np.array()', 'np.zeros()', 'np.ones()', and so on, and then immediately performing NumPy's array operations to these arrays.
For instance, you would call the 'np.zeros()' function directly if you wanted to use NumPy to build an array of zeros.

Indirect Approach

In the indirect method, NumPy is used indirectly by means of other libraries or frameworks that have NumPy integrated into them. This method uses an interface supplied by another library to indirectly use NumPy functions rather than interacting with them directly.
For example, you may use Pandas' 'to_numpy()' method to convert a Pandas DataFrame to a NumPy array.

For Continuation of Article:

NumPy in Python: A Comprehensive Guide Part2

If you read this far, tweet to the author to show them you care. Tweet a Thanks

More Posts

NumPy in Python: An Advanced Guide

Muzzamil Abbas - Mar 13

Pandas in Python: A Comprehensive Guide

Muzzamil Abbas - Apr 9

Git and GitHub for Python Developers A Comprehensive Guide

Tejas Vaij - Apr 7

Python Dictionaries: A Comprehensive Guide

Muhammad Sameer Khan - Mar 24

Numpy Tuitorial: Data Analysis for Data Science

Honey - Mar 24