简体   繁体   English

如何遍历 NumPy 数组,同时按索引过滤单元格值并对其执行数学运算

[英]How to iterate through a NumPy array while filtering cells values by index and performing math on it

I am trying to filter/loop and perform math within the same iteration but cant seem to find the right answer.我试图在同一次迭代中过滤/循环并执行数学运算,但似乎找不到正确的答案。 I have a numpy array, that is size 6, 2, and consists of two values that I want to minus together, however I want the values filtered before the minus process commences.我有一个 numpy 数组,大小为 6、2,由两个我想减去的值组成,但是我希望在减去过程开始之前过滤这些值。

So if the value is greater than in the other column, then the lowest value should be subtracted from the high value, and vice versa .因此,如果该值大于另一列中的值,则应从高值中减去最低值,反之亦然 Also this needs to happen in a loop which iterates through the array while performing the filtering and math.这也需要在一个循环中发生,该循环在执行过滤和数学运算时遍历数组。

This is my code example:这是我的代码示例:

#minus price
print('minus price trying appending')
minus_p_orgp1 = np.append(dif_p_times1, fp, axis=0)
print(minus_p_orgp1)

for ii, vv in enumerate(minus_p_orgp1):
    print('greater')
    greater_1 = np.all(ii > 0, axis=0)
    greater_0 = np.all(ii <= 0, axis=0)
    if greater_1 < greater_0:
        iit = greater_0 - greater_1
    if greater_1 > greater_0:
        iit = greater_1 - greater_0
        print(iit, ii, vv)

ssss = np.zeros(minus_p_orgp1.size - 1)
for i in range(len(minus_p_orgp1) - 1):
    if minus_p_orgp1[i] < minus_p_orgp1[i]:
        ssss[i] = minus_p_orgp1[i + 1] - minus_p_orgp1[i]
    elif minus_p_orgp1[i + 1] > minus_p_orgp1[i]:
        ssss[i] = minus_p_orgp1[i] - minus_p_orgp1[i + 1]
print(ssss)

This is a print of the array where the upper vector is def_p_time1, and lower vector is fp:这是数组的打印,其中上向量是 def_p_time1,下向量是 fp:

  minus price trying appending
[[79340.33057205 78379.24102508 72188.80527274 76557.26239563
  72857.90423589 71137.7943199 ]
 [43528.22       43705.         43931.07       44571.24
  44330.43       44465.64      ]]

What can I do to achieve my goal?我可以做些什么来实现我的目标?

I have also tried to do the process with just having the array being two separate vectors with size 6, 1. But that also seems very difficult, let me know what you think.我也尝试过将数组作为两个大小为 6、1 的独立向量来完成这个过程。但这似乎也很困难,让我知道你的想法。

I have also just tried this;我也刚试过这个; however it just prints out zeros when running the code:但是它在运行代码时只会打印出零:

trii = np.array([[0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1]])
print(trii)
print(minus_p_orgp1[~(trii >= 1)])
print('it works')
itt = minus_p_orgp1[~(trii >= 1)]
itt1 = minus_p_orgp1[~(trii >= 0)]
sssss = np.zeros(dif_p_times1.size - 1)
ssss = np.zeros(minus_p_orgp1.size - 1)
for i in range(len(dif_p_times1) - 1):
    for ii in range(len(fp) - 1):
        if itt < itt1:
            sssss[i] = itt[i] + itt1[i + 1]
            ssss[i, ii] = fp[ii + 1] - dif_p_times1[i]

        elif itt > itt1:
            sssss[i] = itt[i + 1] + itt1[i]
            ssss[i, ii] = dif_p_times1[i] - fp[ii + 1]
print(sssss)

[[0 0 0 0 0 0]
 [1 1 1 1 1 1]]
[63455.70703442 68744.47486851 77804.44752373 79686.34612013
 69322.78250338 83255.08459329]
it does something
[0. 0. 0. 0. 0.]

Here is a new attempt, however it still doesn't work:这是一个新的尝试,但它仍然不起作用:

ssss = np.zeros(minus_p_orgp1.size - 1)
x = minus_p_orgp1[::2]
y = minus_p_orgp1[::-2]
z = ssss[::2]

for z, x, y in range(len(minus_p_orgp1) - 1):
    
    if x[i + 1] < y[i]:
        z[i] = y[i + 1] - x[i]
    elif x[i + 1] > y[i]:
        z[i] = x[i + 1] - y[i]
print(z)

