Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何获得numpy,仅适用于第一个True_Python_Numpy - Fatal编程技术网

Python 如何获得numpy,仅适用于第一个True

Python 如何获得numpy,仅适用于第一个True,python,numpy,Python,Numpy,我有如下所示的数据数组 a=np.array([1,2,3,4,5,6,7,8,1,2,3,4,5,6]) 我想获得条件格式,如a>5 我可以用np.where获得它。但np.next true的连续检查在哪里 np.where(a>5,'T','F') > array(['F', 'F', 'F', 'F', 'F', 'T', 'T', 'T', 'F', 'F', 'F', 'F', 'F', 'T'], dtype='<U1') np.其中(a>5,'T','F')

我有如下所示的数据数组

a=np.array([1,2,3,4,5,6,7,8,1,2,3,4,5,6])
我想获得条件格式,如
a>5

我可以用
np.where
获得它。但np.next true的连续检查在哪里

np.where(a>5,'T','F')
> array(['F', 'F', 'F', 'F', 'F', 'T', 'T', 'T', 'F', 'F', 'F', 'F', 'F', 'T'], dtype='<U1')
np.其中(a>5,'T','F')

>数组(['F','F','F','F','T','T','F','F','F','F','T'],dtype='您可以使用
np.roll
移动值,然后检查原始元素是否为真,移动的元素是否为假

a_bool = a > 5
a_shifted_bool = np.roll(a_bool, 1)
a_shifted_bool[0] = False # first element is last element of a_bool, which we need to ignore

np.where(a_bool & ~a_shifted_bool, 'T', 'F')
array(['F', 'F', 'F', 'F', 'F', 'T', 'F', 'F', 'F', 'F', 'F', 'F', 'F',
       'T'], dtype='<U1')
a_bool=a>5
a__bool=np.滚动(a_bool,1)
a_shifted_bool[0]=False#第一个元素是_bool的最后一个元素,我们需要忽略它
np.where(a_bool&~a_移位的a_bool,'T','F')
数组(['F','F','F','F','F','T','F','F','F','F','F','F','F',',

“T'],dtype=”我猜@Suraj S分析股票时,会选择最有价值的时间来买卖股票

我尝试了另一个例子,我认为@ggaurav答案是一个好方法,我从你的答案中学到了更多

import numpy as np

a=np.array([1,2,3,4,5,6,7,8,1,2,3,4,5,6,4,5,6,7,8,4,2,3,6,4,7,9,4,1,2,3,2,5])

a_bool = a > 5
a_shifted_bool = np.roll(a_bool, 1)
a_shifted_bool[0] = False # first element is last element of a_bool, which we need to ignore

np.where(a_bool & ~a_shifted_bool, 'T', 'F')

数组(['F','F','F','F','F','T','F','F','F','F','F','T','F','F','F','T','T','F','F','F','F','T','F','F','F','T','F','F','F','F','F','F','T','F','F','F','F','F','F','F','F','F','F','F','F','F','F','F','F','F','T dtype='night ide为了使它更接近OP的要求,而不是
np where()<
is
np.roll
vs
df.shift(1)
是相同的,只有一个区别。在
np.roll
中,数组的最后一个元素成为第一个元素,但使用
df.shift(1)
,第一个元素变为
np.nan
-滚动超过最后一个位置的元素在第一个位置重新引入。哦,是的,我尝试股票:)