2017-07-14 65 views
2

给定以下具有60个元素的熊猫数据框。重新调整价格表从较长的长度到较小的长度

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倍的价格的长度。

+0

你做什么重新调整? –

+0

这个问题真的来自科学论文,我试图重现结果,但我很难做到重新缩放。 [Here](http://content.iospress.com/articles/algorithmic-finance/af059#eq3)是我发现的一个公式,我只是不知道如何在这里应用 – RaduS

+0

请看[ df.rolling'](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.rolling.html)。可能有些用处。用于窗口滚动的 –

回答

3

从阅读文章看来,您似乎将列表重新调整为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 ]),..... 
+0

谢谢@jeremycg的答案。就是这样:)我会在15小时内给予这个答案,当它允许我;) – RaduS

+0

谢谢!很高兴它的工作 – jeremycg