如何在NumPy中规范化数组?

时间:2022-03-27 01:51:28

I would like to have the norm of one NumPy array. More specifically, I am looking for an equivalent version of this function

我想拥有一个NumPy数组的规范。更具体地说,我正在寻找此功能的等效版本

def normalize(v):
    norm = np.linalg.norm(v)
    if norm == 0: 
       return v
    return v / norm

Is there something like that in skearn or numpy?

在卷须或笨蛋中有类似的东西吗?

This function works in a situation where v is the 0 vector.

此函数适用于v为0向量的情况。

5 个解决方案

#1


86  

If you're using scikit-learn you can use sklearn.preprocessing.normalize:

如果你正在使用scikit-learn,你可以使用sklearn.preprocessing.normalize:

import numpy as np
from sklearn.preprocessing import normalize

x = np.random.rand(1000)*10
norm1 = x / np.linalg.norm(x)
norm2 = normalize(x[:,np.newaxis], axis=0).ravel()
print np.all(norm1 == norm2)
# True

#2


28  

I would agree that it were nice if such a function was part of the included batteries. But it isn't, as far as I know. Here is a version for arbitrary axes, and giving optimal performance.

我同意如果这样的功能是包含电池的一部分,那就太好了。但据我所知,它并非如此。这是任意轴的版本,并提供最佳性能。

import numpy as np

def normalized(a, axis=-1, order=2):
    l2 = np.atleast_1d(np.linalg.norm(a, order, axis))
    l2[l2==0] = 1
    return a / np.expand_dims(l2, axis)

A = np.random.randn(3,3,3)
print(normalized(A,0))
print(normalized(A,1))
print(normalized(A,2))

print(normalized(np.arange(3)[:,None]))
print(normalized(np.arange(3)))

#3


11  

You can specify ord to get the L1 norm. To avoid zero division I use eps, but that's maybe not great.

您可以指定ord来获得L1规范。为了避免零分割,我使用eps,但这可能不是很好。

def normalize(v):
    norm=np.linalg.norm(v, ord=1)
    if norm==0:
        norm=np.finfo(v.dtype).eps
    return v/norm

#4


2  

If you have multidimensional data and want each axis normalized to itself:

如果您有多维数据并希望每个轴都标准化为自身:

def normalize(d):
    # d is a (n x dimension) np array
    d -= np.min(d, axis=0)
    d /= np.ptp(d, axis=0)
    return d

Uses numpys peak to peak function.

使用numpys峰峰功能。

#5


0  

There is also the function unit_vector() to normalize vectors in the popular transformations module by Christoph Gohlke:

在Christop Gohlke的流行转换模块中还有一个函数unit_vector()来规范化向量:

import transformations as trafo
import numpy as np

data = np.array([[1.0, 1.0, 0.0],
                 [1.0, 1.0, 1.0],
                 [1.0, 2.0, 3.0]])

print(trafo.unit_vector(data, axis=1))

#1


86  

If you're using scikit-learn you can use sklearn.preprocessing.normalize:

如果你正在使用scikit-learn,你可以使用sklearn.preprocessing.normalize:

import numpy as np
from sklearn.preprocessing import normalize

x = np.random.rand(1000)*10
norm1 = x / np.linalg.norm(x)
norm2 = normalize(x[:,np.newaxis], axis=0).ravel()
print np.all(norm1 == norm2)
# True

#2


28  

I would agree that it were nice if such a function was part of the included batteries. But it isn't, as far as I know. Here is a version for arbitrary axes, and giving optimal performance.

我同意如果这样的功能是包含电池的一部分,那就太好了。但据我所知,它并非如此。这是任意轴的版本,并提供最佳性能。

import numpy as np

def normalized(a, axis=-1, order=2):
    l2 = np.atleast_1d(np.linalg.norm(a, order, axis))
    l2[l2==0] = 1
    return a / np.expand_dims(l2, axis)

A = np.random.randn(3,3,3)
print(normalized(A,0))
print(normalized(A,1))
print(normalized(A,2))

print(normalized(np.arange(3)[:,None]))
print(normalized(np.arange(3)))

#3


11  

You can specify ord to get the L1 norm. To avoid zero division I use eps, but that's maybe not great.

您可以指定ord来获得L1规范。为了避免零分割,我使用eps,但这可能不是很好。

def normalize(v):
    norm=np.linalg.norm(v, ord=1)
    if norm==0:
        norm=np.finfo(v.dtype).eps
    return v/norm

#4


2  

If you have multidimensional data and want each axis normalized to itself:

如果您有多维数据并希望每个轴都标准化为自身:

def normalize(d):
    # d is a (n x dimension) np array
    d -= np.min(d, axis=0)
    d /= np.ptp(d, axis=0)
    return d

Uses numpys peak to peak function.

使用numpys峰峰功能。

#5


0  

There is also the function unit_vector() to normalize vectors in the popular transformations module by Christoph Gohlke:

在Christop Gohlke的流行转换模块中还有一个函数unit_vector()来规范化向量:

import transformations as trafo
import numpy as np

data = np.array([[1.0, 1.0, 0.0],
                 [1.0, 1.0, 1.0],
                 [1.0, 2.0, 3.0]])

print(trafo.unit_vector(data, axis=1))