如何在NumPy中标准化数组? [英] How to normalize an array in NumPy?
本文介绍了如何在NumPy中标准化数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要一个NumPy数组的范数.更具体地说,我正在寻找此功能的等效版本
I would like to have the norm of one NumPy array. More specifically, I am looking for an equivalent version of this function
def normalize(v):
norm = np.linalg.norm(v)
if norm == 0:
return v
return v / norm
在skearn
或numpy
中是否存在类似的内容?
Is there something like that in skearn
or numpy
?
此功能在v
是0向量的情况下起作用.
This function works in a situation where v
is the 0 vector.
推荐答案
If you're using scikit-learn you can use 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
这篇关于如何在NumPy中标准化数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文