gpt4 book ai didi

python - 取每 2 个连续元素的平均值并将它们插入回数组

转载 作者:行者123 更新时间:2023-12-04 15:56:39 27 4
gpt4 key购买 nike

我有一个数组,想找到 2 个数字之间的平均值,并在 2 个数字之间添加一个附加元素。例如,如果我从

x = np.array([1, 3, 5, 7, 9])
我想结束
[1, 2, 3, 4, 5, 6, 7, 8, 9]
我该怎么做呢?

最佳答案

这是使用 numpy.repeat 的一种简单有效的方法:

x = np.array([1, 3, 5, 7, 9])

xx = x.repeat(2)
(xx[1:]+xx[:-1]) / 2
# array([1., 2., 3., 4., 5., 6., 7., 8., 9.])

# or if you want to preserve int dtype
(xx[1:]+xx[:-1]) // 2
# array([1, 2, 3, 4, 5, 6, 7, 8, 9])
一个简单的基准测试脚本:
import numpy as np
from numpy.lib import stride_tricks
from itertools import zip_longest

def Brandt(x,forceint=False):
y = np.diff(x)/2 + x[:-1]
z = [n for pair in zip_longest(x,y) for n in pair if n]
return np.asarray(z, int) if forceint else np.asarray(z)

def Ch3steR(x):
strd = x.strides[0]
vals = stride_tricks.as_strided(x, shape=(len(x) - 1, 2),
strides=(strd, strd))
means = vals.mean(axis=1)
return np.insert(x, np.arange(1, len(x)), means)

def moving_average(x, w):
return np.convolve(x, np.ones(w), 'valid') / w
def Tankred(x):
return np.insert(x, np.arange(1, len(x)), moving_average(x, 2))

def fskj(x):
avg = (x[:-1] + x[1:]) / 2
zipped = np.stack((x[:-1], avg), -1)
flattened = zipped.flatten()
return np.append(flattened, x[-1])

def user1740577(x):
for i in np.arange(0,len(x)+2,2):
x = np.insert(x,i+1,np.average(x[i:i+2]))
return x

def loopywalt(x,forceint=False):
xx = x.repeat(2)
return (xx[:-1]+xx[1:]) // 2 if forceint else (xx[:-1]+xx[1:]) / 2

all_ = (Brandt,Ch3steR,Tankred,fskj,user1740577,loopywalt)
blacklist=[]
from timeit import timeit
rng = np.random.default_rng(seed=1)
for ex in [np.array([1,3,5,7,9]),rng.integers(1,1000,1000),
rng.integers(1,1000,1000000)]:
print();print("n =",len(ex))
for method in all_:
if method in blacklist:
continue
t = timeit(lambda:method(ex),number=10)
if t<0.1:
t = timeit(lambda:method(ex),number=1000)
else:
blacklist.append(method)
t *= 100
print(method.__name__,t,'ms')
结果:
n = 5
Brandt 0.018790690000969335 ms
Ch3steR 0.06143478500052879 ms
Tankred 0.039249178998943535 ms
fskj 0.026057840999783366 ms
user1740577 0.15504688399960287 ms
loopywalt 0.0033979790005105315 ms

n = 1000
Brandt 0.4772341360003338 ms
Ch3steR 0.10018322700125282 ms
Tankred 0.0674891500002559 ms
fskj 0.03475799899933918 ms
user1740577 17.72124929993879 ms
loopywalt 0.017431922000469058 ms

n = 1000000
Brandt 491.9887762000144 ms
Ch3steR 56.97805079998943 ms
Tankred 44.63849610001489 ms
fskj 25.709937600004196 ms
loopywalt 20.622111500051687 ms

关于python - 取每 2 个连续元素的平均值并将它们插入回数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69265273/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com