我有一个大的(nx dim)数组,每一行都是一个空间中的向量(不管维度是什么,但让我们在2d中进行):
import numpy as np
A = np.array([[50,14],[26,11],[81,9],[-11,-19]])
A.shape
(4,2)
我想快速计算每一行的单位向量。
N = np.linalg.norm(A, axis=1)
# something like this, but for each row:
A /= N # not working:
# ValueError: operands could not be broadcast together
# with shapes (4,2) (4,) (4,2)
# or in a pandas-like manner:
np.divide(A, N, axis=1, inplace=True) # not working either
你怎么能做到这一点呢?
1条答案
按热度按时间hpcdzsge1#
您可以使用广播操作,例如:
这两者都将使数组具有
(4,1)
而不是(4,)
但是要小心,A.dtype
应该是float64
*否则,在使用时将遇到此错误ufunc
```A /= np.linalg.norm(A, axis=1)[:,None]
TypeError: ufunc 'true_divide' output (typecode 'd') could not be coerced to
provided output parameter (typecode 'l') according to the casting
rule ''same_kind''
A = A/np.linalg.norm(A, axis=1)[:,None]
A = np.array([[50.,14],[26,11],[81,9],[-11,-19]])
import sklearn
sklearn.version # 0.24.2
from sklearn.preprocessing import normalize
normalize(A, norm="l2", axis=1)
array([[ 0.962964 , 0.2696299],
[ 0.9209673, 0.38964 ],
[ 0.9938837, 0.1104315],
[-0.5010363, -0.8654263]])
as per the doc, you can set the copy flag to False to perform inplace row
normalization and avoid a copy (if the input is already a numpy array or a
scipy.sparse CSR matrix and if axis is 1):
normalize(A, norm="l2", axis=1, copy=False)
array([[ 0.962964 , 0.2696299],
[ 0.9209673, 0.38964 ],
[ 0.9938837, 0.1104315],
[-0.5010363, -0.8654263]])