1.5 The NumPy Library
In Python, a software library is a reusable collection of code. NumPy, pronounced "Num-Pie" for Numerical Python is the fundamental package for scientific computing with Python. https://numpy.org/
Unlike print which is a built-in function, we need to import numpy before using its functions such as numpy.absolute. Importing refers to allowing Python to access the script from another Python module or library. The numpy library provides access to an extensive mathematical functions.
import numpy
To access one of its functions, specify the name (or its alias) of the library and the name of the function, separated by a dot(period).
Some Python Functions (Methods) from the numpy library
| Name | Method | Example | Results (rounded) |
|---|---|---|---|
| Square root |
numpy.sqrt(x)
|
2.23606797749979
| |
| Absolute value |
numpy.absolute(x)
| -5 |
5
|
| Rounding |
numpy.round(x,2)
| 1.3333 | 1.33 |
| the number pi |
numpy.pi
|
3.141592653589793
| |
| e |
numpy.e
|
e
|
2.718281828459045
|
| e raised to |
numpy.exp(x)
|
7.38905609893065
| |
| Natural log |
numpy.log(x)
|
1.6094379124341003
| |
| Common log |
numpy.log10(x)
|
0.9542425094393249
|
Example: Given the formula , evaluate , if , , and .
Option 1: substitute all into the formula
import numpy as np
np.absolute(-2.575)*(0.34*(1-0.34)/800)**(1/2)
Show expected output
0.043126445627943884Option 2: use some variables to break-down the steps
import numpy as np
a = 0.34*(1-.34)/800
b = a**(1/2)
np.absolute(-2.575)*b
Show expected output
0.043126445627943884Option 3: declare z, p and n as variables and assign the given values first then use the formula to evaluate E using these variables:
import numpy as np
z = -2.575
p = 0.34
n = 800
E = np.absolute(z)*(p*(1-p)/n)**(1/2)
E
Show expected output
0.043126445627943884Example: Round the value of to two decimal places.
import numpy as np
np.round(1/3,2)
Show expected output
0.33Example: Round the value of to three decimal places.
import numpy as np
np.round(2**(1/2),3)
Show expected output
1.414Example: What is the (rounded) value of in Python?
- Click
+ Code - Type
import numpy - Type
numpi.pi - Click
playbutton to execute the code 3.141592653589793(displayed)
import numpy
numpy.pi
Show expected output
3.141592653589793Example: Area of a circle with radius r is given by the formula
- Find the area of a circle with radius 5.
- Round your answer to a whole number.
- Type
import numpy - Type
numpy.pi*5**2on the second line - Click
playto execute the code
import numpy
numpy.pi*5**2
Show expected output
78.53981633974483import numpy
Area = numpy.pi*5**2
round(Area,0)
Show expected output
79.0Example: what is the rounded value of ?
import numpy
numpy.exp(3)
Show expected output
20.085536923187668Example: what is the rounded value of ?
numpy.sqrt(3)
Show expected output
1.7320508075688772Example: use ** to find
3**(1/2)
Show expected output
1.7320508075688772Adapted from Python for Introductory Statistics, by Simon Aman (Truman College, City Colleges of Chicago), licensed under CC BY 4.0. Changes were made: reformatted as an accessible XYZ web edition with live in-browser code cells. License: CC-BY-4.0.