久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

按索引對numpy數組的累積求和

Cumulative summation of a numpy array by index(按索引對numpy數組的累積求和)
本文介紹了按索引對numpy數組的累積求和的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

假設您有一個需要相加的值數組

Assume you have an array of values that will need to be summed together

d = [1,1,1,1,1]

第二個數組指定哪些元素需要相加

and a second array specifying which elements need to be summed together

i = [0,0,1,2,2]

結果將存儲在大小為 max(i)+1 的新數組中.因此,例如 i=[0,0,0,0,0] 相當于將 d 的所有元素相加并將結果存儲在位置 0 的大小為 1 的新數組.

The result will be stored in a new array of size max(i)+1. So for example i=[0,0,0,0,0] would be equivalent to summing all the elements of d and storing the result at position 0 of a new array of size 1.

我嘗試使用

c = zeros(max(i)+1)
c[i] += d

但是,+= 操作只將每個元素添加一次,從而給出了

However, the += operation adds each element only once, thus giving the unexpected result of

[1,1,1]

而不是

[2,1,2]

如何正確實現這種求和?

How would one correctly implement this kind of summation?

推薦答案

這個解決方案對于大型數組應該更有效(它迭代可能的索引值而不是 i 的單個條目):

This solution should be more efficient for large arrays (it iterates over the possible index values instead of the individual entries of i):

import numpy as np

i = np.array([0,0,1,2,2])
d = np.array([0,1,2,3,4])

i_max = i.max()
c = np.empty(i_max+1)
for j in range(i_max+1):
    c[j] = d[i==j].sum()

print c
[1. 2. 7.]

這篇關于按索引對numpy數組的累積求和的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 黄频在线观看 | 亚洲第一av| 欧美国产日韩在线 | 麻豆中文字幕 | 黄色一级录像 | 欧美激情一区 | 欧洲精品一区 | 中文字幕丰满人伦在线 | 久久久久久免费 | 欧美又大又硬又粗bbbbb | 免费在线成人 | 精品无人国产偷自产在线 | 国产午夜视频在线观看 | 四虎影视av | 精品一区二区三区免费看 | 九九热只有精品 | 91看片在线 | 女子spa高潮呻吟抽搐 | 欧美偷拍精品 | 中文字幕第一页在线 | 亚洲综合三区 | 亚洲综合激情五月久久 | 日日舔| 天堂久久久久 | 欧美激情五月 | 福利影院在线观看 | 91狠狠干 | 日本丰满肉感bbwbbwbbw | 成人免费毛片男人用品 | 色一区二区三区 | 久久黄色大片 | 国产精品久久网 | 国产小视频在线观看 | 午夜免费福利 | 中文字幕在线免费看线人 | a级片免费在线观看 | 亚洲最大黄色网址 | 国语对白永久免费 | 岛国在线视频 | 全部免费毛片在线播放高潮 | 久久在线视频 |