Learn about Numpy Arrays in Python programming: Getting started, Numpy arrays are great alternatives to Python Lists. Some of the key advantages of Numpy arrays are that they are fast, easy to work with and give users the opportunity to perform calculations across entire arrays.
In the following example, you will first create two Python lists. Then, you will import the numpy package and create numpy arrays out of the newly created lists.
import numpy as np # Create 2 numpy arrays from height and weight np_height = np.array(height) np_weight = np.array(weight)
Print out the type of np_height
Now we can perform element-wise calculations on height and weight. For example, you could take all 6 of the height and weight observations above, and calculate the BMI for each observation with a single equation. These operations are very fast and computationally efficient. They are particularly helpful when you have 1000s of observations in your data.
# Calculate bmi bmi = np_weight / np_height ** 2 # Print the result print(bmi)
Another great feature of Numpy arrays is the ability to subset. For instance, if you wanted to know which observations in our BMI array are above 23, we could quickly subset it to find out.
# For a boolean response bmi > 23 # Print only those observations above 23 bmi[bmi > 23]
First, convert the list of weights from a list to a Numpy array. Then, convert all of the weights from kilograms to pounds. Use the scalar conversion of 2.2 lbs per kilogram to make your conversion. Lastly, print the resulting array of weights in pounds on screen.
#this is the answer code. use compiler below to do assignment on your own dime. weight_kg = [81.65, 97.52, 95.25, 92.98, 86.18, 88.45] import numpy as np # Create a numpy array np_weight_kg from weight_kg np_weight_kg = np.array(weight_kg) # Create np_weight_lbs from np_weight_kg np_weight_lbs = np_weight_kg * 2.2 # Print out np_weight_lbs print(np_weight_lbs)
Trinket.io on-line Python compiler
The Python Standard Library Ver 3.6.5
Learn Code Introspection Python Programming
Learn Partial functions Python programming
Learn RE – Regular Expressions in Python
Comparing Python to other languages
Google’s “Grasshopper” Mobile Game Teaches Adults How To Code In An Easy, Accessible Way — And It’s Free
Learn List Comprehensions in Python Programming
Why Python is the cooler programming language
How can Alice help teach OOP (Object Oriented Programming)?