HOWTO · Python

NumPy Correlation Function

Learn how to calculate Pearson correlation with NumPy, read the returned coefficient matrix, and visualize paired data.

On this page

Use NumPy’s np.corrcoef() function to calculate Pearson correlation coefficients for paired numeric data. The off-diagonal entries in its result show the coefficient between the arrays; an optional scatter plot can help you inspect their pattern.

Correlation in NumPy

A correlation coefficient summarizes the direction and strength of an association between two variables. A positive coefficient describes values that tend to increase together, while a negative coefficient describes one variable tending to decrease as the other increases. A coefficient near zero indicates little linear association, but does not rule out a nonlinear relationship. Correlation describes association, not proof that one variable causes changes in another.

Pearson, Kendall, and Spearman are different measures of association. np.corrcoef() calculates Pearson correlation, which describes linear association using covariance scaled by the variables’ standard deviations. It does not calculate Kendall or Spearman correlation; use a suitable function from SciPy for those measures. For Pearson correlation in NumPy, use the exact function name np.corrcoef().

Use the np.corrcoef() Function to Implement Correlation in Python

Pass two arrays containing paired observations to np.corrcoef(). The arrays should have matching lengths so each position represents one observation pair. The function returns a two-by-two matrix: each off-diagonal entry is the Pearson coefficient between the arrays, and those two entries are equal. When both correlations are defined, the diagonal entries are 1 because each variable is perfectly correlated with itself.

Here, the arrays contain ten paired values. Run this example to calculate their coefficient:

import numpy as np

a = np.arange(20, 30)
b = np.array([8, 12, 29, 33, 60, 48, 21, 44, 78, 96])
x = np.corrcoef(a, b)
print(x)

Output:

[[1.         0.82449488]
 [0.82449488 1.        ]]

The two off-diagonal entries are approximately 0.82, indicating a positive linear association in these observations. Read either entry to get the coefficient between the arrays; the repeated value reflects the symmetry of the correlation matrix. The diagonal is 1 for variables with defined, nonzero variation. If an input is constant, its standard deviation is zero and its Pearson correlation is undefined, so the result can contain NaN rather than a usable coefficient. Check your data and handle such results before using them in later calculations.

Use Correlation With the Matplotlib Library to Make Correlation Graphs

A scatter plot places each paired observation at its corresponding (x, y) coordinate. It provides a visual view of the data behind a coefficient and can reveal clusters or curved patterns that a single linear summary may not describe well. Use the plot together with the calculated coefficient: a visible upward trend is consistent with positive correlation, but a graph alone does not calculate or establish the numeric value.

The following optional example prints the coefficient matrix and displays the paired observations. It uses Matplotlib’s pyplot interface; install NumPy and Matplotlib in your Python environment before running it. The plot window depends on the environment used to display figures.

import matplotlib.pyplot as plt
import matplotlib
import numpy as np

x = np.arange(20, 30)
y = np.array([8, 12, 29, 33, 60, 48, 21, 44, 78, 96])
print(np.corrcoef(x, y))

# %matplotlib inline
matplotlib.style.use("ggplot")
plt.scatter(x, y)
plt.show()

Scatter plot of paired observations with a general upward trend

In this sample, the points generally rise as x increases, matching the positive coefficient from np.corrcoef(). Real datasets can show more varied patterns, so inspect the paired values as well as the coefficient when interpreting an association.