正确使用numpy.vecotrize(Using numpy.vecotrize correctly)

系统教程 行业动态 更新时间:2024-06-14 16:57:41
正确使用numpy.vecotrize(Using numpy.vecotrize correctly)

我正在努力使用np.vectorize这个代码片段

def to_homogenous(x): ''' Convert a point x to homogenous coordinates. ''' return np.hstack((x,np.array([1]))).flatten() vec_to_homo = np.vectorize(to_homogenous) coords = vec_to_homo([0,1])

这会导致以下错误:

ValueErrorTraceback(最近一次调用最后) 在() ----> 1 coords1 = vec_to_homo([0,1])

调用中的 /home/lib/python2.7/site-packages/numpy/lib/function_base.pyc(self,* args,** kwargs) 2753 vargs.extend([kwargs [_n] for _n in name]) 2754 - > 2755返回self._vectorize_call(func = func,args = vargs) 2756 2757 def _get_ufunc_and_otypes(self,func,args):

/vectorize_call(self,func,args)中的/home/lib/python2.7/site-packages/numpy/lib/function_base.pyc 2832 2833如果ufunc.nout == 1: - > 2834 res = array(outputs,copy = False,subok = True,dtype =χ[0]) 2835其他: 2836 res = tuple([array(x,copy = False,subok = True,dtype = t)

ValueError:使用序列设置数组元素。

我了解错误消息,但我不能为我的生活找到我应该如何改变我的功能/调用矢量化来解决这个问题。 如果我只传递一个整数,函数会产生正确的结果,即vec_to_homo(0)产生[0,1] 。

我的代码片段与上面链接的文档中提供的示例非常接近,所以我真的不知道发生了什么问题。

请帮忙。 谢谢!

I'm struggling with this code snippet using np.vectorize

def to_homogenous(x): ''' Convert a point x to homogenous coordinates. ''' return np.hstack((x,np.array([1]))).flatten() vec_to_homo = np.vectorize(to_homogenous) coords = vec_to_homo([0,1])

This results in the following Error:

ValueErrorTraceback (most recent call last) in () ----> 1 coords1 = vec_to_homo([0,1])

/home/lib/python2.7/site-packages/numpy/lib/function_base.pyc in call(self, *args, **kwargs) 2753 vargs.extend([kwargs[_n] for _n in names]) 2754 -> 2755 return self._vectorize_call(func=func, args=vargs) 2756 2757 def _get_ufunc_and_otypes(self, func, args):

/home/lib/python2.7/site-packages/numpy/lib/function_base.pyc in _vectorize_call(self, func, args) 2832 2833 if ufunc.nout == 1: -> 2834 res = array(outputs, copy=False, subok=True, dtype=otypes[0]) 2835 else: 2836 res = tuple([array(x, copy=False, subok=True, dtype=t)

ValueError: setting an array element with a sequence.

I understand the error message but I cannot for the life of me find how I should change my function/the call to vectorize to solve this problem. The function yields correct results if I pass only a single integer, i.e. vec_to_homo(0) yields [0,1].

My snippet is very close to the example provided in the documentation linked above, so I really don't get what is going wrong.

Please help. Thanks!

最满意答案

你的问题和这个问题一样 。 np.vectorize仅适用于将浮点数映射到浮点数的函数,而不是浮点数到Numpy数组。

另外, np.vectorize不会加速代码执行。 您可以在函数内部创建自己的逻辑来处理列表,并使用理解,循环或类似方法返回2D数组。

Your problem is as in this question. np.vectorize is only appropriate for functions that map floats to floats, not floats to Numpy arrays.

Additionally, np.vectorize does not speed up code execution. You can create your own logic inside the function to process a list and return a 2D array using comprehension, looping or similar.

更多推荐

本文发布于:2023-04-13 12:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/25ad2cb5a6ac74b84ec9898648a251d4.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正确   numpy   correctly   vecotrize

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!