In Pandas or Numpy how do you multipaly reduce with XOR a row until all reductions are reduced until a final bottowm row

huangapple go评论49阅读模式
英文:

In Pandas or Numpy how do you multipaly reduce with XOR a row until all reductions are reduced until a final bottowm row

问题

在Pandas或Numpy中,如何多次按行进行减少,直到所有减少操作减少到最后一行?

我想按一种方式减少我的B列,每个项目都进行异或操作,直到最后一行:[1, 8, 6, 12, 1, 2],以便我可以保存在C列中。

我尝试使用apply函数和循环,但对于大型数据集来说这可能非常昂贵。是否有人有更好的方法,而不是使用循环逐行创建行,不太容易将其减少到一行?(因为我使用非常大的数据集,这只是一个示例)

英文:

In Pandas or Numpy how do you multiplely reducea row until all reductions are reduced until a final bottowm row

ok i want to reduce my B column in way keeps xoring down each item until the final row: [1 ,8 , 6 , 12 , 1 , 2] so i can save it row C

if tried using an apply loo but this can get very expensive for large datasets. Does anyone have a shortcutor better method thah using a loop to create row after row with
not the easist logic to reduce this to the row?

Here is the data the second number version how i xor each next row to obtain an answer for a new list and keep continuing down to a final result. this is quite slow and
was looking for a better solution

    A   B  C
0  12   2  0
1  10   6  0
2   2   8  0
3   9  11  0  
4   5  12  0
5   0   5  0
6   4   4  0

for example column B looks like this with a .T transform:

    0   1  2   3   4  5  6
A  12  10  2   9   5  0  4
B   2   6  8  11  12  5  4

so basically is I do these operations:
2 ^ 6. which is 4
6 ^ 8, which is 14
8 ^ 11 which is 3
11 ^ 12 which is 7
5 ^ 4 which is 1. which I store in a separate array because I only want the final result. So now I have

so is have [ 4, 14, 3, 7, 1 ]  and a second array with [1]
4^14 = 10, 14^3=13 3^7=4, 7^1 =1

no I have to keep reducing
[10, 3, 4, 14, 8]
10^3 = 9 3^4 = 7, 4^14=8.  and I manually move the last term to the new array which is now [1, 8]
I do this so forth so my array has all the final results and end up with 

the final result of each iterative reduction: [1  , 8  , 6  ,  12  ,1  ,2] as shown below:

  A   B
[[12  2]
 [10  6] 4   
 [ 2  8] 14  10
 [ 9 11] 3   3   9  
 [ 5 12] 7   4   7    14 
 [ 0  5] 9   14  10   13  3
 [ 4  4] 1   8   6    12  1  2


so that the final out come is the last row in column C

    A   B  C
0  12   2  1
1  10   6  8
2   2   8  6
3   9  11  12  
4   5  12  1
5   0   5  2
6   4   4  0

ok i want to reduce my B column in a that way keeps xoring down each item until the final row: [1 ,8 , 6 , 12 , 1 , 2] so i can save it row C

if tried using an apply function and loop but this can get very expensive for large datasets. Does anyone have a shortcutor better method thah using a loop to create row after row with not the easist logic to reduce this to the row?(as I work with very large data sets, this is just an example )

答案1

得分: 1

这可以通过一个单一的循环来完成。为了提高性能,您可以使用 numba

arr = np.array([2, 6, 8, 11, 12, 5, 4])  # 列 B
n = len(arr)
results = []

while n > 1:
    results.append(np.bitwise_xor(arr[:n-1], arr[1:]))
    arr = results[-1]
    n = arr.size

holder = np.zeros((len(results), len(results)))
indices = np.triu_indices(len(holder))  # holder 的上三角索引
holder[indices] = np.concatenate(results, axis=0)
print(holder[:, -1])
>> [ 1.  8.  6. 12. 15.  2.]
英文:

This can be done with one single loop. For performance you can used numba maybe.

arr = np.array([2, 6, 8, 11, 12, 5, 4]) # column B
n = len(arr)
results = []

while n > 1:
    results.append(np.bitwise_xor(arr[:n-1], arr[1:]))
    arr = results[-1]
    n = arr.size

holder = np.zeros((len(results), len(results)))
indices = np.triu_indices(len(holder)) # indices of upper triangle of holder
holder[indices] = np.concatenate(results, axis=0)
print(holder[:,-1])
>> [ 1.  8.  6. 12. 15.  2.]

huangapple
  • 本文由 发表于 2023年6月5日 06:52:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402711.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定