Computation on NumPy Arrays using Python

NumPy is the backbone of numerical computing in Python and is preferred in Data Science due to its efficiency, versatility, and extensive mathematical capabilities. If you want to know how to use NumPy for numerical computations, this article is for you. In this article, I’ll take you through a complete guide on performing computation on NumPy Arrays using Python.

Computation on NumPy Arrays using Python

NumPy is a fundamental library in Python used for numerical and mathematical operations in Data Science and scientific computing. Let’s go through some concepts and functions used to perform computation on NumPy Arrays using Python.

Array Creation

NumPy arrays are the core of the library. They can be created in several ways. Here’s how to create NumPy Arrays using Python lists:

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

Some other functions to create NumPy Arrays:

  1. np.zeros(shape): Creates an array filled with zeros.
  2. np.ones(shape): Creates an array filled with ones.
  3. np.arange(start, stop, step): Creates an array with a range of numbers.
  4. np.linspace(start, stop, num): Creates an array with evenly spaced numbers over a specified interval.

Here are the examples of all the above functions:

zeros_array = np.zeros((2, 3))
ones_array = np.ones((3, 2))
range_array = np.arange(1, 10, 2)
linspace_array = np.linspace(0, 1, 5)

Array Attributes

Below are the functions used to understand Array attributes:

  • .shape: Array dimensions.
  • .dtype: Data type of the array elements.
  • .ndim: Number of array dimensions.
  • .size: Total number of elements.

Here are the examples of all the above functions:

print(np_array.shape)
print(np_array.dtype)
print(np_array.ndim)
print(np_array.size)
(5,)
int64
1
5

Indexing and Slicing

Here’s how to perform indexing and slicing using NumPy Arrays:

element = np_array[2]  # Access the third element
sub_array = np_array[1:4]  # Access elements from index 1 to 3

Basic Mathematical Operations

Below are the operators used to perform basic mathematical computations using NumPy Arrays:

  • Arithmetic Operations: +, -, *, /, ** (element-wise operations).
  • Aggregation Functions: np.sum(), np.mean(), np.min(), np.max().
  • Reshaping Arrays: np_array.reshape(new_shape).
  • Transpose: np_array.T.

Below are the examples of all the above operations:

Arithmetic Operations:
array_sum = np_array + np_array
array_diff = np_array - np_array
array_product = np_array * np_array
array_division = np_array / np_array
array_power = np_array ** 2
Aggregation Functions:
array_sum = np.sum(np_array)
array_mean = np.mean(np_array)
array_min = np.min(np_array)
array_max = np.max(np_array)
Reshaping and Transpose:
reshaped_array = np_array.reshape((5, 1))
transposed_array = reshaped_array.T

Matrix Multiplication

Here’s how to perform matrix multiplication using NumPy Arrays:

matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[5, 6], [7, 8]])
matrix_product = np.matmul(matrix1, matrix2)

Linear Algebra

Below are the functions you can use to perform Linear Algebra using NumPy Arrays:

  • np.linalg.inv(): Inverse of a matrix.
  • np.linalg.det(): Determinant of a matrix.
  • np.linalg.eig(): Eigenvalues and eigenvectors.

Here are the examples of all the linear algebra functions mentioned above:

inverse_matrix = np.linalg.inv(matrix1)
determinant = np.linalg.det(matrix1)
eigenvalues, eigenvectors = np.linalg.eig(matrix1)

We can use these concepts in various practical scenarios. Let’s solve a simple linear equation system as an example:

# System of equations:
# 3x + 1y = 9
# 1x + 2y = 8

A = np.array([[3, 1], [1, 2]])
B = np.array([9, 8])

solution = np.linalg.solve(A, B)
print(solution)
[2. 3.]

This guide covers the fundamentals of computation using NumPy Arrays, but there’s much more to explore. You can explore more at the official documentation of NumPy.

Summary

NumPy is the backbone of numerical computing in Python and is preferred in Data Science due to its efficiency, versatility, and extensive mathematical capabilities. I hope you liked this article on a guide to Computation on NumPy Arrays using Python. Feel free to ask valuable questions in the comments section below.

Aman Kharwal
Aman Kharwal

AI/ML Engineer | Published Author. My aim is to decode data science for the real world in the most simple words.

Articles: 2074

Leave a Reply

Discover more from AmanXai by Aman Kharwal

Subscribe now to keep reading and get access to the full archive.

Continue reading