-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumPy methods
More file actions
33 lines (27 loc) · 1.1 KB
/
numPy methods
File metadata and controls
33 lines (27 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Sample array
arr = np.array([[10, 20, 5], [30, 15, 25]])
# a. Extract numbers less than and greater than a specified number
specified_number = 18
less_than = arr[arr < specified_number]
greater_than = arr[arr > specified_number]
print("Less than 18:", less_than)
print("Greater than 18:", greater_than)
# b. Indices of max and min along axis
import numpy as np
# Create a sample 2D NumPy array
arr = np.array([[10, 20, 5],
[15, 3, 25],
[30, 8, 12]])
print("Original array:")
print(arr)
# Find the indices of the maximum values along each row (axis=1)
max_indices = np.argmax(arr, axis=1)
# Find the indices of the minimum values along each row (axis=1)
min_indices = np.argmin(arr, axis=1)
print("\nIndices of the maximum values along each row:", max_indices)
print("Indices of the minimum values along each row:", min_indices)
# To verify, you can use these indices to get the actual max and min values
max_values = arr[np.arange(len(arr)), max_indices]
min_values = arr[np.arange(len(arr)), min_indices]
print("\nMaximum values:", max_values)
print("Minimum values:", min_values)