替换NumPy数组中的元素块

huangapple go评论82阅读模式
英文:

Replacing chunks of elements in numpy array

问题

我有一个像这样的 np.array
x = [1,2,3,4,5,6,7,8,9,10 ... N]。我需要将前面的 n 个块替换为特定的元素,像这样:

for i in np.arange(0,125):
    x[i] = x[0]
for i in np.arange(125,250):
    x[i] = x[125]
for i in np.arange(250,375):
    x[i] = x[250]

显然,这不是正确的方法,但我只是写出来以便向您展示我需要实现的目标。

英文:

I have an np.array like this one:
x = [1,2,3,4,5,6,7,8,9,10 ... N]. I need to replace the first n chunks with a certain element, like so:

for i in np.arange(0,125):
    x[i] = x[0]
for i in np.arange(125,250):
    x[i] = x[125]
for i in np.arange(250,375):
    x[i] = x[250]

This is obviously not the way to go, but I just wrote it to this so I can show you what I need to achieve.

答案1

得分: 4

一种方法是 -

In [47]: x
Out[47]: array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21])

In [49]: n = 5

In [50]: x[::n][np.arange(len(x))//n]
Out[50]: array([10, 10, 10, 10, 10, 15, 15, 15, 15, 15, 20, 20])

另一种方法使用 np.repeat -

In [67]: np.repeat(x[::n], n)[:len(x)]
Out[67]: array([10, 10, 10, 10, 10, 15, 15, 15, 15, 15, 20, 20])

对于原地编辑,我们可以以广播的方式重塑并分配,如下所示 -

m = (len(x)-1)//n
x[:n*m].reshape(-1,n)[:] = x[:n*m:n,None]
x[n*m:] = x[n*m]
英文:

One way would be -

In [47]: x
Out[47]: array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21])

In [49]: n = 5

In [50]: x[::n][np.arange(len(x))//n]
Out[50]: array([10, 10, 10, 10, 10, 15, 15, 15, 15, 15, 20, 20])

Another with np.repeat -

In [67]: np.repeat(x[::n], n)[:len(x)]
Out[67]: array([10, 10, 10, 10, 10, 15, 15, 15, 15, 15, 20, 20])

For in-situ edit, we can reshape and assign in a broadcasted-manner, like so -

m = (len(x)-1)//n
x[:n*m].reshape(-1,n)[:] = x[:n*m:n,None]
x[n*m:] = x[n*m]

答案2

得分: 2

import numpy as np
x = np.arange(0,1000)
a = x[0]
b = x[125]
c = x[250]
x[0:125] = a
x[125:250] = b
x[250:375] = c
无需编写循环,您可以使用切片替换一组值。
如果切分相等,可以循环计算起始和结束位置,而不是硬编码。

英文:
import numpy as np
x = np.arange(0,1000)
a = x[0]
b = x[125]
c = x[250]
x[0:125] = a
x[125:250] = b
x[250:375] = c

No need to write loops, you can replace bunch of values using slicing.
if the splits are equal, you can loop to calculate the stat and end positions instead of hard coding

答案3

得分: 0

为了保持切片/值对数量的灵活性,您可以编写如下代码:

def chunk_replace(array, slice_list, value_list):
    
    for s, v in zip(slice_list, value_list):
        array
展开收缩
= v
return array array = np.arange(1000) slice_list = [slice(0, 125), slice(125, 250), slice(250, 375)] value_list = [array[0], array[125], array[250]] result = chunk_replace(array, slice_list, value_list)
英文:

To keep flexibility in the number of slice/value pairs you can write something like:

def chunk_replace(array, slice_list, value_list):
    
    for s,v in zip(slice_list, value_list):
        array
展开收缩
= v return array array = np.arange(1000) slice_list = [slice(0,125), slice(125, 250), slice(250, 375)] value_list = [array[0], array[125], array[250]] result = chunk_replace(array, slice_list, value_list)

huangapple
  • 本文由 发表于 2020年1月3日 18:01:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576518.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定