重塑xAxis上的数组并在Python中填充平均值? [英] Reshape array on xAxis and fill with mean value in Python?

查看:149
本文介绍了重塑xAxis上的数组并在Python中填充平均值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Python中重塑数组并用平均值填充它. 示例:

i'm trying to reshape a array in Python and fill it with mean values. Example:

  • 给定数组:[2,3,-20,10,4]
  • 搜索到的数组:[2、2.5、3,-8.5,-20, -5,10,7,4]
  • Given array: [2, 3, -20, 10, 4]
  • Searched array: [2, 2.5, 3, -8.5, -20, -5, 10, 7, 4]

更高级:我有一个包含1000个样本的数组.但是我知道应该有1300个样本.如何将数组缩放到新的长度,并用均值很好地填充它? 插值的解决方案也可以让我高兴

More advanced: I've got an array with e.g 1000 samples. But I know it should be 1300 samples long. How to scale the the array to the new length and fill it well distributed with mean values? A solution with interpolation could make me happy too

我被问到一个例子,我说分布均匀的值是什么意思.例如:传感器应以100Hz的频率传送数据.但是有时传感器无法提供完整的采样频率.而不是在13秒内获得1300个样本,而是获得900到1300个样本之间的随机数量.我不知道何时缺少值.我想将丢失的值均匀地分布在整个数组上,并为它们分配一个有意义的值.

I was questioned for an example what i mean with well distributed values. E.g: a sensor should deliver data with 100Hz. But sometimes the sensor is not able to provide the full sampling frequency. Instead of getting 1300 samples in 13 seconds i get a random amount between 900 and 1300 samples. I don't know when a value is missing. I want to distribute the missing values uniformly over the whole array and assign them a meaningful value.

谢谢

推荐答案

我写了一个对我来说更好的解决方案.我在大型数组上遇到浮动错误问题. 为了纠正这些,我随机插入了一些缺失的.也许有人知道如何避免这种情况 我敢肯定,代码很容易实现.

I've written a solution which is even better for me. I had some problems with floating errors on large arrays. To correct those i inserted some missing ones randomly. Maybe someone knows how to avoid this I'm sure the code is very optimizable feel free to do this.

import numpy as np
def resizeArray(data, newLength):

    datalength = len(data)
    if (datalength == newLength): return data

    appendIndices = []
    appendNow = 0
    step = newLength / datalength
    increase =  step % 1
    for i in np.arange(0, datalength-2, step):
        appendNow += increase
        if appendNow >= 1:
            appendIndices.append(round(i,0))
            appendNow = appendNow % 1

    #still missing values due to floating errors?
    diff = newLength - datalength - len(appendIndices)
    if diff > 0:
        for i in range(0, diff):
            appendIndices.append(np.random.randint(1, datalength - 2))

    #insert average at the specified indizes
    appendVals = [(data[i] + data[i+1]) / 2 for i in appendIndices]
    a = np.insert(data, appendIndices, appendVals)

    return a

这篇关于重塑xAxis上的数组并在Python中填充平均值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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