【发布时间】:2016-11-23 10:57:40
【问题描述】:
我在遍历我的 numpy 数组时遇到问题。我有两个函数(f 和 g)和 x 轴的时间戳。首先,我确定了 f 和 g 的截距。然后我想计算拦截发生了多少次,而我的函数 f 的 值 大于 0。 运行代码后出现以下错误。 (我也试过 f.all() ):
具有多个元素的数组的真值是不明确的。 使用 a.any() 或 a.all()
代码如下:
import pandas as pd
import numpy as np
df = pd.read_csv("Sensor2.csv", delimiter=";", parse_dates=True)
df.drop('x', axis=1, inplace=True)
df.drop('y', axis=1, inplace=True)
df["100MA"] = pd.rolling_mean(df["z"], 100, min_periods=1)
df["200MA"] = pd.rolling_mean(df["z"], 200, min_periods=1)
x = np.array(df['Timestamp'])
f = np.array(df['100MA'])
g = np.array(df['200MA'])
index = np.argwhere((np.diff(np.sign(f - g)) != 0)).reshape(-1).astype(int)
counter = 0
for i in np.nditer(index):
if f > 0:
counter = counter +1
print counter
感谢您的帮助!
【问题讨论】:
-
最后一次迭代没有意义。也不要使用
nditer。直接迭代。
标签: python arrays loops numpy for-loop