-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathsubsetting-2d-numpy-arrays.py
More file actions
48 lines (38 loc) · 1.58 KB
/
subsetting-2d-numpy-arrays.py
File metadata and controls
48 lines (38 loc) · 1.58 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'''
Subsetting 2D NumPy Arrays
100xp
If your 2D numpy array has a regular structure, i.e. each row and column has a
fixed number of values, complicated ways of subsetting become very easy.
Have a look at the code below where the elements "a" and "c" are extracted
from a list of lists.
# regular list of lists
x = [["a", "b"], ["c", "d"]]
[x[0][0], x[1][0]]
# numpy
import numpy as np
np_x = np.array(x)
np_x[:,0]
For regular Python lists, this is a real pain. For 2D numpy arrays, however,
it's pretty intuitive! The indexes before the comma refer to the rows, while
those after the comma refer to the columns. The : is for slicing; in this example,
it tells Python to include all rows.
The code that converts the pre-loaded baseball list to a 2D numpy array is already
in the script. The first column contains the players' height in inches and the second
column holds player weight, in pounds. Add some lines to make the correct selections.
Remember that in Python, the first element is at index 0!
Instructions
-Print out the 50th row of np_baseball.
-Make a new variable, np_weight, containing the entire second column of np_baseball.
-Select the height (first column) of the 124th baseball player in np_baseball and print it out.
'''
# baseball is available as a regular list of lists
# Import numpy package
import numpy as np
# Create np_baseball (2 cols)
np_baseball = np.array(baseball)
# Print out the 50th row of np_baseball
print(np_baseball[49,:])
# Select the entire second column of np_baseball: np_weight
np_weight = np_baseball[:,1]
# Print out height of 124th player
print(np_baseball[123,0])