In this topic, we'll explore NumPy, a fundamental library for numerical computing in Python. NumPy provides support for multidimensional arrays, mathematical functions, linear algebra operations, and more. We'll cover the basics of NumPy, including array creation, indexing, operations, and more.
NumPy, which stands for Numerical Python, is a powerful library in Python used for numerical computing. It provides support for creating and manipulating multidimensional arrays and offers a wide range of mathematical functions to perform operations efficiently.
import numpy as np
# Create a NumPy array
arr = np.array([1, 2, 3, 4, 5])
print("NumPy Array:", arr)
np
.np.array()
function and print it to the console.NumPy arrays can be created using various methods, such as from Python lists, using built-in functions, or by specifying data types.
import numpy as np
# Create a NumPy array from a Python list
arr1 = np.array([1, 2, 3, 4, 5])
# Create a NumPy array of zeros
arr2 = np.zeros(5)
# Create a NumPy array of ones with a specified shape
arr3 = np.ones((2, 3))
print("Array from list:", arr1)
print("Array of zeros:", arr2)
print("Array of ones:", arr3)
np.array()
to create an array from a Python list.np.zeros()
to create an array filled with zeros.np.ones()
to create an array filled with ones with a specified shape.NumPy arrays have attributes such as shape, size, and data type, which provide information about the array’s dimensions and elements.
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print("Array:", arr)
print("Shape:", arr.shape)
print("Size:", arr.size)
print("Data Type:", arr.dtype)
NumPy arrays support indexing and slicing to access individual elements or subarrays.
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print("First element:", arr[0])
print("Last element:", arr[-1])
Array slicing allows selecting a subset of elements from an array based on a specified range.
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print("Subset of array:", arr[1:4])
NumPy arrays support element-wise arithmetic operations, such as addition, subtraction, multiplication, and division.
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
print("Addition:", arr1 + arr2)
print("Subtraction:", arr1 - arr2)
print("Multiplication:", arr1 * arr2)
print("Division:", arr2 / arr1)
NumPy provides universal functions (ufuncs) for performing element-wise operations on arrays, such as trigonometric functions, exponentiation, and rounding.
import numpy as np
arr = np.array([1, 2, 3])
print("Exponential:", np.exp(arr))
print("Sine:", np.sin(arr))
print("Square root:", np.sqrt(arr))
np.exp()
, np.sin()
, and np.sqrt()
to perform exponential, sine, and square root operations on the array elements.NumPy provides various functions for manipulating arrays, such as reshaping, concatenating, and splitting arrays.
import numpy as np
arr = np.arange(1, 10)
# Reshape array
reshaped_arr = arr.reshape(3, 3)
# Concatenate arrays
concatenated_arr = np.concatenate([reshaped_arr, reshaped_arr])
# Split array
split_arr = np.split(concatenated_arr, 3)
print("Original Array:\n", arr)
print("Reshaped Array:\n", reshaped_arr)
print("Concatenated Array:\n", concatenated_arr)
print("Split Array:\n", split_arr)
np.arange()
and reshape it into a 3×3 matrix.Broadcasting is a powerful feature of NumPy that allows operations to be performed on arrays of different shapes by automatically aligning their dimensions.
import numpy as np
arr1 = np.array([[1, 2, 3], [4, 5, 6]])
arr2 = np.array([10, 20, 30])
result = arr1 + arr2
print("Array 1:\n", arr1)
print("Array 2:\n", arr2)
print("Result:\n", result)
arr1
and a 1-dimensional array arr2
.arr1
and arr2
, and NumPy automatically broadcasts the smaller array to match the shape of the larger array.result
array.NumPy provides functions for performing various linear algebra operations, such as matrix multiplication, determinant calculation, and eigenvalue decomposition.
import numpy as np
matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[5, 6], [7, 8]])
# Matrix multiplication
product = np.dot(matrix1, matrix2)
# Determinant calculation
determinant = np.linalg.det(matrix1)
print("Matrix 1:\n", matrix1)
print("Matrix 2:\n", matrix2)
print("Matrix Product:\n", product)
print("Determinant of Matrix 1:", determinant)
matrix1
and matrix2
.np.dot()
and calculate the determinant of matrix1
using np.linalg.det()
.In this topic, we've covered the basics of NumPy, including array creation, indexing, slicing, and operations and advanced NumPy concepts, including array manipulation, broadcasting, and linear algebra operations. NumPy is a powerful library that forms the foundation for numerical computing in Python, enabling efficient manipulation of large datasets and complex mathematical operations.
NumPy's rich set of functionalities makes it an indispensable tool for numerical computing and data analysis in Python.
By mastering these advanced concepts, developers can efficiently handle complex numerical tasks, manipulate multidimensional data arrays, and perform advanced mathematical operations with ease. Whether you're working on scientific computing, machine learning, or data visualization, NumPy provides the tools you need to tackle challenging problems effectively. Happy Coding!❤️