简体   繁体   English

Python-将元组中的数组转换为普通数组

[英]Python - Convert the array in a tuple to just a normal array

I have a signal where I want to find the average height of the values. 我有一个信号想要找到这些值的平均高度。 This is done by finding the zero crossings and calculating the max and min between each zero crossing, then averaging these values. 这是通过找到零交叉点并计算每个零交叉点之间的最大值和最小值,然后取这些值的平均值来完成的。

My problem occurs when I want to use np.where() to find where the signal is crossing zero. 当我想使用np.where()查找信号越过零的位置时,会发生我的问题。 When I use np.where() I get the result in a tuple, but I want it in an array where I can count the amount of times zero is crossed. 当我使用np.where()我将结果存储在一个元组中,但我希望将其存储在一个数组中,在该数组中我可以计算出零被越过的次数。

I am new to Python and coming from Matlab it is a bit confusing with all the different classes. 我是Python的新手,来自Matlab,它与所有不同的类有点混淆。 As you can see, I get an error because nu = len(zero_u) gives 1 as a result, because the whole array is written in a tuple as one element. 如您所见,我得到一个错误是因为nu = len(zero_u)结果为1 ,因为整个数组作为一个元素写在一个元组中。

Any ideas how to go around this? 任何想法如何解决这个问题?

The code looks like this: 代码如下:

import numpy as np


def averageheight(f):

    rms = np.std(f)

    f = f + (rms * 10**-6)

    # Find zero crossing

    fsign = np.sign(f)
    fdiff = np.diff(fsign)

    zero_u = np.asarray(np.where(fdiff > 0)) + 1
    zero_d = np.asarray(np.where(fdiff < 0)) + 1

    nu = len(zero_u)
    nd = len(zero_d)
    value_max = np.zeros((nu, 1))
    value_min = np.zeros((nu, 1))
    imaxvec = np.zeros((nu, 1))
    iminvec = np.zeros((nu, 1))

    if (nu > 2) and (nd > 2):

        if zero_u[0] > zero_d[0]:
            zero_d[0] = []

        nu = len(zero_u)
        nd = len(zero_d)
        ncross = np.fmin(nu, nd)

        # Find Maxima:
        for ic in range(0, ncross - 1):
            up = int(zero_u[ic])
            down = int(zero_d[ic])
            fvec = f[up:down]
            value_max[ic] = np.amax(fvec)
            index_max = value_max.argmax()
            imaxvec[ic] = up + index_max - 1

        # Find Minima:
        for ic in range(0, ncross - 2):
            down = int(zero_d[ic])
            up = int(zero_u[ic+1])
            fvec = f[down:up]
            value_min[ic] = np.amin(fvec)
            index_min = value_min.argmin()
            iminvec[ic] = down + index_min - 1

        # Remove spurious values, bumps and zero_d
        thr = rms/3

        maxfind = np.where(value_max < thr)

        for i in range(0, len(maxfind)):
            imaxfind = np.where(value_max == maxfind[i])
            imaxvec[imaxfind] = 0
            value_max[imaxfind] = 0

        minfind = np.where(value_min > -thr)

        for j in range(0, len(minfind)):
            iminfind = np.where(value_min == minfind[j])
            value_min[iminfind] = 0
            iminvec[iminfind] = 0

        # Find Average Height

        avh = np.mean(value_max) - np.mean(value_min)

    else:
        avh = 0

    return avh

np.where , and np.nonzero even more so, clearly explains that it returns a tuple, with one array for each dimension of the condition array: np.wherenp.nonzero甚至更清楚地说明了它返回一个元组,其中条件数组的每个维都有一个数组:

In [71]: arr = np.random.randint(-5,5,10)
In [72]: arr
Out[72]: array([ 3,  4,  2, -3, -1,  0, -5,  4,  2, -3])
In [73]: arr.shape
Out[73]: (10,)
In [74]: np.where(arr>=0)
Out[74]: (array([0, 1, 2, 5, 7, 8]),)
In [75]: arr[_]
Out[75]: array([3, 4, 2, 0, 4, 2])

That Out[74] tuple can be used directly as an index. Out[74]元组可以直接用作索引。

You can also extract the array from the tuple: 您还可以从元组提取数组:

In [76]: np.where(arr>=0)[0]
Out[76]: array([0, 1, 2, 5, 7, 8])

That, I think is a better choice than the np.asarray(np.where(...)) 我认为那是比np.asarray(np.where(...))更好的选择

This convention for where becomes clearer when we use it on a 2d array 当我们在2d数组上使用它时,关于where约定变得更加清晰

In [77]: arr2 = arr.reshape(2,5)
In [78]: np.where(arr2>=0)
Out[78]: (array([0, 0, 0, 1, 1, 1]), array([0, 1, 2, 0, 2, 3]))
In [79]: arr2[_]
Out[79]: array([3, 4, 2, 0, 4, 2])

Again we are indexing with a tuple. 同样,我们正在使用元组建立索引。 arr2[1,3] is really arr2[(1,3)] . arr2[1,3]实际上是arr2[(1,3)] The values in [] indexing brackets are actually passed to the indexing function as a tuple of values. 实际上, []索引括号中的值将作为值的元组传递给索引函数。

np.argwhere applies transpose to the result of where , producing an array: np.argwheretranspose应用于where的结果,产生一个数组:

In [80]: np.transpose(np.where(arr2>=0))
Out[80]: 
array([[0, 0],
       [0, 1],
       [0, 2],
       [1, 0],
       [1, 2],
       [1, 3]])

That's the same indexing arrays, but arranged in a 2d column matrix. 这是相同的索引数组,但排列在2d列矩阵中。

If you need the count of where without the actual values, a slightly faster function is 如果您需要的数量where没有实际值,还稍快功能

In [81]: np.count_nonzero(arr>=0)
Out[81]: 6

In fact np.nonzero uses the count to first determine the size of the arrays that it will return. 实际上, np.nonzero使用该count来首先确定它将返回的数组的大小。

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

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