Unveiling the Power of NumPy: A Look at Essential Array Functions
Array Creation Routines
These routines are used to create new NumPy arrays. Some commonly used functions include:
np.arange
: Creates an array with evenly spaced values within a specific range.np.empty
: Creates an uninitialized array (faster thannp.zeros
but potentially contains garbage values).np.ones
: Creates an array filled with ones.np.zeros
: Creates an array filled with zeros.np.array
: Creates an array from a list, tuple, or other iterable.
Array Manipulation Routines
These routines perform operations on existing NumPy arrays. Examples include:
np.sort
: Sorts the elements of an array.np.split
: Splits an array into multiple sub-arrays.np.concatenate
: Combines multiple arrays along a specified axis.np.transpose
: Swaps the axes of an array.np.reshape
: Changes the shape of an array.
Linear Algebra Routines
These routines provide functions for linear algebra operations on arrays. For instance:
np.linalg.eig
: Computes the eigenvalues and eigenvectors of a matrix.np.linalg.inv
: Computes the inverse of a matrix.np.dot
: Performs matrix multiplication.
Mathematical Routines
These routines offer various mathematical functions that can be applied element-wise to arrays. Some examples include:
np.add
,np.subtract
,np.multiply
,np.divide
: Basic arithmetic operations.np.sqrt
: Square root function.np.exp
,np.log
: Exponential and logarithmic functions.np.sin
,np.cos
,np.tan
: Trigonometric functions.
Statistical Routines
These routines perform statistical operations on arrays. For instance:
np.percentile
: Calculates percentiles of an array.np.median
: Finds the median of an array.np.std
: Computes the standard deviation of an array.np.mean
: Calculates the mean of an array.
Random Number Generation Routines
These routines generate random numbers:
np.random.randint
: Generates random integers within a specified range.np.random.randn
: Generates random numbers from a standard normal distribution.np.random.rand
: Generates random floats between 0 and 1.
Array Creation
import numpy as np
# From a list
data = [1, 2, 3, 4, 5]
arr = np.array(data)
print(arr) # Output: [1 2 3 4 5]
# Zeros and ones with specific shape
zeros_2x3 = np.zeros((2, 3)) # 2 rows, 3 columns filled with zeros
print(zeros_2x3)
ones_diag = np.ones((3, 3), dtype=bool) # Diagonal filled with ones (boolean type)
print(ones_diag)
Array Manipulation
# Reshape
arr = np.arange(12).reshape(3, 4)
print(arr) # Output: [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]]
# Transpose (swap rows and columns)
transposed = arr.T
print(transposed) # Output: [[ 0 4 8] [ 1 5 9] [ 2 6 10] [ 3 7 11]]
# Concatenate (vertically)
top = np.array([10, 11, 12])
combined = np.concatenate((arr, top.reshape(1, -1)), axis=0) # Reshape top for proper axis=0
print(combined)
Linear Algebra
# Matrix multiplication
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
product = np.dot(A, B)
print(product) # Output: [[19 22] [43 50]]
# Inverse of a matrix
inv_A = np.linalg.inv(A)
print(inv_A) # Output: [[-2. 1. ] [ 1.5 -0.5]]
Mathematical Routines
arr = np.array([1, 4, 9])
# Sine of each element
sin_values = np.sin(arr)
print(sin_values) # Output: [ 0.84147098 0.75680249 0.38941834]
# Square root
sqrt_values = np.sqrt(arr)
print(sqrt_values) # Output: [1. 2. 3.]
data = np.random.rand(10) # Generate 10 random floats
# Mean
average = np.mean(data)
print(average) # Random value between 0 and 1
# Standard deviation
stddev = np.std(data)
print(stddev) # Random value
- Modules
NumPy itself is a module, and within it, some routines might be grouped into specific modules based on their functionality (e.g.,numpy.random
for random number generation). - Operations
This term emphasizes the action performed by the routines on the data. - Methods
While less common in the context of NumPy itself, some routines might be considered methods if they operate on specific objects like arrays. - Functions
This is the most general term and encompasses all functionalities offered by NumPy, including routines for array creation, manipulation, mathematical operations, etc.