python - 将价目表从较长的长度调整为较小的长度

标签 python pandas interpolation resampling rescale

给定以下包含 60 个元素的 pandas 数据框。

import pandas as pd
data = [60,62.75,73.28,75.77,70.28
    ,67.85,74.58,72.91,68.33,78.59
    ,75.58,78.93,74.61,85.3,84.63
    ,84.61,87.76,95.02,98.83,92.44
    ,84.8,89.51,90.25,93.82,86.64
    ,77.84,76.06,77.75,72.13,80.2
    ,79.05,76.11,80.28,76.38,73.3
    ,72.28,77,69.28,71.31,79.25
    ,75.11,73.16,78.91,84.78,85.17
    ,91.53,94.85,87.79,97.92,92.88
    ,91.92,88.32,81.49,88.67,91.46
    ,91.71,82.17,93.05,103.98,105]

data_pd = pd.DataFrame(data, columns=["price"])

是否有一个公式可以以这种方式重新缩放它,以便对于从索引 0 到索引 i+1 开始的大于 20 个元素的每个窗口,数据为重新调整为 20 个元素?

这是一个循环,它使用用于重新缩放的数据创建窗口,我只是不知道有什么方法可以针对当前的问题进行重新缩放。关于如何做到这一点有什么建议吗?

miniLenght = 20
rescaledData = []
for i in range(len(data_pd)):
    if(i >= miniLenght):
        dataForScaling = data_pd[0:i]
        scaledDataToMinLenght = dataForScaling #do the scaling here so that the length of the rescaled data is always equal to miniLenght
        rescaledData.append(scaledDataToMinLenght)

基本上,重新缩放后,rescaledData 应有 40 个数组,每个数组的长度为 20 个价格。

最佳答案

从阅读本文来看,您似乎正在将列表大小调整回 20 个索引,然后在 20 个索引处插入数据。

我们将像它们一样创建索引(range(0, len(large), step = len(large)/miniLenght)),然后使用 numpys interp - 有一百万种数据插值方法。 np.interp 使用线性插值,因此如果您要求索引 1.5,您将获得点 1 和 2 的平均值,依此类推。

因此,这里是对代码的快速修改(注意,我们可能可以使用“滚动”对其进行完全矢量化):

import numpy as np
miniLenght = 20
rescaledData = []

for i in range(len(data_pd)):
    if(i >= miniLenght):
        dataForScaling = data_pd['price'][0:i]
        #figure out how many 'steps' we have
        steps = len(dataForScaling)
        #make indices where the data needs to be sliced to get 20 points
        indices = np.arange(0,steps, step = steps/miniLenght)
        #use np.interp at those points, with the original values as given
        rescaledData.append(np.interp(indices, np.arange(steps), dataForScaling))

输出符合预期:

[array([ 60.  ,  62.75,  73.28,  75.77,  70.28,  67.85,  74.58,  72.91,
         68.33,  78.59,  75.58,  78.93,  74.61,  85.3 ,  84.63,  84.61,
         87.76,  95.02,  98.83,  92.44]),
 array([ 60.    ,  63.2765,  73.529 ,  74.9465,  69.794 ,  69.5325,
         74.079 ,  71.307 ,  72.434 ,  77.2355,  77.255 ,  76.554 ,
         81.024 ,  84.8645,  84.616 ,  86.9725,  93.568 ,  98.2585,
         93.079 ,  85.182 ]),.....

关于python - 将价目表从较长的长度调整为较小的长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45111546/

相关文章:

python - numpy.memmap 到 Image.frombuffer - 不复制

python - string.format(...,**locals()) 的缩写形式

python - os.environ ['http_proxy' ] 不工作

python - groupby 一列并将剩余列转换为字典

Python/Scipy 二维插值(非均匀数据)

python - 如何正确实现反向传播

python - 在同一行的 csv 文件中获取值

python - 用 1 替换列中的非 Null 值

python - 如何定义 RegularGridInterpolator 的值

python - 外推(和内插)Pandas DataFrame