简体   繁体   English

按元素求和2d numpy数组

[英]Sum 2d numpy array by element

I need to sum up a numpy array by index 1, AKA, the region the Species is in. The original data is in a .csv file, but I converted that to a numpy array. 我需要通过索引1(即AKA,即物种所在的区域)来总结一个numpy数组。原始数据位于.csv文件中,但我将其转换为numpy数组。 All that's left is figuring out how to sort and sum by what I need. 剩下的就是弄清楚如何根据我的需要进行排序和汇总。 Would a simple if statement be better than a numpy array function? 简单的if语句会比numpy数组函数好吗?

The array looks kinda like this (Linked below): 该数组看起来像这样(链接如下):

#(Species) (Region located) (# of individuals) <-- For your convenience

[['Purple Puffin' '1' '1']
['Wisteria Wombat' '3' '4']
['Pumpkin Pomeranian' '1' '3']
['Wisteria Wombat' '2' '3']
['Burgundy Bichon Frise' '2' '1']
['Purple Puffin' '1' '4']
['Wisteria Wombat' '2' '2']
['Pumpkin Pomeranian' '1' '2']]

But the full array has more data and I can link that in. 但是整个阵列有更多数据,我可以将其链接。

I need to sum up the "# of individuals" in each "Region". 我需要总结每个“区域”中的“个人人数”。 The final output should look like this in a numpy array: 最终输出应在numpy数组中如下所示:

['Burgundy Bichon Frise' '1' '#']
['Pumpkin Pomeranian' '1' '#']
['Purple Puffin' '1' '#']
['Wisteria Wombat' '1' '#']

['Burgundy Bichon Frise' '2' '#']
['Pumpkin Pomeranian' '2' '#']
['Purple Puffin' '2' '#']
['Wisteria Wombat' '2' '#']

['Burgundy Bichon Frise' '3' '#']
['Pumpkin Pomeranian' '3' '#']
['Purple Puffin' '3' '#']
['Wisteria Wombat' '3' '#']

Each region is separated into species and each species population is summed together. 每个区域都分为物种,每个物种的种群相加。 This needs to end up in a numpy array. 这需要以一个numpy数组结束。

EDIT I got the array sorted by the species and region. 编辑我得到按物种和地区排序的数组。 Now I just need to know how to add up the "# of individuals" in each region by each species. 现在,我只需要知道如何按每种物种在每个区域中添加“个体数”。

Link to full numpy data set 链接到完整的numpy数据集

You can use np.unique and np.bincount : 您可以使用np.uniquenp.bincount

>>> inp
array([['Purple Puffin', '1', '1'],
       ['Wisteria Wombat', '3', '4'],
       ['Pumpkin Pomeranian', '1', '3'],
       ['Wisteria Wombat', '2', '3'],
       ['Burgundy Bichon Frise', '2', '1'],
       ['Purple Puffin', '1', '4'],
       ['Wisteria Wombat', '2', '2'],
       ['Pumpkin Pomeranian', '1', '2']], dtype='<U21')
>>> unq, inv = np.unique(inp[:, 1::-1], axis=0, return_inverse=True)
>>> cnt = np.bincount(inv, inp[:, 2].astype(int)).astype(int)
>>> res = np.c_[unq[:, ::-1], cnt]
>>> res
array([['Pumpkin Pomeranian', '1', '5'],
       ['Purple Puffin', '1', '5'],
       ['Burgundy Bichon Frise', '2', '1'],
       ['Wisteria Wombat', '2', '5'],
       ['Wisteria Wombat', '3', '4']], dtype='<U21')

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

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