Python,Numpy - 尝试根据条件拆分数组

标签 python numpy scipy

我正在尝试在数组中查找簇(即数组中的组,其中 [n+1] 和 [n] 之间的差异小于某个值)。我有一个 numpy 数组,它是一个时间戳序列。我可以使用 numpy.diff() 找到时间戳之间的差异,但是我很难在不遍历数组的情况下尝试确定簇。举个例子:

t = t = np.array([ 147, 5729, 5794, 5806, 6798, 8756, 8772, 8776, 9976])
dt  = np.diff(t)
dt = array([5582,   65,   12,  992, 1958,   16,    4, 1200])

如果我的集群条件是 dt < 100 t[1]、t[2] 和 t[3] 将是一个集群,而 t[5]、t[6] 和 t[7] 将是另一个集群。我试过使用 numpy.where(),但我没有成功地调整条件以分离出集群,即

cluster1 = np.array([5729, 5794, 5806])
cluster2 = np.array([8756, 8772, 8776])

或者类似的东西。

感谢任何帮助。

最佳答案

import numpy as np

t = np.array([ 147, 5729, 5794, 5806, 6798, 8756, 8772, 8776, 9976])
dt  = np.diff(t)
pos = np.where(dt > 100)[0] + 1
print np.split(t, pos)

输出是:

[array([147]), 
array([5729, 5794, 5806]), 
array([6798]), 
array([8756, 8772, 8776]), 
array([9976])]

关于Python,Numpy - 尝试根据条件拆分数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9389629/

相关文章:

python - 合并 Pandas 中的数据框(没有列名称)

python - scrapy:CrawlSpider 中的 'exceptions.KeyError'

python - Matplotlib imshow - 显示不同的颜色

python - 通过近似相等的数字比较左加入 Pandas

python - 从二维数组中提取一个 block

python - Python 中的快速傅立叶变换

python - Python 中的多元回归

python - Scipy.interpolate 获取产生特定 y 值的 x 值 2 个数字列表

Python:如何仅使用 while 循环返回列表而不修改原始列表?

python-3.x - Scipy stats.mode 没有返回最大发生值