如何应用numpy.linalg。矩阵的每一行的范数?

时间:2021-08-16 21:22:44

I have a 2D matrix and I want to take norm of each row. But when I use numpy.linalg.norm(X) directly, it takes the norm of the whole matrix.

我有一个二维矩阵,我想取每一行的范数。但是当我直接使用numpy.linalg.norm(X)时,它取整个矩阵的范数。

I can take norm of each row by using a for loop and then taking norm of each X[i], but it takes a huge time since I have 30k rows.

我可以使用for循环来取每一行的范数,然后取每一个X[I]的范数,但这需要很长时间,因为我有30k行。

Any suggestions to find a quicker way? Or is it possible to apply np.linalg.norm to each row of a matrix?

有什么建议可以找到更快的方法吗?或者可以应用np.linalg吗?矩阵的每一行的范数?

4 个解决方案

#1


61  

Note that, as perimosocordiae shows, as of NumPy version 1.9, np.linalg.norm(x, axis=1) is the fastest way to compute the L2-norm.

请注意,如perimosocordiae所示,NumPy版本1.9,np.linalg。范数(x,轴=1)是计算二阶范数最快的方法。


If you are computing an L2-norm, you could compute it directly (using the axis=-1 argument to sum along rows):

如果你正在计算一个l2 -范数,你可以直接计算它(使用轴=-1参数沿行求和):

np.sum(np.abs(x)**2,axis=-1)**(1./2)

Lp-norms can be computed similarly of course.

当然,lp -范数也可以用类似的方法计算。

It is considerably faster than np.apply_along_axis, though perhaps not as convenient:

它比np要快得多。apply_along_axis,尽管可能没有那么方便:

In [48]: %timeit np.apply_along_axis(np.linalg.norm, 1, x)
1000 loops, best of 3: 208 us per loop

In [49]: %timeit np.sum(np.abs(x)**2,axis=-1)**(1./2)
100000 loops, best of 3: 18.3 us per loop

Other ord forms of norm can be computed directly too (with similar speedups):

其他ord范数形式也可以直接计算(速度相似):

In [55]: %timeit np.apply_along_axis(lambda row:np.linalg.norm(row,ord=1), 1, x)
1000 loops, best of 3: 203 us per loop

In [54]: %timeit np.sum(abs(x), axis=-1)
100000 loops, best of 3: 10.9 us per loop

#2


38  

Resurrecting an old question due to a numpy update. As of the 1.9 release, numpy.linalg.norm now accepts an axis argument. [code, documentation]

更新一个旧的问题。在1.9版本中,numpy.linalg。norm现在接受axis参数。(代码、文档)

This is the new fastest method in town:

这是城里最快的新方法:

In [10]: x = np.random.random((500,500))

In [11]: %timeit np.apply_along_axis(np.linalg.norm, 1, x)
10 loops, best of 3: 21 ms per loop

In [12]: %timeit np.sum(np.abs(x)**2,axis=-1)**(1./2)
100 loops, best of 3: 2.6 ms per loop

In [13]: %timeit np.linalg.norm(x, axis=1)
1000 loops, best of 3: 1.4 ms per loop

And to prove it's calculating the same thing:

为了证明它的计算是一样的

In [14]: np.allclose(np.linalg.norm(x, axis=1), np.sum(np.abs(x)**2,axis=-1)**(1./2))
Out[14]: True

#3


7  

Much faster than the accepted answer is

比公认的答案快得多

numpy.sqrt(numpy.einsum('ij,ij->i', a, a))

Note the log-scale:

注意对数尺度:

如何应用numpy.linalg。矩阵的每一行的范数?


Code to reproduce the plot:

复制情节代码:

import numpy
import perfplot


def sum_sqrt(a):
    return numpy.sqrt(numpy.sum(numpy.abs(a)**2, axis=-1))


def apply_norm_along_axis(a):
    return numpy.apply_along_axis(numpy.linalg.norm, 1, a)


def norm_axis(a):
    return numpy.linalg.norm(a, axis=1)


def einsum_sqrt(a):
    return numpy.sqrt(numpy.einsum('ij,ij->i', a, a))


perfplot.show(
    setup=lambda n: numpy.random.rand(n, 3),
    kernels=[sum_sqrt, apply_norm_along_axis, norm_axis, einsum_sqrt],
    n_range=[2**k for k in range(15)],
    logx=True,
    logy=True,
    xlabel='len(a)'
    )

#4


6  

Try the following:

试试以下:

