Basic operations 2 questions
Need help with this assignment?Get an original answer from a qualified tutor — from $10/page.
Get it written →
##Exercise #3
# a. Create list baseball_height that contains data of the baseball players' height in inches
baseball_height = [74, 69, 71, 73, 76, 79, 75, 81]
#Use np.array() to create a numpy array from baseball_height. Name this array np_baseball_height.
#Print out the type of np_baseball_height to check that you got it right.
#convert the units to meters by Multiplying np_baseball_height with 0.0254.
#Store the new values in a new array, np_baseball_height_m.
#Print out np_baseball_height_m and check if the output makes sense.
# b. Create list baseball_weight that contains data of the baseball players' weight in pounds
baseball_weight = [174, 210, 181, 193, 230, 200, 185, 190]
#Create a numpy array from the baseball_weight list. Name this array np_baseball_weight.
#Multiply by 0.453592 to go from pounds to kilograms. Store the resulting numpy array as np_baseball_weight_kg.
#Use np_baseball_height_m and np_baseball_weight_kg to calculate the BMI of each player.
#Print out bmi
#c. Create a boolean numpy array: the element of the array should be True if the corresponding
#baseball player's BMI is below 21. You can use the < operator for this. Name the array light.
#Print the array light.
# Print out a numpy array with the BMIs of all baseball players whose BMI is below` 21.
#Use light inside square brackets to do a selection on the bmi array.
##2D NumPy Arrays
#Type of NumPy Arrays
type(np_height)
#Out: numpy.ndarray
type(np_weight)
#Out: numpy.ndarray
#ndarray = N-dimensional array
np_2d = np.array([[1.73, 1.68, 1.71, 1.89, 1.79],
[65.4, 59.2, 63.6, 88.4, 68.7]])
np_2d
#Out:array([[ 1.73, 1.68, 1.71, 1.89, 1.79],
# [ 65.4 , 59.2 , 63.6 , 88.4 , 68.7 ]])
np_2d.shape
#(2, 5); 2 rows, 5 columns
np.array([[1.73, 1.68, 1.71, 1.89, 1.79],
[65.4, 59.2, 63.6, 88.4, "68.7"]]) #array can contains only one type
#array([['1.73', '1.68', '1.71', '1.89', '1.79'],
# ['65.4', '59.2', '63.6', '88.4', '68.7']],
# dtype='<U32')
#Subsetting
np_2d[0]
#Out: array([ 1.73, 1.68, 1.71, 1.89, 1.79])
np_2d[0][2]
#Out: 1.71
np_2d[0,2]
#Out: 1.71
np_2d[:,1:3]
#array([[ 1.68, 1.71],
#[ 59.2 , 63.6 ]])
np_2d[1,:]
#Out: array([ 65.4, 59.2, 63.6, 88.4, 68.7])
#2D Arithmetic
np_mat = np.array([[1, 2],
[3, 4],
[5, 6]])
np_mat * 2
np_mat + np.array([10, 8])
np_mat + np_mat
np_mat*3
##Exercise #4
# Create baseball, a list of lists, the first column represents weight and the second
#column represents height
baseball = [[180, 78.4],
[215, 102.7],
[210, 98.5],
[188, 75.2]]
#a. Use np.array() to create a 2D numpy array from baseball. Name it np_baseball.
#Print out the type of np_baseball.
#b. Print out the shape attribute of np_baseball. Use np_baseball.shape.
#c. Print out the 3rd row of np_baseball.
#d. Make a new variable, np_weight, containing the entire first column of np_baseball.
#e. Select the height (second column) of the 2nd baseball player in np_baseball and print it out.
#f. You want to convert the units of height and weight, i.e. from pound to kg and inches to meter
#As a first step, create a numpy array with two values: 0.453592, 0.0254 and name this
#array factors. Multiply np_baseball with factors and print out the result.
Get a plagiarism-free answer to this question
Send us your instructions and we’ll match you with the best writer in your subject.
- 100% human-written, zero AI
- Turnitin report included
- Confidential — we never share your data
- Free revisions & refunds