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

Numpy:條件總和

Numpy: conditional sum(Numpy:條件總和)
本文介紹了Numpy:條件總和的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我有以下 numpy 數(shù)組:

I have the following numpy array:

import numpy as np
arr = np.array([[1,2,3,4,2000],
                [5,6,7,8,2000],
                [9,0,1,2,2001],
                [3,4,5,6,2001],
                [7,8,9,0,2002],
                [1,2,3,4,2002],
                [5,6,7,8,2003],
                [9,0,1,2,2003]
              ])

我理解 np.sum(arr, axis=0) 提供結(jié)果:

array([   40,    28,    36,    34, 16012])

我想做的(沒(méi)有for循環(huán))是根據(jù)最后一列的值對(duì)列求和,以便提供的結(jié)果是:

what I would like to do (without a for loop) is sum the columns based on the value of the last column so that the result provided is:

array([[   6,    8,   10,   12, 4000],
       [  12,    4,    6,    8, 4002],
       [   8,   10,   12,    4, 4004],
       [  14,    6,    8,   10, 4006]])

我意識(shí)到如果沒(méi)有循環(huán)可能會(huì)有些牽強(qiáng),但希望能做到最好……

I realize that it may be a stretch to do without a loop, but hoping for the best...

如果必須使用 for 循環(huán),那將如何工作?

If a for loop must be used, then how would that work?

我試過(guò) np.sum(arr[:, 4]==2000, axis=0) (我會(huì)用 for 循環(huán)中的變量替換 2000),但是它給出了 2

I tried np.sum(arr[:, 4]==2000, axis=0) (where I would substitute 2000 with the variable from the for loop), however it gave a result of 2

推薦答案

你可以在純 numpy 中使用 np.diffnp.add.reduceat.np.diff 將為您提供最右側(cè)列更改的索引:

You can do this in pure numpy using a clever application of np.diff and np.add.reduceat. np.diff will give you the indices where the rightmost column changes:

d = np.diff(arr[:, -1])

np.where 會(huì)將您的布爾索引 d 轉(zhuǎn)換為 np.add.reduceat 期望的整數(shù)索引:

np.where will convert your boolean index d into the integer indices that np.add.reduceat expects:

d = np.where(d)[0]

reduceat 也期望看到零索引,并且所有內(nèi)容都需要移動(dòng)一:

reduceat will also expect to see a zero index, and everything needs to be shifted by one:

indices = np.r_[0, e + 1]

使用 np.r_ 這里比 方便一點(diǎn)np.concatenate 因?yàn)樗试S標(biāo)量.然后總和變?yōu)?

Using np.r_ here is a bit more convenient than np.concatenate because it allows scalars. The sum then becomes:

result = np.add.reduceat(arr, indices, axis=0)

這當(dāng)然可以組合成一條線:

This can be combined into a one-liner of course:

>>> result = np.add.reduceat(arr, np.r_[0, np.where(np.diff(arr[:, -1]))[0] + 1], axis=0)
>>> result
array([[   6,    8,   10,   12, 4000],
       [  12,    4,    6,    8, 4002],
       [   8,   10,   12,    4, 4004],
       [  14,    6,    8,   10, 4006]])

這篇關(guān)于Numpy:條件總和的文章就介紹到這了,希望我們推薦的答案對(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è)圖像中矩形的中心和角度)
主站蜘蛛池模板: 亚洲一区国产精品 | 人人人人人爽 | 精品久久久久一区二区国产 | 在线观看 亚洲 | 日日操天天射 | 日韩欧美国产精品一区 | www.久久久久久久久久久 | www.99久久.com| 日韩欧美一区二区在线播放 | 男女网站免费观看 | 中文字幕一区二区在线观看 | 成人精品视频99在线观看免费 | 国产伦精品一区二区三区精品视频 | 91精品国产乱码久久久久久久久 | 久久精品无码一区二区三区 | 国产精品一区久久久 | 五月天婷婷综合 | 欧美一区免费 | 在线免费观看日本视频 | 在线欧美小视频 | 欧美成人激情 | 99视频免费在线观看 | 国产一级片久久久 | 国产精品国产三级国产a | 久久av网 | 性国产xxxx乳高跟 | www.99热.com | 久久久久久国产精品 | 国产伦精品一区二区三毛 | 四虎网站在线观看 | 国产精品视频一二三区 | 国产精久久久久久久 | 中文字幕在线观看第一页 | 精品96久久久久久中文字幕无 | 国产激情91久久精品导航 | 超碰3| 日韩在线不卡 | 久久久久国产一区二区三区四区 | 四虎海外| 欧美视频二区 | 免费 视频 1级|