Click to share! ⬇️

NumPy is a powerful library for working with large, multi-dimensional arrays and matrices of numerical data in Python. It provides functions for performing mathematical operations on these large, multi-dimensional arrays and matrices and tools for reading and writing array data to disk. NumPy is a crucial component of the scientific Python ecosystem and is widely used in various scientific and mathematical applications.

NumPy is particularly useful for working with large arrays because it enables you to perform element-wise operations on the data, as well as powerful aggregations and reshaping operations. This can be much faster and more memory-efficient than using Python’s built-in data structures and operations.

Install NumPy

To use NumPy, you will first need to install it. NumPy can be installed using pip, the Python package manager, by running the following command:

pip install numpy

Once NumPy is installed, you can import it into your Python code using the following import statement:

import numpy as np

Introduction To ndarray

The NumPy library is organized around the concept of the ndarray (n-dimensional array), which is a fast and efficient multidimensional array for holding and manipulating large arrays of homogeneous data (i.e., data of the same type, such as integers or floating point values). The ndarray is an essential building block of many other scientific Python libraries and is an essential tool for many scientific and mathematical applications.

ndarray Examples

Here are some examples of creating and manipulating NumPy ndarray objects:

# Import NumPy
import numpy as np

# Create a 1-dimensional array
a = np.array([1, 2, 3])
print(a)
# Output: [1 2 3]

# Create a 2-dimensional array
b = np.array([[1, 2, 3], [4, 5, 6]])
print(b)
# Output: [[1 2 3]
#          [4 5 6]]

# Create a 3-dimensional array
c = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print(c)
# Output: [[[ 1  2  3]
#           [ 4  5  6]]
#
#          [[ 7  8  9]
#           [10 11 12]]]

# Get the shape of an array
print(a.shape)
# Output: (3,)
print(b.shape)
# Output: (2, 3)
print(c.shape)
# Output: (2, 2, 3)

# Get the data type of an array
print(a.dtype)
# Output: int64
print(b.dtype)
# Output: int64
print(c.dtype)
# Output: int64

# Get the number of dimensions of an array
print(a.ndim)
# Output: 1
print(b.ndim)
# Output: 2
print(c.ndim)
# Output: 3

# Get the size of an array (total number of elements)
print(a.size)
# Output: 3
print(b.size)
# Output: 6
print(c.size)
# Output: 12

# Get the memory layout of an array
print(a.flags)
# Output:
#   C_CONTIGUOUS : True
#   F_CONTIGUOUS : True
#   OWNDATA : True
#   WRITEABLE : True
#   ALIGNED : True
#   WRITEBACKIFCOPY : False
#   UPDATEIFCOPY : False

Element-wise Operations

In NumPy, an “element-wise operation” is an operation that is applied to each element in an array rather than to the array as a whole. For example, consider the following NumPy arrays:

import numpy as np

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

If we want to perform an element-wise operation on these arrays, we can use any of NumPy’s element-wise functions, such as add, subtract, multiply, etc. For example:

c = np.add(a, b)
print(c)
# Output: [5 7 9]

d = np.subtract(a, b)
print(d)
# Output: [-3 -3 -3]

e = np.multiply(a, b)
print(e)
# Output: [ 4 10 18]

We can also use arithmetic operators such as +, -, *, etc. to perform element-wise operations. For example:

f = a + b
print(f)
# Output: [5 7 9]

g = a - b
print(g)
# Output: [-3 -3 -3]

h = a * b
print(h)
# Output: [ 4 10 18]

Element-wise operations are a powerful tool for working with arrays, as they allow you to apply a given operation to every element in the array, rather than having to loop over the array yourself. This can be much faster and more memory-efficient than using Python’s built-in data structures and operations.

NumPy Aggregations

In NumPy, aggregations refers to functions that perform aggregation operations (i.e., returning a single value from a large number of values) on arrays. NumPy provides a variety of functions for performing aggregations, such as sum, mean, median, min, max, etc.

For example, consider the following NumPy array:

import numpy as np

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

We can use the sum function to compute the sum of all the elements in the array:

s = np.sum(a)
print(s)
# Output: 15

We can use the mean function to compute the mean (average) of all the elements in the array:

m = np.mean(a)
print(m)
# Output: 3.0

We can use the median function to compute the median of all the elements in the array:

md = np.median(a)
print(md)
# Output: 3.0

We can use the min and max functions to compute the minimum and maximum values of the elements in the array:

mi = np.min(a)
print(mi)
# Output: 1

mx = np.max(a)
print(mx)
# Output: 5

There are many other functions available such as std (standard deviation), var (variance), and argmin/argmax (the indices of the minimum and maximum values). These functions can be very useful for performing statistical analysis and other kinds of data manipulation on large arrays of data.

Reshaping Operations

In NumPy, “reshaping operations” refers to functions that change the shape of an array, while preserving the data contained within it. NumPy provides several functions for reshaping arrays, including reshape, flatten, and transpose.

For example, consider the following NumPy array:

import numpy as np

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

We can use the reshape function to change the shape of the array to any shape we want, as long as the total number of elements remains the same:

b = np.reshape(a, (3, 2))
print(b)
# Output: [[1 2]
#          [3 4]
#          [5 6]]

c = np.reshape(a, (6, 1))
print(c)
# Output: [[1]
#          [2]
#          [3]
#          [4]
#          [5]
#          [6]]

We can use the flatten function to flatten a multi-dimensional array into a single-dimensional array:

d = np.flatten(a)
print(d)
# Output: [1 2 3 4 5 6]

We can use the transpose function to transpose an array, which means swapping its rows and columns:

e = np.transpose(a)
print(e)
# Output: [[1 4]
#          [2 5]
#          [3 6]]

There are many popular Python libraries that rely on NumPy and make use of its efficient numerical computations and array manipulation capabilities. Here are a few examples:

  1. SciPy: SciPy is a library for scientific computing in Python that provides functions for working with arrays, numerical optimization, signal and image processing, and more. SciPy makes use of NumPy arrays and functions to provide efficient and convenient solutions for scientific computing tasks.
  2. Pandas: Pandas is a library for data manipulation and analysis in Python that provides data structures and data analysis tools for handling and manipulating numerical tables and time series data. Pandas relies on NumPy for efficient operations on large datasets and for handling missing values and other data manipulation tasks.
  3. Matplotlib: Matplotlib is a library for data visualization in Python that provides functions for creating static, animated, and interactive plots and charts. Matplotlib makes use of NumPy arrays and functions to efficiently visualize and plot large datasets.
  4. Scikit-learn: Scikit-learn is a library for machine learning in Python that provides algorithms and tools for model fitting, evaluation, and prediction. Scikit-learn relies on NumPy arrays and functions for efficient manipulation and processing of large datasets and for implementing machine learning algorithms.
Click to share! ⬇️