In [16]: numpy.apply_along_axis(numpy.linalg.norm, 1, a)
Out[16]: array([ 5.38516481,  1.41421356,  5.38516481])

where a is your 2D array.

a是二维数组。

The above computes the L2 norm. For a different norm, you could use something like:

以上计算为L2规范。对于不同的标准,您可以使用以下内容:

In [22]: numpy.apply_along_axis(lambda row:numpy.linalg.norm(row,ord=1), 1, a)
Out[22]: array([9, 2, 9])

#1


61  

Note that, as perimosocordiae shows, as of NumPy version 1.9, np.linalg.norm(x, axis=1) is the fastest way to compute the L2-norm.

请注意,如perimosocordiae所示,NumPy版本1.9,np.linalg。范数(x,轴=1)是计算二阶范数最快的方法。


If you are computing an L2-norm, you could compute it directly (using the axis=-1 argument to sum along rows):

如果你正在计算一个l2 -范数,你可以直接计算它(使用轴=-1参数沿行求和):

np.sum(np.abs(x)**2,axis=-1)**(1./2)

Lp-norms can be computed similarly of course.

当然,lp -范数也可以用类似的方法计算。

It is considerably faster than np.apply_along_axis, though perhaps not as convenient:

它比np要快得多。apply_along_axis,尽管可能没有那么方便:

In [48]: %timeit np.apply_along_axis(np.linalg.norm, 1, x)
1000 loops, best of 3: 208 us per loop

In [49]: %timeit np.sum(np.abs(x)**2,axis=-1)**(1./2)
100000 loops, best of 3: 18.3 us per loop

Other ord forms of norm can be computed directly too (with similar speedups):

其他ord范数形式也可以直接计算(速度相似):

In [55]: %timeit np.apply_along_axis(lambda row:np.linalg.norm(row,ord=1), 1, x)
1000 loops, best of 3: 203 us per loop

In [54]: %timeit np.sum(abs(x), axis=-1)
100000 loops, best of 3: 10.9 us per loop

#2


38  

Resurrecting an old question due to a numpy update. As of the 1.9 release, numpy.linalg.norm now accepts an axis argument. [code, documentation]

更新一个旧的问题。在1.9版本中,numpy.linalg。norm现在接受axis参数。(代码、文档)

This is the new fastest method in town:

这是城里最快的新方法:

In [10]: x = np.random.random((500,500))

In [11]: %timeit np.apply_along_axis(np.linalg.norm, 1, x)
10 loops, best of 3: 21 ms per loop

In [12]: %timeit np.sum(np.abs(x)**2,axis=-1)**(1./2)
100 loops, best of 3: 2.6 ms per loop

In [13]: %timeit np.linalg.norm(x, axis=1)
1000 loops, best of 3: 1.4 ms per loop

And to prove it's calculating the same thing:

为了证明它的计算是一样的

In [14]: np.allclose(np.linalg.norm(x, axis=1), np.sum(np.abs(x)**2,axis=-1)**(1./2))
Out[14]: True

#3


7  

Much faster than the accepted answer is

比公认的答案快得多

numpy.sqrt(numpy.einsum('ij,ij->i', a, a))

Note the log-scale:

注意对数尺度:

如何应用numpy.linalg。矩阵的每一行的范数?


Code to reproduce the plot:

复制情节代码:

import numpy
import perfplot


def sum_sqrt(a):
    return numpy.sqrt(numpy.sum(numpy.abs(a)**2, axis=-1))


def apply_norm_along_axis(a):
    return numpy.apply_along_axis(numpy.linalg.norm, 1, a)


def norm_axis(a):
    return numpy.linalg.norm(a, axis=1)


def einsum_sqrt(a):
    return numpy.sqrt(numpy.einsum('ij,ij->i', a, a))


perfplot.show(
    setup=lambda n: numpy.random.rand(n, 3),
    kernels=[sum_sqrt, apply_norm_along_axis, norm_axis, einsum_sqrt],
    n_range=[2**k for k in range(15)],
    logx=True,
    logy=True,
    xlabel='len(a)'
    )

#4


6  

Try the following:

试试以下:

In [16]: numpy.apply_along_axis(numpy.linalg.norm, 1, a)
Out[16]: array([ 5.38516481,  1.41421356,  5.38516481])

where a is your 2D array.

a是二维数组。

The above computes the L2 norm. For a different norm, you could use something like:

以上计算为L2规范。对于不同的标准,您可以使用以下内容:

In [22]: numpy.apply_along_axis(lambda row:numpy.linalg.norm(row,ord=1), 1, a)
Out[22]: array([9, 2, 9])