NumPy Is A Powerful Python Library Used For Numerical Computing. Here Are S - 20250101 - 154624 - 0000
NumPy Is A Powerful Python Library Used For Numerical Computing. Here Are S - 20250101 - 154624 - 0000
Importing NumPy:
python
Copy code
import numpy as np
Creating Arrays:
python
Copy code
arr = np.array([1, 2, 3]) # 1D array
arr_2d = np.array([[1, 2], [3, 4]]) #
2D array
2. Array Creation Methods
Indexing:
python
Copy code
arr = np.array([10, 20, 30, 40])
print(arr[2]) # Output: 30
Slicing:
python
Copy code
arr = np.array([0, 1, 2, 3, 4])
print(arr[1:4]) # Output: [1, 2, 3]
Boolean Indexing:
python
Copy code
arr[arr > 2] # Returns elements
greater than 2
4. Array Operations
Elementwise Operations:
python
Copy code
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(a + b) # Output: [5, 7, 9]
Broadcasting:
python
Copy code
a = np.array([1, 2, 3])
print(a + 10) # Output: [11, 12,
13]
5. Shape Manipulation
Reshape:
python
Copy code
arr = np.array([1, 2, 3, 4, 5, 6])
arr = arr.reshape((2, 3)) #
Reshaped into 2 rows and 3
columns
Flatten:
python
Copy code
arr.flatten()
6. Aggregation and Statistics
Basic Statistics:
python
Copy code
arr = np.array([1, 2, 3, 4, 5])
arr.mean() # Mean
arr.sum() # Sum
arr.max() # Maximum
arr.min() # Minimum
arr.std() # Standard deviation
Axis-Based Aggregation:
python
Copy code
arr = np.array([[1, 2], [3, 4]])
arr.sum(axis=0) # Sum along
columns
arr.sum(axis=1) # Sum along
rows
7. Linear Algebra
Dot Product:
python
Copy code
np.dot(a, b)
Matrix Operations:
python
Copy code
np.linalg.inv(matrix) # Inverse
np.linalg.det(matrix) #
Determinant
8. Advanced Indexing
Fancy Indexing:
python
Copy code
arr = np.array([10, 20, 30, 40])
indices = [0, 2]
print(arr[indices]) # Output: [10,
30]
Masked Arrays:
python
Copy code
mask = arr > 20
print(arr[mask]) # Filter
elements greater than 20
9. File I/O