久久久久久久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网址在线播放 | 午夜精品福利视频 | 日韩视频精品在线 | 亚洲一区不卡在线 | 日韩欧美在线观看一区 | 欧美日韩电影一区二区 | 欧美一区二区成人 | 日本黄色大片免费看 | cao在线| 色播视频在线观看 | 亚洲精品一区二区 | 一级大片| 午夜影院| 91文字幕巨乱亚洲香蕉 | 国产免费让你躁在线视频 | 在线不卡一区 | 黄色一级片在线播放 | 精品av| 天天干亚洲 | 精品成人佐山爱一区二区 | 狠狠躁18三区二区一区 | 激情综合五月 | 在线观看日韩av | 91视频在线观看免费 | 99久久精品国产一区二区三区 | 午夜av影院 | 日韩成人中文字幕 | 爱操av | 久久国产一区 | 久久国产视频播放 | 777zyz色资源站在线观看 | 久久久久久亚洲 | 亚洲97| 精品综合久久久 | 日韩网| 日韩aⅴ视频 | 黑人巨大精品欧美一区二区一视频 | 天天干视频 | 亚洲男人天堂网 | 97超碰成人|