2D Arithmetic
2D numpy
arrays can perform calculations element by element, like numpy
arrays.
np_baseball
is coded for you; it's again a 2D numpy
array with 3 columns representing height (in inches), weight (in pounds) and age (in years). baseball
is available as a regular list of lists and updated
is available as 2D numpy array.
This exercise is part of the course
Introduction to Python
Exercise instructions
- You managed to get hold of the changes in height, weight and age of all baseball players. It is available as a 2D
numpy
array,updated
. Addnp_baseball
andupdated
and print out the result. - You want to convert the units of height and weight to metric (meters and kilograms, respectively). As a first step, create a
numpy
array with three values:0.0254
,0.453592
and1
. Name this arrayconversion
. - Multiply
np_baseball
withconversion
and print out the result.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
import numpy as np
np_baseball = np.array(baseball)
# Print out addition of np_baseball and updated
# Create numpy array: conversion
# Print out product of np_baseball and conversion