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

Numpy:條件總和

Numpy: conditional sum(Numpy:條件總和)
本文介紹了Numpy:條件總和的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有以下 numpy 數組:

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) 提供結果:

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

我想做的(沒有for循環)是根據最后一列的值對列求和,以便提供的結果是:

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

我意識到如果沒有循環可能會有些牽強,但希望能做到最好……

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

如果必須使用 for 循環,那將如何工作?

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

我試過 np.sum(arr[:, 4]==2000, axis=0) (我會用 for 循環中的變量替換 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 將為您提供最右側列更改的索引:

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 會將您的布爾索引 d 轉換為 np.add.reduceat 期望的整數索引:

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

d = np.where(d)[0]

reduceat 也期望看到零索引,并且所有內容都需要移動一:

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_ 這里比 方便一點np.concatenate 因為它允許標量.然后總和變為:

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)

這當然可以組合成一條線:

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

這篇關于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 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: www.国产精品| 成人亚洲视频 | 免费看欧美一级片 | 亚洲 自拍 另类 欧美 丝袜 | 久草免费在线 | 超碰最新在线 | 亚洲欧美综合精品久久成人 | 久热精品视频 | 精品一区二区av | 人妖一区| 久久男人| 日韩欧美在线视频 | 欧美黑人一区二区三区 | 91精品国产一区 | 国产日产精品一区二区三区四区 | 小视频你懂得 | 久久久久久美女 | 欧美日韩中文在线观看 | 香蕉一区 | 国产欧美日韩综合精品一 | 亚洲激精日韩激精欧美精品 | 精品在线观看入口 | 欧美日韩国产一区二区三区不卡 | 天天综合网永久 | 欧美日韩黄色一级片 | 久热电影 | www.一级片 | 狠狠的日 | 欧美片网站免费 | 天天摸天天干 | 亚洲高清视频在线观看 | 欧美亚洲免费 | 成人亚洲网 | 国产成人精品久久二区二区 | 天天干人人| 精品一二三区在线观看 | 午夜看片网站 | 欧美精品欧美精品系列 | 国产丝袜一区二区三区免费视频 | www.日韩系列 | 久草欧美 |