是否有一种numpy-thonic方式,例如函数,来查找数组中最接近的值?
例:
np.find_nearest( array, value )
Run Code Online (Sandbox Code Playgroud) 在Python中查找列表中元素索引的好方法是什么?
请注意,列表可能未排序.
有没有办法指定要使用的比较运算符?
我在一个数据帧中有一个时间戳,我试图匹配第二个数据帧中最接近的时间戳,以便从第二个数据帧中提取数据.请参阅下面的我的方法的一般示例:
library(lubridate)
data <- data.frame(datetime=ymd_hms(c('2015-04-01 12:23:00 UTC', '2015-04-01 13:49:00 UTC', '2015-04-01 14:06:00 UTC' ,'2015-04-01 14:49:00 UTC')),
value=c(1,2,3,4))
reference <- data.frame(datetime=ymd_hms(c('2015-04-01 12:00:00 UTC', '2015-04-01 13:00:00 UTC', '2015-04-01 14:00:00 UTC' ,'2015-04-01 15:00:00 UTC', '2015-04-01 16:00:00 UTC')),
refvalue=c(5,6,7,8,9))
data$refvalue <- apply(data, 1, function (x){
differences <- abs(as.numeric(difftime(ymd_hms(x['datetime']), reference$datetime)))
mindiff <- min(differences)
return(reference$refvalue[differences == mindiff])
})
data
# datetime value refvalue
# 1 2015-04-01 12:23:00 1 5
# 2 2015-04-01 13:49:00 2 7
# 3 2015-04-01 14:06:00 3 7
# 4 2015-04-01 14:49:00 4 …Run Code Online (Sandbox Code Playgroud) 我有一个值数组t,它总是按递增的顺序(但不总是均匀间隔).我有另一个单值x.我需要在t中找到索引,使得t [index]最接近x.函数必须为x <t.min()返回零,并且x> t.max()的最大索引(或-1).
我写了两个函数来做到这一点.在这个简单的定时测试中,第一个f1更快.但我喜欢第二个只是一行.此计算将在大型阵列上完成,可能每秒多次.
任何人都可以提出一些其他功能与第一个相似的时间,但具有更清晰的代码?比第一个更快的东西怎么样(速度最重要)?
谢谢!
码:
import numpy as np
import timeit
t = np.arange(10,100000) # Not always uniform, but in increasing order
x = np.random.uniform(10,100000) # Some value to find within t
def f1(t, x):
ind = np.searchsorted(t, x) # Get index to preserve order
ind = min(len(t)-1, ind) # In case x > max(t)
ind = max(1, ind) # In case x < min(t)
if x < (t[ind-1] + t[ind]) / 2.0: # Closer to the …Run Code Online (Sandbox Code Playgroud) 我有一个具有唯一正整数的数组/集,即
>>> unique = np.unique(np.random.choice(100, 4, replace=False))
Run Code Online (Sandbox Code Playgroud)
并且包含从前一个数组中采样的多个元素的数组,例如
>>> A = np.random.choice(unique, 100)
Run Code Online (Sandbox Code Playgroud)
我想将数组的值映射A到这些值出现的位置unique.
到目前为止,我找到的最佳解决方案是通过映射数组:
>>> table = np.zeros(unique.max()+1, unique.dtype)
>>> table[unique] = np.arange(unique.size)
Run Code Online (Sandbox Code Playgroud)
上面为每个元素分配了数组上的索引,因此可以在以后用于映射A高级索引:
>>> table[A]
array([2, 2, 3, 3, 3, 3, 1, 1, 1, 0, 2, 0, 1, 0, 2, 1, 0, 0, 2, 3, 0, 0, 0,
0, 3, 3, 2, 1, 0, 0, 0, 2, 1, 0, 3, 0, 1, 3, 0, 1, 2, 3, 3, 3, 3, 1,
3, …Run Code Online (Sandbox Code Playgroud) 我有两个numpy数组,比如X=[x1,x2,x3,x4], y=[y1,y2,y3,y4].其中三个元素很接近,第四个元素可能接近或不接近.
喜欢:
X [ 84.04467948 52.42447842 39.13555678 21.99846595]
y [ 78.86529444 52.42447842 38.74910101 21.99846595]
Run Code Online (Sandbox Code Playgroud)
或者它可以是:
X [ 84.04467948 60 52.42447842 39.13555678]
y [ 78.86529444 52.42447842 38.74910101 21.99846595]
Run Code Online (Sandbox Code Playgroud)
我想定义一个函数来查找两个数组中的相应索引,如第一种情况:
y[0]对应X[0],y[1]对应X[1],y[2]对应X[2],y[3] 相当于 X[3]在第二种情况下:
y[0]对应X[0],y[1]对应X[2],y[2] 相当于 X[3] y[3]对应于X[1].我不能写一个完全解决问题的功能,请帮忙.
known_array:numpy数组; 仅由标量值组成;shape: (m, 1)
test_array:numpy数组; 仅由标量值组成;shape: (n, 1)
indices:numpy数组; shape: (n, 1); 对于每个值,test_array查找最接近的值的索引known_array
residual:numpy数组; shape: (n, 1); 对于每个值,test_array找到与最接近的值的差异known_array
In [17]: known_array = np.array([random.randint(-30,30) for i in range(5)])
In [18]: known_array
Out[18]: array([-24, -18, -13, -30, 29])
In [19]: test_array = np.array([random.randint(-10,10) for i in range(10)])
In [20]: test_array
Out[20]: array([-6, 4, -6, 4, 8, -4, 8, -6, 2, 8])
Run Code Online (Sandbox Code Playgroud)
def find_nearest(known_array, value): …Run Code Online (Sandbox Code Playgroud) 根据在另一个数组中找到的最近浮点数"过滤"数组时,我遇到了性能问题.
这是一个MWE问题:
import numpy as np
def random_data(N):
# Generate some random data.
return np.random.uniform(0., 10., N).tolist()
# Data lists.
N1 = 1500
list1 = [random_data(N1), random_data(N1), random_data(N1)]
list2 = random_data(1000)
# Define list1's range.
min_1, max_1 = min(list1[2]), max(list1[2])
# This list will contain the "filtered" list1.
list4 = [[], [], []]
# Go through each element in list2.
for elem2 in list2:
# If it is located within the list1 range.
if min_1 <= elem2 <= …Run Code Online (Sandbox Code Playgroud) 我试图在ndarray中找到一个浮点数。由于我使用的软件包(Abaqus),它输出的精度有点低。例如,10类似于10.00003。因此,我想知道是否有一种“正确”的方法可以做到,这比我的代码更整洁。
示例代码:
import numpy as np
array = np.arange(10)
number = 5.00001
Run Code Online (Sandbox Code Playgroud)
如果我这样做?
idx = np.where(number==array)[0][0]
Run Code Online (Sandbox Code Playgroud)
然后结果为空,因为5.00001不等于5。
现在我正在做:
atol = 1e-3 # Absolute tolerance
idx = np.where(abs(number-array) < atol)[0][0]
Run Code Online (Sandbox Code Playgroud)
可以,并且不会太杂乱...但是我想知道是否会有更整洁的方式来做到这一点。谢谢!
PS:这numpy.allclose()是另一种实现方法,但是我需要使用number * np.ones([array.shape[0], array.shape[1]]),对我来说似乎仍然很冗长...
编辑:非常感谢大家的奇妙答案!np.isclose()是我要查找的确切函数,由于它不在文档中,所以我错过了它。如果不是您,我不会意识到这一点,除非他们更新了文档。再次感谢你!