【发布时间】:2017-06-28 19:19:10
【问题描述】:
我为 3.X 编写了一个运行良好的脚本,但是我需要将其转换为 2.7,并且在这样做时遇到了这个错误,我不知道如何解决。
此函数通过将数据划分为多个周期并为每个周期找到一个百分位数以应用于所述数据来更正数据。
import numpy
import math
import random
from bokeh.plotting import *
from bokeh.layouts import *
from __future__ import division
def rs_percent_corr(start, end, rs, rso, thresh, period):
num_periods = int(math.ceil((end - start) / period))
rs_period = numpy.zeros(period)
rso_period = numpy.zeros(period)
period_corr = numpy.zeros(num_periods)
# Placing intervals in separate array for easy handling
rs_interval = numpy.array(rs[start:end])
rso_interval = numpy.array(rso[start:end])
# separate the interval into predefined periods and compute correction
count_one = 0 # index for full correction interval
count_two = 0 # index for within each period
count_three = 0 # index for number of periods
while count_one < len(rs_interval):
if (count_two < period) and count_one == len(rs_interval) - 1:
# if statement handles final period
rs_period[count_two] = rs_interval[count_one]
rso_period[count_two] = rso_interval[count_one]
count_one += 1
count_two += 1
while count_two < period:
# This fills out the rest of the final period with NaNs so
# they are not impacted by the remaining zeros
rs_period[count_two] = numpy.nan
rso_period[count_two] = numpy.nan
count_two += 1
ratio = numpy.divide(rs_period, rso_period)
period_corr[count_three] = numpy.nanpercentile(ratio, thresh)
elif count_two < period:
# haven't run out of data points, and period still hasn't been filled
rs_period[count_two] = rs_interval[count_one]
rso_period[count_two] = rso_interval[count_one]
count_one += 1
count_two += 1
else:
# end of a period
count_two = 0
ratio = numpy.divide(rs_period, rso_period)
period_corr[count_three] = numpy.nanpercentile(ratio, thresh)
count_three += 1
return period_corr
在 3.X 中运行脚本时它可以工作,但尝试在 2.7 中运行它会在 period_corr[count_three] = numpy.nanpercentile(ratio, thresh) 行上生成“IndexError: Index 110 is out of bounds for axis 0 with size 110”
我错过了什么?提前感谢您的宝贵时间。
【问题讨论】:
-
您可以编辑您的问题并添加完整的回溯吗?
标签: python-2.7 python-3.x numpy