python—计算数组中每一行的单位向量

kse8i1jr  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(308)

我有一个大的(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

你怎么能做到这一点呢?

hpcdzsge

hpcdzsge1#

您可以使用广播操作,例如:

A /= np.linalg.norm(A, axis=1)[:,None]

# or

A /= np.linalg.norm(A, axis=1).reshape(4,1)

这两者都将使数组具有 (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.dtype` :

A = A/np.linalg.norm(A, axis=1)[:,None]


* 使用 `float64` 您只需在其中一个数字上添加逗号即可:

A = np.array([[50.,14],[26,11],[81,9],[-11,-19]])

您也可以使用 `normalize` scikit learn的预处理工具箱的功能:

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]])

相关问题