numpy vectorize在(2,)数组上使用
我有一个(m,2)
的数组数组,我想使用以下功能将其转换为(m,1)
的形状。
def func(x):
if x == [1., 1.]:
return 0.
if x == [-1., 1.] or x == [-1., -1.]:
return 1.
if x == [1., -1.]:
return 2.
我希望将其应用于(M,2)内的每个
数组导致(2,)
向量,(M,1)
array。我尝试使用numpy.Dectorize
,但是似乎该函数在数组的每个元素中都应用(在通常的情况下是有意义的)。所以我没有应用它。
我的目的不是用于循环。谁能帮我吗?谢谢。
I have a numpy array of (m, 2)
and I want to transform it to shape of (m, 1)
using a function below.
def func(x):
if x == [1., 1.]:
return 0.
if x == [-1., 1.] or x == [-1., -1.]:
return 1.
if x == [1., -1.]:
return 2.
I want this for applied on each (2,)
vector inside the (m, 2)
array resulting an (m, 1)
array. I tried to use numpy.vectorize
but it seems that the function gets applied in each element of a array (which makes sense in general purpose case). So I have failed to apply it.
My intension is not to use for loop. Can anyone help me with this? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
输出:
Output: