Python packages and libraries are collections of functions and modules centered around a common theme. In this section we will learn how to import a Python package and use functions from that package.
Objectives
Import a Python package or library (our example NumPy).
Use functions from a Python package.
SubsectionImporting the package NumPy
NumPy is an open source scientific computing package that allows you to use standard mathematical functions and constants like sine or pi. NumPy also allows you to work with arrays of numbers so you can efficiently perform computations.
Note to call a function from an imported package you must use the syntax package.function. To avoid retyping the long name of a package every time, it is standard practice to rename the package as you import.
You Try1.4.
Rename the package numpy in the above code by replacing the import line with import numpy as np. Next use the new name of the package to call the function np.sin() and re-run.
SubsectionStandard functions in NumPy
In addition to the standard trigonometric functions, NumPy also contains \(e^x\) as exp() and natural log as log(), as well as the standard mathematical constants like pi and e.
You Try1.5.
Use the NumPy package to compute the area of a circle using pi.
Hint.
If you import numpy as np, you will use np.pi
SubsectionNumPy arrays
NumPy arrays can help us efficiently do computations with a collection of numbers all at once. We can also represent vectors or matrices using NumPy arrays. Run the code below that calls the NumPy function array to see how it works.
NumPy also has built in functions to automatically create an array with certain conditions. For example linspace() takes a given interval and sets up an array of evenly spaced numbers on that interval.
Note that the array includes 5, so it takes 11 evenly spaced numbers to get numbers 0.5 apart. Note also that although the number values are evenly spaced, the printed array is not. Namely the default is to space out the entries by the maximum number of decimal places with blank spaces instead of extra zeroes.
You Try1.6.
Edit the above code to use linspace to create an array of numbers between 0 and 5 that are one-quarter apart. When you evaluate your code should print the list 0, 0.25, 0.5, 0.75, etc.
Another way to create an array of values for a given interval is arange(), which uses a step value to set up a sequence of numbers within that interval.
Note that the array gets cut off before the right endpoint of the interval if your stepsize doesn’t divide the interval evenly.
You Try1.7.
Edit the above code to use arange to create an array of numbers between 0 and 5 that are one-quarter apart. When you evaluate your code should print the list 0, 0.25, 0.5, 0.75, etc.
You can find more information about the package NumPy at numpy.org. Python’s wiki has a list of other commonly used modules and packages 1
wiki.python.org/moin/UsefulModules
.
Summary.
You can import a Python package and rename it by using import package as name.
You can call a function from a package using packagename.function
Numpy arrays can help us efficiently do computations.
The Numpy functions linspace() and arange() set up an array of values over an interval.
ExercisesExercises
1.
Research NumPy
Look up how to efficiently create a 15 element array of all \(1\)’s.
Use this to create a 15 element array of all \(100\)’s.
Solution.
There is more than one way to do this. One approach is to use the numpy function ones().
Once you have an array of all 1’s, you can multiply it by any number you want.
2.
The magnitude of an earthquake is measured using the Richter Scale, which is a logarithmic scale. The amount of energy \(E\) in ergs an earthquake releases can be determined from the magnitude \(M\) using the formula
website lets you download magnitude data for the 30 most recent earthquakes in the world with magnitude \(>2.5\text{.}\)
Use Numpy to determine how much energy has been released in the 30 most recent earthquakes. Note that you can copy and paste data from a csv file into SageMathCell. You will just need to then format that data for Numpy.