Data Analysis with Python: From Raw Data to Real-World Insights
BOOK 2 · NUMPY · CHAPTER 3

Universal Functions

On this page

Once data is stored in arrays, the next natural step is to perform calculations on it. NumPy provides a large collection of built-in mathematical tools called universal functions, often referred to as uFuncs (short for universal functions).

A universal function performs element-wise operations on arrays — meaning it applies the same operation to each element independently, without the need for loops. These operations avoid writing an explicit Python loop for each element. Their performance depends on array size, dtype, memory layout and the operation; measure a relevant workload before making a speed claim.

For example, adding two arrays adds each corresponding element:

import numpy as np

a = np.array([1, 2, 3])
b = np.array([10, 20, 30])

print(a + b)
[11 22 33]

For standard numerical dtypes, NumPy generally performs these loops in compiled implementations. Object arrays can invoke Python objects and have different performance characteristics. The NumPy ufunc reference defines the interface. We will also use reductions such as mean and standard deviation; these are useful NumPy operations but are not themselves ufunc objects.

Square Roots with np.sqrt()

The square root is one of the most fundamental mathematical operations. It answers the question: what number multiplied by itself gives this value?

NumPy’s np.sqrt() function calculates the square root of each element in an array.

values = np.array([1, 4, 9, 16])
roots = np.sqrt(values)
print(roots)
[1. 2. 3. 4.]

Each element has been replaced by its square root: 1=1,4=2,9=3,16=4\sqrt{1} = 1, \quad \sqrt{4} = 2, \quad \sqrt{9} = 3, \quad \sqrt{16} = 4

In data analysis, square roots are often used when measuring distances or variability. For instance, in literary stylometry, the Euclidean distance between word-frequency vectors is computed using square roots. Similarly, in statistics, the standard deviation is the square root of variance — showing how spread out data points are around their mean.

Natural Logarithm with np.log()

In mathematics, the logarithm expresses the power to which a given base must be raised to produce a specific number.

logb(x)=ymeans thatby=x\log_b(x) = y \quad \text{means that} \quad b^y = x

When we talk about the natural logarithm, the base is e (approximately 2.718).

log(x)=ymeans thatey=x\log(x) = y \quad \text{means that} \quad e^y = x

In NumPy, the natural logarithm of each element can be computed with np.log():

values = np.array([1, 4, 9, 16])
logs = np.log(values)
print(logs)
[0.         1.38629436 2.19722458 2.77258872]

Each output value represents the logarithm of the input using base e. For instance, ln(1)=0andln(16)≈2.77\ln(1) = 0 \quad \text{and} \quad \ln(16) \approx 2.77

Logarithms are especially useful when working with data that grows or changes very quickly. For a hypothetical count following N(t)=N0ertN(t)=N_0 e^{rt} with N0>0N_0>0, taking its natural logarithm gives logN(t)=logN0+rt\log N(t)=\log N_0+rt, a straight line in time. This is an algebraic example, not evidence that a real archive grows exponentially. A logarithm changes the scale and interpretation; it does not remove random variation or guarantee a linear relationship. In linguistic data, logarithmic scaling also helps reduce the effect of very frequent words (like the or and) when analyzing vocabulary diversity.

Arithmetic Mean with np.mean()

The mean, or average, is one of the simplest and most common summary statistics. It is the sum of the values divided by their number. It need not equal an observed value or the middle ordered observation; that latter idea leads to the median.

NumPy provides np.mean() to calculate the arithmetic mean of all elements in an array:

values = np.array([1, 4, 9, 16])
average = np.mean(values)
print(average)
7.5

Here, NumPy adds all the numbers together (1 + 4 + 9 + 16 = 30) and divides by the number of elements (4), giving 7.5.

In data analysis, the mean is often the first step in exploring a dataset. For example, we might compute the average word length in a novel, the average temperature in a climate dataset, or the average sentiment score of political speeches. The mean provides a baseline from which deviations and trends can be measured.

Combining Universal Functions

The real power of NumPy’s universal functions comes from combining them to perform complex calculations in just a few lines of code.

For example, suppose we have a dataset of squared values, and we want to calculate their mean and then the square root of that mean:

data = np.array([1, 4, 9, 16])
result = np.sqrt(np.mean(data))
print(result)
2.7386127875258306

In this example, np.mean(data) calculates the average of [1, 4, 9, 16], which is 7.5. Then np.sqrt(7.5) takes the square root of that mean, resulting in approximately 2.74. Because the inputs here are already squares, this is the root mean square of the underlying values 1, 2, 3 and 4. It differs from their arithmetic mean, 2.5. For unsquared measurements, root mean square is computed as np.sqrt(np.mean(measurements**2)); taking a square root of their mean alone is a different operation.

Other Useful Numerical Functions

Distinguish elementwise ufuncs from reductions that summarize several values. Examples include:

  • np.exp() — Calculates the exponential of each element, meaning it raises e (about 2.718) to the power of each number. This is often used when modeling growth processes, such as population increase or text frequency over time.
  • np.sin(), np.cos(), np.tan() — Compute trigonometric functions that describe waves, cycles, and rotations. They are useful when analyzing periodic data like seasonal trends, sound signals, or recurring historical events.
  • np.abs() — Returns the absolute value of each element, converting negative numbers to positive. This is useful when only magnitudes matter, such as measuring distances or sentiment intensity regardless of direction.
  • np.sum() — Adds up all the numbers in an array and returns their total. It’s a simple but powerful function used in everything from counting occurrences to summing frequencies in text or numerical data..
  • np.std() — Calculates the standard deviation, which measures how much values vary from the mean. A higher standard deviation indicates more spread out data; a lower one means the data points are closer together — useful in comparing variability between datasets.

Let’s see an example combining a few of these:

data = np.array([-3, -1, 0, 2, 4])
print(np.abs(data))   # absolute values
print(np.exp(data))   # exponential
print(np.std(data))   # standard deviation
[3 1 0 2 4]
[4.97870684e-02 3.67879441e-01 1.00000000e+00 7.38905610e+00
 5.45981500e+01]
2.4166091947189146

These operations help us understand, transform, and model data. For instance, in sentiment analysis, we might use the mean and standard deviation to understand variability in emotion scores, or use logarithmic scaling to handle skewed distributions.

Universal functions transform corresponding elements; reductions summarize values along specified axes. Predict the shape and unit of the result before combining them. The next lesson develops weights, normalization and domain checks.

Teaching edition · verified experiments · PDF and EPUB editions not generated.