简体   繁体   English

Numpy vectorize()使整个数组变平

[英]Numpy vectorize() is flattening the whole array

My input is a numpy array of tuples 我的输入是一个元组的numpy数组

values = np.array([(4, 5, 2, 18), (4, 7, 3, 8)])

and my function is as follows: 我的功能如下:

def outerFunc(values):
    print(values)
    def innerFunc(values):
        print(values)
        mean = np.mean(values)
        result = 0
        for i in range(len(values)):
            result += math.pow(values[i] - mean, 2)
        return result

    if isinstance(values, np.ndarray):
        return np.vectorize(innerFunc)(values)
    else:
        return innerFunc(values)

Although I want to vectorize over 1 dimension, ie, one tuple is executed inside the innerFunc, but my output is as follows: 尽管我想向量化1维,即在innerFunc内部执行一个元组,但是我的输出如下:

[[ 4  5  2 18]
 [ 4  7  3  8]]
4
...

Which means the vectorize function is vectorizing over 2 dimensions, and I am getting the following error: 这意味着vectorize函数正在2维上进行矢量化,并且出现以下错误:

for i in range(len(values)):
TypeError: object of type 'numpy.int64' has no len()

What changes to make so that the output is: 进行什么更改,以使输出为:

[[ 4  5  2 18]
 [ 4  7  3  8]]
[4  5  2 18]
...

something like this 像这样的东西

Thank you. 谢谢。

EDIT 编辑

It is working as accepted when the tuples are different length, can anyone explain this, 当元组长度不同时,它可以正常工作,任何人都可以解释一下,

eg, my input is 例如,我的输入是

np.array([(4, 5, 2, 18), (4, 7, 3,)])

and the function prints 和功能打印

[(4, 5, 2, 18) (4, 7, 3)]
(4, 5, 2, 18)
(4, 7, 3)

and the returned value is 返回的值是

[158.75         8.66666667]

So, only when all the tuples are the same length, the function treats them as numbers. 因此,仅当所有元组的长度相同时,该函数才将它们视为数字。

Thank you. 谢谢。

In [1]: values = np.array([(4, 5, 2, 18), (4, 7, 3, 8)])                        
In [2]: values                                                                  
Out[2]: 
array([[ 4,  5,  2, 18],
       [ 4,  7,  3,  8]])
In [3]: values.shape                                                            
Out[3]: (2, 4)
In [4]: x=np.array([(4, 5, 2, 18), (4, 7, 3,)])                                 
In [5]: x                                                                       
Out[5]: array([(4, 5, 2, 18), (4, 7, 3)], dtype=object)
In [6]: x.shape                                                                 
Out[6]: (2,)

values is a 2d numeric array. values是一个二维数字数组。 np.vectorize passes each of the 8 elements, one at a time, to your inner function. np.vectorize将8个元素中的每个元素一次传递给您的内部函数。 It does not iterate by rows. 它不会按行迭代。

x is a 1d array with 2 elements (tuples). x是具有2个元素(元组)的1d数组。 vectorize will pass each of those tuples to your inner. vectorize会将每个元组传递到您的内部。

Don't use vectorize when a simple iteration would work - it's slower and harder to use right. 当简单的迭代可行时,不要使用vectorize -使用起来会更慢,更难。

And look at your arrays after you create them, making sure you understand the shape and dtype. 并在创建数组后查看它们,确保您了解形状和dtype。 Don't make assumptions. 不要做假设。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM