Loops in Python are used to execute a block of code repeatedly. The two main types of loops are for
loops and while
loops.
# Calculate the sum of numbers from 1 to 10 using a for loop
sum_numbers = 0
for i in range(1, 11):
sum_numbers += i
print(sum_numbers)
In this example, the for
loop iterates over the numbers from 1 to 10, and at each iteration, the current number is added to the sum_numbers
variable.
# Calculate the sum of numbers from 1 to 10 using a while loop
sum_numbers = 0
i = 1
while i <= 10:
sum_numbers += i
i += 1
print(sum_numbers)
Here, the while
loop keeps running as long as the condition i <= 10
is true. At each iteration, the current number is added to the sum, and the counter i
is incremented.
NumPy is a library for the Python programming language, adding support for large, multi - dimensional arrays and matrices, along with a large collection of high - level mathematical functions to operate on these arrays.
import numpy as np
# Create a 1 - D array
arr = np.array([1, 2, 3, 4, 5])
print(arr)
# Create a 2 - D array
arr_2d = np.array([[1, 2, 3], [4, 5, 6]])
print(arr_2d)
In the code above, we first import the NumPy library with the alias np
. Then we create a 1 - D and a 2 - D NumPy array using the np.array()
function.
Vectorization in NumPy means performing operations on entire arrays at once instead of element - by - element using loops.
import numpy as np
# Create two arrays
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# Add the two arrays using vectorization
result = arr1 + arr2
print(result)
In this example, instead of using a loop to add each corresponding element of arr1
and arr2
, we simply use the +
operator. NumPy takes care of performing the addition element - wise in a highly optimized way.
import numpy as np
# Create an array
arr = np.array([1, 2, 3, 4, 5])
# Square each element of the array
squared = arr ** 2
print(squared)
# Calculate the sine of each element
sin_values = np.sin(arr)
print(sin_values)
Here, we perform element - wise squaring and calculate the sine of each element in the array using vectorized operations.
import numpy as np
# Create an array
arr = np.array([1, 2, 3, 4, 5])
# Find elements greater than 3
greater_than_3 = arr > 3
print(greater_than_3)
This code creates a boolean array indicating which elements of the original array are greater than 3.
import numpy as np
# Create two arrays with different shapes
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5])
# This will raise a ValueError
try:
result = arr1 + arr2
except ValueError as e:
print(f"Error: {e}")
In this example, we try to add two arrays with different shapes, which leads to a ValueError
. NumPy requires arrays to have compatible shapes for element - wise operations.
When working with very large arrays, vectorized operations can consume a significant amount of memory. For example, creating multiple large intermediate arrays during a complex operation can lead to memory errors.
import numpy as np
# Create an array with a specific data type
arr = np.array([1, 2, 3], dtype=np.int8)
print(arr.dtype)
By specifying the appropriate data type, you can reduce memory usage, especially when dealing with large arrays.
import numpy as np
# Instead of creating intermediate arrays
arr = np.array([1, 2, 3])
# Unnecessary intermediate array
temp = arr * 2
result = temp + 1
# Do it in one step
result = arr * 2 + 1
print(result)
In the second approach, we avoid creating an unnecessary intermediate array, which can save memory and improve performance.
In this tutorial, we have explored the transition from using traditional Python loops to vectorization in NumPy. Loops are useful for simple operations and when the logic is complex, but they can be slow for large datasets. Vectorization, on the other hand, offers a more efficient way to perform operations on arrays in NumPy, making the code more concise and faster. By understanding the concepts, typical usage scenarios, common pitfalls, and best practices, you can effectively use vectorization in your real - world numerical computing tasks.