在numpy数组中索引/切片以修改它 [英] indexing/slicing in numpy array to modify it

查看:248
本文介绍了在numpy数组中索引/切片以修改它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个numpy数组A,我想使用索引列表B修改它中的值。但事情是在我的切片中我可以有多次数组元素...
这个例子将更好地解释我的意思:

I have a numpy array A and I want to modify values in it using a indexing list B. But the thing is in my slicing I can have an element of the array multiple times... This example will explain better what I mean by that :

import numpy as np
A = np.arange(5) + 0.5
B = np.array([0, 1, 0, 2, 0, 3, 0, 4])
print A[B]

按预期返回 [0.5 1.5 0.5 2.5 0.5 3.5 0.5 4.5]
但是,如果我这样做:

returns as expected [ 0.5 1.5 0.5 2.5 0.5 3.5 0.5 4.5]. However if I do that :

A[B] += 1.
print A

我原本期望获得 [4.5 2.5 3.5 4.5 5.5] 因为第一个元素在索引向量B中重复了4次,但它返回 [1.5 2.5 3.5 4.5 5.5]
那么我该怎样做我真正想做的事情呢? (不使用任何循环,因为我在非常大的数组上使用它)

I was expecting to obtain [ 4.5 2.5 3.5 4.5 5.5] as the first element is repeated 4 times in the indexing vector B, but it returns [ 1.5 2.5 3.5 4.5 5.5]. So how can I do what I actually wanted to do? (without using any loop as I'm using that on very large arrays)

推荐答案

为什么会发生这种情况的解释有点牵扯,但基本上,缓慢吃你的作业。这个numpy ufuncs问题有几种解决方法。适用于任何操作的合适的一个是使用相应的ufunc的 at 方法

The explanation why this happens is a little involved, but basically, "buffering ate your homework." There are a couple of ways around this issue of numpy ufuncs. The proper one, that will work with any operation is to use the corresponding ufunc's at method:

>>> A = np.arange(5) + 0.5
>>> B = np.array([0, 1, 0, 2, 0, 3, 0, 4])
>>> np.add.at(A, B, 1)
>>> A
array([ 4.5,  2.5,  3.5,  4.5,  5.5])

这个趋势有点慢,所以为了尽可能快的性能,只有添加,你可以使用 np.bincount

This tends to be kind of slow, so for the fastest performance possible, and only for addition, you can use np.bincount:

>>> A = np.arange(5) + 0.5
>>> A += np.bincount(B) * 1  # replace the 1 with the number you want to add
>>> A
array([ 4.5,  2.5,  3.5,  4.5,  5.5])

编辑

如果要添加的是与 B 相同长度的数组,那么使用 bincount ,以下内容可能会比第一种方法运行得更快:

If what you want to add is an array of the same length as B, then the following, using bincount, is probably going to run faster than the first method:

>>> A = np.arange(5) + 0.5
>>> C = np.ones_like(B)  # They are all ones, but could be anything
>>> A += np.bincount(B, weights=C)
>>> A
array([ 4.5,  2.5,  3.5,  4.5,  5.5])

这篇关于在numpy数组中索引/切片以修改它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