Apply along axis

Apply along axis#

There is function numpy.apply_along_axis, this is function that allow to apply something along selected axis.

import numpy as np
from scipy.spatial.distance import correlation

Keep dimentions#

This keepaxis function means that if you apply it to any column (axis=0), you’ll get results as columns - it doesn’t matter what shape the output is in.

The following example shows how numpy.apply_along_axis will display the same output, but in the case of different axis options.

print("=====axis0=====")
display(np.apply_along_axis(
    func1d=lambda x: np.array([1,2,3]),
    axis=0,
    arr=np.zeros(shape=(3,3))
))
print("=====axis1=====")
display(np.apply_along_axis(
    func1d=lambda x: np.array(np.array([1,2,3])),
    axis=1,
    arr=np.zeros(shape=(3,3))
))
=====axis0=====
array([[1, 1, 1],
       [2, 2, 2],
       [3, 3, 3]])
=====axis1=====
array([[1, 2, 3],
       [1, 2, 3],
       [1, 2, 3]])

So in the case of axis=0 the array was interpreted as columns and in the case of axis=1 the array was interpreted as rows.

Combinations#

You can combine rows/columns of matrices in binary functions. The following examples use numpy.apply_along_axis to calculate correlations between rows/columns.

Here is an example that computes the matrix \(\left[r_{ij}\right]\), where \(r_{ij}\) is the Pearson correlation distance between the \(i\)-th row of arr1 and the \(j\)-th row of arr2.

arr1 = np.array([
    [10,20,30,20],
    [30,10,22,13]
])
arr2 = np.array([
    [0.1,0.2,0.2,0.3],
    [0.3,0.3,0.3,0.7],
    [0.1,0.3,0.2,0.5]
])

np.apply_along_axis(
    func1d = lambda row1: np.apply_along_axis(
        func1d=lambda row2: correlation(row1, row2),
        axis=1, arr=arr2
    ),
    axis=1, arr=arr1
)
array([[0.5       , 1.        , 0.76095428],
       [1.76525353, 1.42267702, 1.80166638]])

The following example shows a matrix \(\left[r_{i,j}\right]\), where \(r_{i,j}\) is the Pearson correlation coefficient between the \(j\)-th column of arr1 and the \(i\)-th column of arr2.

arr1 = np.array([
    [10,20,30],
    [30,10,22],
    [30,12,23]
])
arr2 = np.array([
    [0.1,0.2,0.2,0.3,0.4,0.3],
    [0.3,0.3,0.3,0.7,0.5,0.1],
    [0.5,0.1,0.3,  1,0.3,0.2]
])

np.apply_along_axis(
    func1d = lambda row1: np.apply_along_axis(
        func1d=lambda row2: correlation(row1, row2),
        axis=0, arr=arr2
    ),
    axis=0, arr=arr1
)
array([[0.1339746 , 1.75592895, 1.80295507],
       [1.        , 1.18898224, 1.11470787],
       [0.        , 1.98198051, 1.99339927],
       [0.09580557, 1.807183  , 1.84923193],
       [1.        , 1.18898224, 1.11470787],
       [1.8660254 , 0.05508882, 0.08233706]])

Here it can be a bit confusing that the result of the outer np.apply_along_axis is stored as columns. But this is explained by the fact that the function is applied by columns - that’s why the result is by columns.