[英]Operation between pandas datetime-indexed DataFrames with non-equal elements
[英]Minimum difference between non-equal elements in integer array
我確實有一個包含整數數組的列表,其中每個元素的值<=100。我需要找出每個數組的不相等元素之間的最小差異。 到目前為止,我有以下內容( item
代表一個數組):
unq = numpy.unique(item)
mind = numpy.amin(
(numpy.append(unq, [999]))
-(numpy.append([-999],unq))
)
首先使用numpy
獲得唯一元素的排序數組。 在結尾處添加高正數並在開始處添加高負數后,我將這兩個數組相減並得到最小值。
有沒有更快的方法可以做到這一點?
我認為您的解決方案是可以的,除了可以使用np.diff
( np.diff(np.unique(a))
而不是numpy.append
:
In [1]: import numpy as np
In [2]: a = np.random.randint(0,100,size=50)
In [4]: np.unique(a)
Out[4]:
array([ 0, 2, 3, 5, 7, 8, 15, 18, 20, 22, 23, 27, 30, 31, 32, 33, 37,
38, 42, 43, 45, 48, 49, 57, 59, 62, 65, 70, 74, 75, 76, 78, 79, 80,
83, 84, 88, 91, 93, 94, 96, 98])
In [5]: np.diff(np.unique(a))
Out[5]:
array([2, 1, 2, 2, 1, 7, 3, 2, 2, 1, 4, 3, 1, 1, 1, 4, 1, 4, 1, 2, 3, 1, 8,
2, 3, 3, 5, 4, 1, 1, 2, 1, 1, 3, 1, 4, 3, 2, 1, 2, 2])
In [6]: np.diff(np.unique(a)).min()
Out[6]: 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.