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

Ada*_*dam 6 python numpy

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

x = np.array([1, 3, 5, 7, 9])
Run Code Online (Sandbox Code Playgroud)

我想结束

[1, 2, 3, 4, 5, 6, 7, 8, 9]
Run Code Online (Sandbox Code Playgroud)

我该怎么做呢?

loo*_*alt 6

这是一个简单而有效的方法,使用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])
Run Code Online (Sandbox Code Playgroud)

一个简单的基准测试脚本:

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')
Run Code Online (Sandbox Code Playgroud)

结果:

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
Run Code Online (Sandbox Code Playgroud)


小智 3

您可以使用numpy.insert移动平均值来填充缺失值:

import numpy as np

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

# copied from: /sf/answers/3823970181/
def moving_average(x, w):
    return np.convolve(x, np.ones(w), 'valid') / w

x_filled = np.insert(x, np.arange(1, len(x)), moving_average(x, 2))
Run Code Online (Sandbox Code Playgroud)

x_填充: array([1, 2, 3, 4, 5, 6, 7, 8, 9])