我必须在我的数据分析中显示这个短语:每当gazeAngleVelocity从大于0变为0时,这就是固定的开始。当它从0到大于0时,固定就结束了。当这种情况发生时,创建一个标志。我不知道需要哪个命令;有人知道吗?
您可以移动该列以将值与前一个值进行比较。
(对于本例,我将使用一些虚拟数据)
curr = df["gazeAngleVelocity"]
prev = curr.shift()
df["fixation_start"] = (prev > 0) & (curr == 0)
df["fixation_end"] = (prev == 0) & (curr > 0)
df
给了:
gazeAngleVelocity fixation_start fixation_end
0 0.5 False False
1 1.1 False False
2 0.8 False False
3 0.0 True False
4 1.1 False True
5 2.3 False False
6 0.0 True False
7 0.0 False False
如果您想在更改之前的行上放置标志,只需向另一个方向移动,即可获得下一个值:
next_ = curr.shift(-1)
df["fixation_start"] = (curr > 0) & (next_ == 0)
df["fixation_end"] = (curr == 0) & (next_ > 0)
df
给了:
gazeAngleVelocity fixation_start fixation_end
0 0.5 False False
1 1.1 False False
2 0.8 True False
3 0.0 False True
4 1.1 False False
5 2.3 True False
6 0.0 False False
7 0.0 False False
我的解决方案只是将一个数字与前一个数字进行比较。根据第一个数字的符号是否与最后一个不同,结果可能会有点偏差。让我知道这是否有效:
a = [1,1,-1,-2,-3,4,5,-3,1]
fixations = []
for index in range(len(a)):
if a[index-1] > 0 and a[index] < 0:
fixations.append("fixation_start")
elif a[index-1] < 0 and a[index] > 0:
fixations.append("fixation_end")
else:
fixations.append("continuation")
使用numpy数组可以比较a[i]
和a[i+1]
,如下所示:
import numpy as np
a = np.arange(4)
print('complete array:')
print(a) # [0, 1, 2, 3]
print('all array but first element')
print(a[1:]) # [1, 2, 3]
print('all array but last element')
print(a[:-1]) # [0, 1, 2]
# now we can compare them:
print('positions where next is greater than previous')
print(a[1:] > a[:-1]) # [True, True, True]
但是,请考虑,当删除第一个(或最后一个)元素时,缺少一个";"。元素,因此,结果数组比原数组少一个元素。
在您的例子中,您需要施加两个约束,因此您还需要一个&
(和)。它应该是这样的:
import numpy as np
a = np.array([3, 2, 1, 0, 11, 2, 3, 2, 1111, 0, 11111])
print('start of a fixation')
print(a[:-1][(a[1:] == 0 ) & (a[:-1] > 0)]) # [1, 1111]
print('end of a fixation')
print(a[1:][(a[1:] > 0 ) & (a[:-1] == 0)]) # [11, 11111]