Your First 2D NumPy Array
Before working on the actual MLB data, let's try to create a 2D numpy array from a small list of lists.
In this exercise, baseball is a list of lists. The main list contains 4 elements. Each of these elements is a list containing the height and the weight of 4 baseball players, in this order. baseball is already coded for you in the script.
This exercise is part of the course
Introduction to Python
Exercise instructions
- Use
np.array()to create a 2Dnumpyarray frombaseball. Name itnp_baseball. - Print out the type of
np_baseball. - Print out the
shapeattribute ofnp_baseball. Usenp_baseball.shape.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
import numpy as np
baseball = [[180, 78.4],
[215, 102.7],
[210, 98.5],
[188, 75.2]]
# Create a 2D numpy array from baseball: np_baseball
# Print out the type of np_baseball
# Print out the shape of np_baseball