- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个一维numpy
数组-例如,
a = np.array([1, 4, 5, 7, 1, 2, 2, 4, 10])
N=3
和
x=3
,我将搜索第一个数字,该数字后面的三个条目都小于3。这将是
a[4]
。
for
循环遍历所有值来轻松地实现这一点,但是我想知道是否有更干净,更有效的方法来实现此目的。
最佳答案
方法#1:
这是向量化NumPy的方式-
def start_valid_island(a, thresh, window_size):
m = a<thresh
me = np.r_[False,m,False]
idx = np.flatnonzero(me[:-1]!=me[1:])
lens = idx[1::2]-idx[::2]
return idx[::2][(lens >= window_size).argmax()]
In [44]: a
Out[44]: array([ 1, 4, 5, 7, 1, 2, 2, 4, 10])
In [45]: start_valid_island(a, thresh=3, window_size=3)
Out[45]: 4
In [46]: a[:3] = 1
In [47]: start_valid_island(a, thresh=3, window_size=3)
Out[47]: 0
SciPy's binary-erosion
-
from scipy.ndimage.morphology import binary_erosion
def start_valid_island_v2(a, thresh, window_size):
m = a<thresh
k = np.ones(window_size,dtype=bool)
return binary_erosion(m,k,origin=-(window_size//2)).argmax()
numba
的效率的循环式-
from numba import njit
@njit
def start_valid_island_v3(a, thresh, window_size):
n = len(a)
out = None
for i in range(n-window_size+1):
found = True
for j in range(window_size):
if a[i+j]>=thresh:
found = False
break
if found:
out = i
break
return out
In [142]: np.random.seed(0)
...: a = np.random.randint(0,10,(100000000))
In [145]: %timeit start_valid_island(a, thresh=3, window_size=3)
1 loop, best of 3: 810 ms per loop
In [146]: %timeit start_valid_island_v2(a, thresh=3, window_size=3)
1 loop, best of 3: 1.27 s per loop
In [147]: %timeit start_valid_island_v3(a, thresh=3, window_size=3)
1000000 loops, best of 3: 608 ns per loop
关于python - numpy数组: First occurence of N consecutive values smaller than threshold,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57712650/
我正在将双引号 CSV 转换为 Unix 中的管道分隔 txt 文件。我使用以下 sed 命令将“,”替换为 |然后删除开始和结束双引号。 sed -e 's/","/|/g' -e 's/"//g'
我有一组数据,我需要计算它们的“连续平均值”(我不知道它是否是正确的名称,但我找不到更好的东西),这是一个例子: ID Var2 Var3 1 A 1 2 A 3 3
我目前正在尝试在用户发送消息或取消后,在短信(MFMessageComposeViewController)之后显示电子邮件 View (MFMailComposeViewController)..
题目地址:https://leetcode.com/problems/moving-stones-until-consecutive/ 题目描述 Three stones are on a num
题目地址:https://leetcode.com/problems/consecutive-numbers-sum/ 题目描述 Given a positive integer N, how m
想生成一列b来自 a以至于 if a == 1 then b = 0 else b = counting start from 1 then if a == 1 then b = 0 else b
我有以下 data.table(尽管如果您将它用作 data.frame 也没关系) library(data.table) dt dt first_column second_column
问题:我正在使用 Java,我正在尝试计算字符串中连续的“字符”。 示例: Scanner in = new Scanner(System.in); int n = in.nextInt(); Str
| RecordId | high_speed | speed | DateFrom | DateTo | --------------------------------
我正在尝试使用正则表达式解决问题。我正在使用 Java 正则表达式,它显然类似于 Perl 中的正则表达式。 我想搜索一个字符串,该字符串包含 1-9 中的单个数字,连续重复 3 次或更多次,或者在重
我想在 android 4.2 上按下按钮时连续更改图片此代码仅更改一次 代码: public void addListenerOnButton() { b1 = (Button) findV
开始使用 SPSS/Python,我需要对名称包含两个不同字符串的变量进行平均。我找到了很多单个字符串(或数字等)的示例,但我的字符串并不相邻。 var1_blabla_def_blabla_jkl
我想生成连续的 C++ 字符串,例如在相机中:IMG001、IMG002等能够指示前缀和字符串长度。 我找到了一个可以从具体字符集生成随机字符串的解决方案:link 但是我找不到我想要实现的东西。 最
这个问题不太可能帮助任何 future 的访客;它只与一个小地理区域、一个特定时刻或一个非常狭窄的情况相关,而这些情况通常不适用于互联网的全局受众。如需帮助使这个问题更广泛地适用,visit the
我的办公室大约有大约 300 个网页,应该定期对其进行测试。我在 Visual Studio 2010 中使用 Nunit、Selenium 和 C#。我使用了这个 framework作为基础,我确实
我在用户完成“锻炼”后保存核心数据,并希望能够检查用户锻炼的日期是否按连续顺序排列。我使用下面的代码来检查用户是否在日历上单击的一天进行了锻炼,但无法想象如何检查这些天是否连续。 起初我想获取保存的
我想让我的程序检查数组中任意两个连续的单词是否相同。我相信我的“if”语句是正确的,但是 console.log 显示所有连续的单词都匹配。我在这里想念什么? 感谢任何帮助!我是新手:) var wo
我有一个 Web 服务器,用于保存编号的 Web 应用程序的日志文件。文件名示例如下: dbsclog01s001.log dbsclog01s002.log dbsclog01s003.log 最后
小问题: 我有一个字符串: title="Announcing Elasticsearch.js For Node.js And The Browser" 我想找到每个单词正确大写的所有单词对。 因此
我有一个如下所示的数据框: Text Label a NaN b NaN c NaN 1 NaN 2 NaN b NaN c N
我是一名优秀的程序员,十分优秀!