Numpy Tutorials for Beginners
1. Introduction to NumPy:
NumPy is a foundational library for numerical computations in Python. It provides efficient multi-dimensional arrays and a vast array of mathematical functions.
2. Creating NumPy Arrays:
import numpy as np
arr = np.array([1, 2, 3]) # 1D array
mat = np.array([[1, 2, 3], [4, 5, 6]]) # 2D array
3. Array Attributes:
print(arr.shape) # Shape of array
print(mat.ndim) # Number of dimensions
print(mat.size) # Total number of elements
4. Array Indexing and Slicing:
print(arr[0]) # Access element
print(mat[1, 2]) # Access element in 2D array
print(arr[1:]) # Slice array
print(mat[:, 1:3]) # Slice columns in 2D array
5. Array Reshaping and Resizing:
reshaped = arr.reshape(1, 3) # Reshape array
resized = np.resize(arr, (2, 2)) # Resize array
6. Basic Array Operations:
addition = arr + 2
subtraction = arr - 1
multiplication = arr * 3
division = arr / 2
7. Universal Functions (ufuncs):
squared = np.square(arr)
sqrt = np.sqrt(arr)
exponential = np.exp(arr)
8. Broadcasting:
A = np.array([[1, 2], [3, 4]])
B = np.array([10, 20])
result = A + B # Broadcasting B to each row of A
9. Aggregation Functions:
sum_all = arr.sum()
mean = mat.mean()
max_val = arr.max()
min_val = mat.min()
10. Array Comparison and Boolean Indexing:
greater_than_2 = arr > 2
filtered = arr[arr > 1]
11. Fancy Indexing:
indices = np.array([0, 2])
selected = arr[indices]
12. Sorting and Searching:
sorted_arr = np.sort(arr)
index_of_2 = np.where(arr == 2)
13. Array Stacking and Splitting:
stacked = np.hstack((arr, arr))
split = np.split(arr, 3) # Split into 3 equal parts
14. Array Copying and Views:
copy = arr.copy() # Create a deep copy
view = arr.view() # Create a view (shallow copy)
15. Array Math and Linear Algebra:
dot_product = np.dot(arr, arr)
matrix_product = np.matmul(mat, mat)
16. Random Number Generation with NumPy:
random_arr = np.random.rand(3, 3) # Random numbers from a uniform distribution
normal_dist = np.random.randn(1000) # Random numbers from a normal distribution
17. Masked Arrays:
masked = np.ma.masked_where(arr > 2, arr) # Create a masked array
18. Structured Arrays:
structured = np.array([('Alice', 25), ('Bob', 30)], dtype=[('name', 'U10'), ('age', int)])
19. File Input and Output:
np.save('my_array.npy', arr) # Save array to file
loaded_arr = np.load('my_array.npy') # Load array from file
20. Advanced Array Manipulation:
transposed = mat.T # Transpose array
unique_values = np.unique(arr) # Get unique values
21. Memory Management:
arr_bytes = arr.nbytes # Get memory usage in bytes
22. Optimization with Vectorization:
# Vectorized version of non-vectorized operation
result = np.sum(np.arange(1, 1000000))
23. NumPy and Pandas Integration:
import pandas as pd
data_frame = pd.DataFrame(mat, columns=['A', 'B', 'C'])
numpy_array = data_frame.to_numpy()
24. NumPy and Matplotlib Integration:
import matplotlib.pyplot as plt
plt.plot(arr)
plt.show()
25. Handling Missing Data with NaN:
arr_with_nan = np.array([1, 2, np.nan, 4])
26. Efficient Broadcasting with np.newaxis:
column_vector = arr[:, np.newaxis]
27. Interoperability with Native Python Lists:
python_list = arr.tolist()
numpy_array = np.array(python_list)
28. Performance Tips and Tricks:
- Use vectorized operations for speed.
- Avoid explicit loops.
- Preallocate arrays whenever possible.
29. Working with DateTime in NumPy:
dates = np.array(['2023-08-01', '2023-08-02'], dtype='datetime64')
30. Multi-dimensional Array Indexing:
cube = np.arange(27).reshape(3, 3, 3)
value = cube[1, 2, 0]