-
1. Introduction to Python
-
2. Python Basics
-
3. Working with Data Structures
-
4. Functions and Modules
-
5. Object-Oriented Programming (OOP)
-
6. File Handling
-
7. Error and Exception Handling
-
8. Python for Data Analysis
-
9. Advanced Topics in Python
-
10. Working with APIs
-
11. Python for Automation
-
12. Capstone Projects
- 13. Final Assessment and Quizzes
8.1.1 Arrays and mathematical operations
One of the primary strengths of NumPy is its ability to handle arrays and perform a wide range of mathematical operations efficiently. NumPy arrays allow you to perform element-wise operations, matrix operations, and mathematical computations without the need for explicit loops, which improves both performance and readability.
1. Creating NumPy Arrays
Before diving into mathematical operations, let's first look at how to create NumPy arrays.
import numpy as np # Creating 1D array arr1 = np.array([1, 2, 3, 4]) # Creating 2D array (Matrix) arr2 = np.array([[1, 2], [3, 4]]) # Creating 3D array arr3 = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) print(arr1) print(arr2) print(arr3)
2. Element-wise Mathematical Operations
NumPy allows you to apply mathematical operations element-wise. This means the operation is performed for each element in the array individually.
a. Addition
arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) # Element-wise addition result = arr1 + arr2 print(result) # Output: [5 7 9]
b. Subtraction
result = arr1 - arr2 print(result) # Output: [-3 -3 -3]
c. Multiplication
result = arr1 * arr2 print(result) # Output: [4 10 18]
d. Division
result = arr2 / arr1 print(result) # Output: [4. 2.5 2.]
e. Exponentiation
result = arr1 ** 2 print(result) # Output: [1 4 9]
3. Mathematical Functions in NumPy
NumPy provides a rich set of mathematical functions to operate on arrays. These functions are optimized for performance.
a. Sum, Mean, and Standard Deviation
arr = np.array([1, 2, 3, 4, 5]) # Sum of elements print(np.sum(arr)) # Output: 15 # Mean of elements print(np.mean(arr)) # Output: 3.0 # Standard deviation of elements print(np.std(arr)) # Output: 1.4142135623730951
b. Square Root, Logarithm
arr = np.array([4, 9, 16]) # Square root of each element print(np.sqrt(arr)) # Output: [2. 3. 4.] # Logarithm of each element print(np.log(arr)) # Output: [1.38629436 2.19722458 2.77258872]
c. Trigonometric Functions
NumPy also supports trigonometric operations.
arr = np.array([0, np.pi/2, np.pi]) # Sine of each element print(np.sin(arr)) # Output: [0. 1. 0.] # Cosine of each element print(np.cos(arr)) # Output: [1. 0. -1.]
4. Matrix Operations
NumPy also provides support for matrix operations that are widely used in fields such as linear algebra and machine learning.
a. Dot Product
The dot product of two arrays can be computed using np.dot(). It is commonly used for matrix multiplication.
arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) result = np.dot(arr1, arr2) print(result) # Output: 32 (1*4 + 2*5 + 3*6)
b. Matrix Multiplication
For matrix multiplication, NumPy offers the @ operator (also known as the matrix multiplication operator) or np.matmul().
arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[5, 6], [7, 8]]) result = arr1 @ arr2 print(result) # Output: # [[19 22] # [43 50]]
c. Transpose
You can transpose a matrix using the .T attribute.
arr = np.array([[1, 2], [3, 4]]) # Transposing the matrix result = arr.T print(result) # Output: # [[1 3] # [2 4]]
5. Broadcasting in NumPy
Broadcasting is a powerful feature in NumPy that allows you to perform operations on arrays of different shapes. The smaller array is "broadcast" over the larger array, which allows for element-wise operations between arrays of different sizes.
arr1 = np.array([1, 2, 3]) arr2 = np.array([[1], [2]]) # Broadcasting arr2 to match the shape of arr1 result = arr1 + arr2 print(result) # Output: # [[2 3 4] # [3 4 5]]
6. Vectorized Operations (No Loops)
In standard Python, you would use loops to perform operations on lists or arrays. However, NumPy allows for vectorized operations, meaning the operation can be applied to the entire array without the need for explicit looping, which is both faster and more readable.
arr = np.array([1, 2, 3, 4]) # Multiply each element by 2 (vectorized operation) result = arr * 2 print(result) # Output: [2 4 6 8]
7. Conclusion
NumPy’s powerful array manipulation and mathematical functions allow for fast and efficient handling of numerical data. By leveraging vectorization and broadcasting, you can perform complex mathematical and matrix operations with ease. Whether you are working with small datasets or large-scale scientific computations, NumPy provides a flexible and optimized framework for all your mathematical needs in Python.
Commenting is not enabled on this course.