Numpy / Scipy将原始值转换为索引时间序列?

时间:2013-09-25 22:21:26

标签: numpy statistics scipy time-series

我看过numpy / scipy文档,但是我找不到任何内置函数来执行此操作。

我想将原始数字(温度,当它发生)转换为表示从原始状态到索引序列的时间序列(即第一个值为100,后续值与第一个原始值进行缩放)。因此,如果原始值为(15,7.5,5),则索引值将为(100,50,33)(心理计算,因此为int值)。

这对自己很容易编码,但我想尽可能使用内置编码。自制软件是:

def indexise(seq,base=0,scale=100):
    if not base:
        base=seq[0]
    return (i*scale/base for i in seq)

1 个答案:

答案 0 :(得分:1)

如果seq是一个numpy数组,那么您可以使用numpy向量化操作(i*scale/base for i in seq)而不是scale*seq/base

以下是我修改你的功能的方法:

import numpy as np

def indexise(seq, base=None, scale=100):
    seq = np.asfarray(seq)
    if base is None:
        base = seq[0]
    result = scale*seq/base
    return result

例如,

In [14]: indexise([15, 7.5, 5, 3, 10, 12])
Out[14]: 
array([ 100.        ,   50.        ,   33.33333333,   20.        ,
         66.66666667,   80.        ])

In [15]: indexise([15, 7.5, 5, 3, 10, 12], base=10)
Out[15]: array([ 150.,   75.,   50.,   30.,  100.,  120.])
相关问题