Is there a way to iterate through multidimensional lists which has real values not created from the reshape/arrange functions and still being able to perform boolean filtering on individual cells, from where you perform a math operation?有没有一种方法可以遍历多维列表,这些列表具有不是从重塑/排列函数创建的实际值,并且仍然能够从您执行数学运算的地方对单个单元格执行 boolean 过滤?

I have looked at nditer, and numba, but all seems to do a version where they transpose the dimensions of a 2d array and reduce the values, which with real values only returned me 1 value, and not an array.我看过 nditer 和 numba,但似乎都在做一个版本,他们转置二维数组的维度并减少值,这些值只返回 1 个值,而不是数组。

Looking at your first block看着你的第一个街区

minus_p_orgp1 = np.append(dif_p_times1, fp, axis=0)
print(minus_p_orgp1)

looks like minus_p_orgp1 is a (2,6) array, just a row join of the 2 arrays.看起来minus_p_orgp1是一个 (2,6) 数组,只是 2 arrays 的行连接。

for ii, vv in enumerate(minus_p_orgp1):
    print('greater')
    greater_1 = np.all(ii > 0, axis=0)
    greater_0 = np.all(ii <= 0, axis=0)
    if greater_1 < greater_0:
        iit = greater_0 - greater_1
    if greater_1 > greater_0:
        iit = greater_1 - greater_0
        print(iit, ii, vv)

vv is, iteratively, the 2 rows of minus_p_orgp1 . vvminus_p_orgp1的两行迭代。 ii is, iteratively, 0 and 1. So this np.all(ii>0...) business is just testing whether ii is 0 or . ii是 0 和 1 的迭代。所以这个np.all(ii>0...) business is just testing whether ii is 0 or It sets iit in each loop, but doesn't save the value anywhere.它在每个循环中设置iit ,但不在任何地方保存值。 At the end of the loop it has the last value, but so what?在循环结束时它有最后一个值,但那又如何呢?

In the following size is 12 (2*6), so ssss in np.zeros(11)在下面size是12(2*6),所以np.zeros(11)中的ssss

But the iteration is over 2-1, ie it just evaluates for i=0 :但是迭代超过 2-1,即它只是计算i=0

ssss = np.zeros(minus_p_orgp1.size - 1)
for i in range(len(minus_p_orgp1) - 1):
    if minus_p_orgp1[i] < minus_p_orgp1[i]:
        ssss[i] = minus_p_orgp1[i + 1] - minus_p_orgp1[i]
    elif minus_p_orgp1[i + 1] > minus_p_orgp1[i]:
        ssss[i] = minus_p_orgp1[i] - minus_p_orgp1[i + 1]
print(ssss)

minus_p_orgp1[i] < minus_p_orgp1[i] is an array of 6 False. minus_p_orgp1[i] < minus_p_orgp1[i]是 6 个 False 的数组。 I expect that to raise an ambiguity error when used in the if clause.我希望在if子句中使用时会引发ambiguity错误。

minus_p_orgp1[i + 1] > minus_p_orgp1[i] makes more sense, but still can't be used in elif . minus_p_orgp1[i + 1] > minus_p_orgp1[i]更有意义,但仍然不能在elif中使用。 Are you taking the difference across columns or rows?你是在跨列还是跨行?

The initial word description is a bit vague, but it sure sounds like you just want the positive difference between two arrays:最初的单词描述有点含糊,但听起来您确实只想知道两个 arrays 之间的正差:

In [68]: x = np.array([1,3,2,4]); y = np.array([2,1,3,3])
In [69]: x-y
Out[69]: array([-1,  2, -1,  1])
In [70]: y-x
Out[70]: array([ 1, -2,  1, -1])
In [71]: np.abs(x-y)
Out[71]: array([1, 2, 1, 1])

an iterative equivalent (which would work with lists just as well):一个迭代等价物(同样适用于列表):

In [72]: z = np.zeros(4, int)
In [73]: for i in range(4):
    ...:     if x[i]>y[i]:
    ...:         z[i] = x[i]-y[i]
    ...:     else:
    ...:         z[i] = y[i]-x[i]
    ...: 
In [74]: z
Out[74]: array([1, 2, 1, 1])

or simply:或者简单地:

In [75]: [abs(i-j) for i,j in zip(x,y)]
Out[75]: [1, 2, 1, 1]

As per hpaulj's answer, this worked:根据 hpaulj 的回答,这有效:

[abs(i-j) for i,j in zip(x,y)]

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

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