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

按索引對(duì)numpy數(shù)組的累積求和

Cumulative summation of a numpy array by index(按索引對(duì)numpy數(shù)組的累積求和)
本文介紹了按索引對(duì)numpy數(shù)組的累積求和的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

假設(shè)您有一個(gè)需要相加的值數(shù)組

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

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

第二個(gè)數(shù)組指定哪些元素需要相加

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

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

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

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

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

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

[1,1,1]

而不是

[2,1,2]

如何正確實(shí)現(xiàn)這種求和?

How would one correctly implement this kind of summation?

推薦答案

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

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.]

這篇關(guān)于按索引對(duì)numpy數(shù)組的累積求和的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周?chē)L制一個(gè)矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測(cè)和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個(gè)矩形邊界框中應(yīng)用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據(jù)文本方向檢測(cè)圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測(cè)圖像中矩形的中心和角度)
主站蜘蛛池模板: 免费视频成人国产精品网站 | 中日字幕大片在线播放 | 91xx在线观看| 免费国产视频在线观看 | 国产精品久久久久久久久久免费 | 日韩免费在线观看视频 | 日本午夜一区二区三区 | 天天影视亚洲综合网 | 又黑又粗又长的欧美一区 | 欧美久久一区 | av网站在线免费观看 | 99久久久无码国产精品 | 亚洲国产成人精品久久久国产成人一区 | 欧美精品久久久久 | 精品福利视频一区二区三区 | 亚洲社区在线 | 91福利在线观看视频 | 超碰在线97国产 | 91在线视频 | 欧美成人精品二区三区99精品 | 欧美一级艳情片免费观看 | 激情av在线| 99久久久国产精品 | 亚洲欧美日韩中文字幕一区二区三区 | 久久蜜桃av一区二区天堂 | 欧美色综合天天久久综合精品 | 亚洲日本一区二区三区四区 | 国产精品视频不卡 | 日日干夜夜干 | 中文字幕在线精品 | 久久精彩视频 | 国产精品久久7777777 | 国产免费av在线 | 欧美成人h版在线观看 | 亚洲性视频 | 亚洲精品www久久久久久广东 | 国产成人99 | 七七婷婷婷婷精品国产 | 色婷婷综合久久久久中文一区二区 | 曰韩三级 | 啪啪免费网 |