🔢 NumPy Common Functions¶
NumPy, or Numerical Python, is a powerful library for numerical computations in Python. This tutorial will cover some of the most commonly used functions in NumPy.
1. Creating Arrays¶
First, let's look at how to create arrays using NumPy.
import numpy as np
# Create a 1D array
arr1 = np.array([1, 2, 3, 4, 5])
print("1D Array:", arr1)
# Create a 2D array
arr2 = np.array([[1, 2, 3], [4, 5, 6]])
print("2D Array:\n", arr2)
1D Array: [1 2 3 4 5] 2D Array: [[1 2 3] [4 5 6]]
2. Array Operations¶
NumPy allows you to perform element-wise operations on arrays, such as addition, subtraction, multiplication, and division.
# Element-wise addition
print("Addition:\n", arr1 + arr1)
# Element-wise subtraction
print("Subtraction:\n", arr1 - arr1)
# Element-wise multiplication
print("Multiplication:\n", arr1 * arr1)
# Element-wise division
print("Division:\n", arr1 / arr1)
Addition: [ 2 4 6 8 10] Subtraction: [0 0 0 0 0] Multiplication: [ 1 4 9 16 25] Division: [1. 1. 1. 1. 1.]
3. Mathematical Functions¶
NumPy provides a wide variety of mathematical functions.
# Square root
print("Square root:\n", np.sqrt(arr1))
# Exponential (base e)
print("Exponential:\n", np.exp(arr1))
# Logarithm base e, 2 and 10
print("Natural logarithm:\n", np.log(arr1))
print("Base 2 logarithm:\n", np.log2(arr1))
print("Base 10 logarithm:\n", np.log10(arr1))
# Trigonometric functions
print("Sin:\n", np.sin(arr1))
print("Cos:\n", np.cos(arr1))
print("Tan:\n", np.tan(arr1))
Square root: [1. 1.41421356 1.73205081 2. 2.23606798] Exponential: [ 2.71828183 7.3890561 20.08553692 54.59815003 148.4131591 ] Natural logarithm: [0. 0.69314718 1.09861229 1.38629436 1.60943791] Base 2 logarithm: [0. 1. 1.5849625 2. 2.32192809] Base 10 logarithm: [0. 0.30103 0.47712125 0.60205999 0.69897 ] Sin: [ 0.84147098 0.90929743 0.14112001 -0.7568025 -0.95892427] Cos: [ 0.54030231 -0.41614684 -0.9899925 -0.65364362 0.28366219] Tan: [ 1.55740772 -2.18503986 -0.14254654 1.15782128 -3.38051501]
4. Statistical Functions¶
NumPy provides functions to compute statistics of datasets.
# Mean
print("Mean:", np.mean(arr1))
# Standard deviation
print("Standard Deviation:", np.std(arr1))
# Variance
print("Variance:", np.var(arr1))
# Min and Max
print("Min:", np.min(arr1))
print("Max:", np.max(arr1))
# Sum and Product
print("Sum:", np.sum(arr1))
print("Product:", np.prod(arr1))
Mean: 3.0 Standard Deviation: 1.4142135623730951 Variance: 2.0 Min: 1 Max: 5 Sum: 15 Product: 120
5. Linear Algebra Functions¶
NumPy also supports a number of linear algebra functions.
# Dot product
print("Dot Product:", np.dot(arr1, arr1))
# Matrix multiplication
print("Matrix Multiplication:\n", np.matmul(arr2, arr2.T)) # .T for transpose
# Determinant, Inverse, Eigenvalues
arr3 = np.array([[1, 2], [3, 4]])
print("Determinant:", np.linalg.det(arr3))
print("Inverse:\n", np.linalg.inv(arr3))
print("Eigenvalues:", np.linalg.eigvals(arr3))
# Solve linear system
arr4 = np.array([5, 6])
print("Solution to linear system:", np.linalg.solve(arr3, arr4))
Dot Product: 55 Matrix Multiplication: [[14 32] [32 77]] Determinant: -2.0000000000000004 Inverse: [[-2. 1. ] [ 1.5 -0.5]] Eigenvalues: [-0.37228132 5.37228132] Solution to linear system: [-4. 4.5]
This is just the tip of the iceberg of what NumPy can do. It provides a powerful and efficient way to perform numerical operations in Python. It's also the foundation for many other scientific computing packages in Python, such as SciPy and pandas. By mastering NumPy, you'll be well on your way to doing scientific computing with Python.