0

我查看了 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)
4

1 回答 1

1

如果seq是 numpy 数组,则(i*scale/base for i in seq)可以使用 numpy 矢量化操作代替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.])
于 2013-09-25T23:40:38.943 回答