Mapping and reduction functions in numpy#

# initialization: make numpy available
import numpy as np

Numpy mapping functions#

A vast number of numpy functions work by applying a mathematical mapping on each element of the array. Here we list some of the familiar ones and illustrate each group with some examples

Power, exponential and logarithmic functions#

  • np.sqrt() for calculating square root

  • np.exp() for exponentiation

  • np.log() for natural logarithm

  • np.log10() for base-10 logarithm

  • np.log2() for base-2 logrithm

Examples:

x = np.array([1, 2, 3])
np.exp(x)
array([ 2.71828183,  7.3890561 , 20.08553692])
x = np.array([1, 10, 0.1])
np.log10(x)
array([ 0.,  1., -1.])

Trigonometric functions#

Note: in all these functions angles are expressed in radians:

  • np.sin(): sine function

  • np.cos(): cosine function

  • np.tan(): tangent function

  • np.arcsin(): arcsine (inverse sine) function

  • np.arccos(): arccosine (inverse consine) function

  • np.arctan(): arctangent (inverse tangent) function

  • np.arctan2(): arctangent function accepting both x and y as arguments

Moreover, the following convenient helper functions are defined:

  • deg2rad(): convert degrees to radians

  • rad2deg(): convert radians to degrees

Examples:

np.sin(3.14)
0.0015926529164868282
np.arctan2(-1, -np.sqrt(3))
-2.6179938779914944

Rounding functions#

  • np.round(): round values to fixed decimal point

  • np.floor(): round down to the nearest integer

  • np.ceil(): round up to the nearest integer

Examples:

x = np.array([3.1415926, 2.718281828])
np.round(x, 3)
array([3.142, 2.718])
np.ceil(x)
array([4., 3.])

Miscellaneous functions#

  • np.fabs(): absolute value

Example:

x = np.array([-2, -1, 0, 1, 2])
np.fabs(x)
array([2., 1., 0., 1., 2.])

Remarks: constants in numpy#

In addition to functions, numpy also come with a few constants. The most notable ones are np.pi (for \(\pi\)) and np.e (for the natural / Euler / exponential number \(e\)). Moreover, there is also the “not a number” value np.nan. Note that mathematical operations on np.nan generally results in np.nan

np.sin(np.pi)
1.2246467991473532e-16
np.log(np.e)
1.0
np.nan * 2
nan

Reduction functions#

Other than mapping functions, numpy also implements a number of reduction functions. In brief, these are functions that “reduce” a (1-dimensional) numpy array to a single number. Below we list some familiar ones:

Statistics functions#

  • np.mean(): the average of an array, \(\overline{x} = (x_1 + \ldots + x_n) / n\)

  • np.var(): the variance of an array, \(\operatorname{var}(\{x_i\}) = [(x_1 - \overline{x})^2 + \ldots + (x_n - \overline{x})^2]/n \)

  • np.std(): the standard deviation of an array, \(\operatorname{std}(\{x_i\}) = \sqrt{\operatorname{var}(\{x_i\})}\)

  • np.median(): the median of an array (the element that ranks half the way between the smallest and the largest values)

Examples:

x = np.array([7, 4, 6, 5, 2])
np.mean(x)
4.8
np.std(x)
1.7204650534085253

Accumulation functions#

  • np.prod(): product of all elements in the array

  • np.sum(): sum of all elements in the array

Examples:

x = np.array([1, 1.5, 2, 2.5, 3])
np.prod(x)
22.5
np.sum(x)
10.0

Extremum functions#

  • np.max(): finding the maximum value of an array

  • np.min(): finding the minimum value of an array

  • np.argmax(): finding the index at which the maximum is attained

  • np.argmin(): finding the index at which the minimum is attained

Examples:

x = np.array([2, 8, 6, 3, 4])
np.max(x)
8
np.argmax(x)
1

Dealing with nan values in reduction#

As noted, mathematical operations on np.nan generally results in np.nan. This includes reduction functions. For example:

x = np.array([2, 8, 6, np.nan, 4])
np.mean(x)
nan

However, np.nan is often used to represent missing values, and when calcaluating statistics involving missing values it is often appropriate to simply omit these values instead of voiding the entire calculation. Many reduction functions in numpy have an “nan-omitted” variation precisely for such a purpose. The names of these take the form of “nan” followed by the original names of the functions. For example, the nan-omitted variation of np.mean() is np.nanmean():

np.nanmean(x)
5.0

Compare with the result from manually removing the nan value:

np.mean(x[~np.isnan(x)])
5.0

Functions similar to np.nanmean() include:

  • np.nanmean()

  • np.nanmedian()

  • np.nanvar()

  • np.nanstd()

  • np.nanmin()

  • np.nanmax()

  • np.nansum()

Sorting functions#

Some functions in numpy do not fall neatly into the baskets introduced so far. The most important ones are the sorting functions:

  • np.sort(): sort an array (from smallest to largest, by default)

  • np.argsort(): sort the indices of an array by the value of the corresponding element

Example:

x = np.array([2, 8, 6, 3, 4])
np.sort(x)
array([2, 3, 4, 6, 8])
np.argsort(x)
array([0, 3, 4, 2, 1], dtype=int64)