采样numpy数组的最快方法是什么?

Jah*_*fet 5 python numpy sampling

我有一个3D(时间,X,Y)numpy数组,包含6个小时的时间序列几年.(比如5).我想创建一个采样时间序列,其中包含从可用记录中随机抽取的每个日历日的1个实例(每天5种可能性),如下所示.

  • 1月01日:2006年
  • 1月02日:2011年
  • 1月03日:2009年
  • ...

这意味着我需要从01/01/2006获取4个值,从2011年2月1日起获取4个值等.我有一个工作版本,其工作方式如下:

  • 重塑输入数组以添加"年"维度(时间,年份,X,Y)
  • 创建一个随机生成的0到4之间整数的365值数组
  • 使用np.repeat和整数数组仅提取相关值:

例:

sampledValues = Variable[np.arange(numberOfDays * ValuesPerDays), sampledYears.repeat(ValuesPerDays),:,:]
Run Code Online (Sandbox Code Playgroud)

这似乎有效,但我想知道这是否是解决我问题的最佳/最快方法?速度很重要,因为我在循环中这样做,adn将受益于测试尽可能多的情况.

我这样做了吗?

谢谢

编辑 我忘了提到我过滤了输入数据集以删除闰年的第29个.

基本上,该操作的目的是找到一个365天的样本,与平均值等方面的长期时间序列匹配良好.如果采样的时间序列通过我的质量测试,我想导出它并重新开始.

eum*_*iro 3

2008年有366天,所以不要重塑。

看看scikits.timeseries

import scikits.timeseries as ts

start_date = ts.Date('H', '2006-01-01 00:00')
end_date = ts.Date('H', '2010-12-31 18:00')
arr3d = ... # your 3D array [time, X, Y]

dates = ts.date_array(start_date=start_date, end_date=end_date, freq='H')[::6]
t = ts.time_series(arr3d, dates=dates)
# just make sure arr3d.shape[0] == len(dates) !
Run Code Online (Sandbox Code Playgroud)

现在您可以t使用日/月/年对象访问数据:

t[np.logical_and(t.day == 1, t.month == 1)]
Run Code Online (Sandbox Code Playgroud)

例如:

for day_of_year in xrange(1, 366):
    year = np.random.randint(2006, 2011)

    t[np.logical_and(t.day_of_year == day_of_year, t.year == year)]
    # returns a [4, X, Y] array with data from that day
Run Code Online (Sandbox Code Playgroud)

发挥 的属性,t使其也适用于闰年。