Login
📚 Python for Introductory Statistics
Chapters ▾

1.5 The NumPy Library

Watch demo video

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

NameMethodExampleResults (rounded)
Square root numpy.sqrt(x) 5 2.23606797749979
Absolute value numpy.absolute(x) -5 5
Rounding numpy.round(x,2) 1.33331.33
the number pi numpy.pi π 3.141592653589793
e numpy.e e 2.718281828459045
e raised to numpy.exp(x)   e 2 7.38905609893065
Natural log numpy.log(x) ln 5 1.6094379124341003
Common log numpy.log10(x) log ( 9 ) 0.9542425094393249

Example: Given the formula  E=|z|p(1p)n, evaluate  E, if  z=2.575,  p=0.34, and  n=800.

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.043126445627943884

Option 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.043126445627943884

Option 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.043126445627943884

Example: Round the value of 13 to two decimal places.

import numpy as np
np.round(1/3,2)
Show expected output
0.33

Example: Round the value of 2 to three decimal places.

import numpy as np
np.round(2**(1/2),3)
Show expected output
1.414

Example: What is the (rounded) value of π in Python?

  1. Click + Code
  2. Type import numpy
  3. Type numpi.pi
  4. Click play button to execute the code
  5. 3.141592653589793 (displayed)
import numpy
numpy.pi
Show expected output
3.141592653589793

Example: Area of a circle with radius r is given by the formula  A=πr2

  1. Type import numpy
  2. Type numpy.pi*5**2 on the second line
  3. Click play to execute the code
import numpy
numpy.pi*5**2   
Show expected output
78.53981633974483
import numpy
Area = numpy.pi*5**2 
round(Area,0)
Show expected output
79.0

Example: what is the rounded value of e3?

import numpy
numpy.exp(3)
Show expected output
20.085536923187668

Example: what is the rounded value of 3?

numpy.sqrt(3)
Show expected output
1.7320508075688772

Example: use ** to find 3

3**(1/2)
Show expected output
1.7320508075688772

Adapted 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.