【发布时间】:2018-06-27 06:37:55
【问题描述】:
avg = moving_average(Y, window_size=12).tolist()
residual = Y - avg
std = np.std(residual)
sigma=3
print({'standard_deviation': round(std, 3),
'anomalies_dict': collections.OrderedDict([(index, y_i) for
index, y_i, avg_i in zip_longest(count(), Y, avg)
if (y_i > avg_i + (sigma*std)) | (y_i < avg_i - (sigma*std))])})
Y --> 一系列浮动元素
运行上述代码后,它在 Python 2.7 上运行良好,但在 3.6 上运行失败。 上述代码与 2.7 版本代码的唯一变化是,我在这里使用 zip_longest 而我在 2.7 代码中使用 izip。
Traceback (most recent call last):
File "<ipython-input-37-acf40bad87e9>", line 8, in <module>
index, y_i, avg_i in zip_longest(count(), Y, avg)
File "<ipython-input-37-acf40bad87e9>", line 9, in <listcomp>
if (y_i > avg_i + (sigma*std)) | (y_i < avg_i - (sigma*std))])})
TypeError: unsupported operand type(s) for +: 'NoneType' and 'float'
在这方面需要帮助。
【问题讨论】:
-
zip在 Python3 中的作用显然与 Python2 中的izip相同。 stackoverflow.com/questions/32659552/…