python - 查找数组中的最大连续次数值小于阈值
问题描述
我有以下 numpy 数组
array([0.66594665, 0.33003433, NaN, 0.42567293, 0.48161913, 0.30000838, 0.13639367, 0.84300475, 0.19029748, NaN])
我想找到数组中的值连续小于 0.5 的次数。有没有办法在不使用 for 循环的情况下做到这一点?在此示例中,以下子序列的答案为 4:0.42567293, 0.48161913, 0.30000838, 0.13639367
解决方案
import numpy as np
# Create a numpy array
arr = np.array([0.66594665, 0.33003433, np.nan, 0.42567293, 0.48161913, 0.30000838, 0.13639367, 0.84300475, 0.19029748, np.nan])
# Create a numpy array with consecutive values less than 0.5
arr_less_than_0_5 = np.where(arr < 0.5)[0]
# Print the array
print(arr_less_than_0_5)
# Get the number of consecutive times the values in a numpy array are less than 0.5
print(len(arr_less_than_0_5))
问题是给出值小于 0.5 的连续次数。它没有被要求打印特定的值。
所以这回答了你的问题
推荐阅读
- javascript - 查询同一字段的多个 Firestore 集合
- android - 如何在 apk 构建过程中启用 xml 和 json 文件的缩小?
- google-apps-script - 如何检索 IMPORTRANGE 更新谷歌表格中数据的确切时间?
- linux - Cups - 可能的 DDoS 和损坏的管道 (errno=32)
- github - 如何在 GitHub 操作上获取分支名称?
- xaml - 删除不需要的空白
- javascript - 在 HTA 应用程序内的 WScript.Shell 中发送输入值
- arrays - 移入双向链表 Ramda.js
- python - 当我尝试使用 cx_freeze 构建时,如何解决 pywintypes.error?
- c# - dotnet core 3 - 获取 System.NullReferenceException 可能是因为我没有加载正确的相关实体