- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有以下优化问题,我可以通过“蛮力”解决,但我想知道是否有人已经实现了我可以用来更快、更优雅地完成它的求解器。
我有两个不相交的整数列表。这些实际上是独一无二的,所以我可以说这是两套。一个是短的 (s
) 大约有 S=90000
个元素,另一个是长的 (l
) 大约有 L=2.5M
元素。我需要的是从 l
中提取长度正好为 S
的子集 l2
以便 s
的元素之间的总体距离code> 和 l2
是长度为 S
的 l
的所有子集中最小的。 s
和 l
元素之间的成对距离就是它们差值的绝对值。
因此,如果 s
和 l
不相交并且 l
是 s
的超集,则结果 l2
与 s
完全相同。
由于数组很长,通过测试 l
的各个子集来使用蛮力方法是不切实际的。
是否有某种现有的优化库或其他包可以用来解决这个问题?
顺便说一句,可能有不同的方法来测量两个集合之间的距离,我真的不关心它是哪一个,只要它会为上面的极端超集示例给出 0 即可。
最佳答案
我知道你说这些是列表,但有什么理由不暂时将它们转换为 numpy 数组?这可以很简单(如果您不知道如何进行转换):
s = np.array(s)
l = np.array(l)
从那里,您可以使用“searchsorted”功能。我的测试运行时间不到 1.5 秒。
from __future__ import division, print_function
import numpy as np
import datetime as dt
# build numpy array
s = np.random.rand(90000)
l = np.random.rand(2.5E6)
# sort
s.sort()
l.sort()
# searchsorted finds where values in array2 should be inserted in array1 to
# maintain the "sortedness" of a new list
# define index locations where "s" should be inserted in "l"
indices = np.searchsorted(l,s)
# build dummy list to store "s2"
# this is faster than repeatedly resizing an array
s2 = s*0
# using "indices" determine which adjacent value is the nearest match
# need to be careful here since we cannot look "below" the first index
# nor can we look "above" the last value
d1 = dt.datetime.now()
for r in np.arange(s.shape[0]):
ix = indices[r]
if indices[r]==0:
s2[ix] = l[0]
elif indices[r]==l.shape[0]:
s2[ix] = l[r-1]
else:
tmp = l[ix:ix+2]
s2[r] = tmp[ np.abs(s[r]-tmp)==np.min(np.abs(s[r]-tmp)) ]
print('Execution time: ',dt.datetime.now()-d1)
我已经进行了几次试验,看起来这可行,但你自己确认一下。如果这不起作用,则不应花费太多精力来调整它。
将 for 循环更改为:
for r in np.arange(s.shape[0]):
ix = indices[r]
if indices[r]==0:
s2[ix] = l[0]
l[0] = np.nan
elif indices[r]==l.shape[0]:
s2[ix] = l[r-1]
l[r-1] = np.nan
else:
width = 0
while width<min([10,r]) and np.isnan(l[ix-width:ix+2+width].mean()):
width += 1
tmp = l[ix-width:ix+2+width]
s2[r] = tmp[ np.abs(s[r]-tmp)==np.nanmin(np.abs(s[r]-tmp)) ][0]
l[l==s2[r]] = np.nan
这会做两件事:1. 它删除了 l 内最近的邻居,使其在未来的迭代中不被考虑2. 它在 l 内递增地增加搜索宽度以确保找到最近的邻居
同样,这可能需要调整才能拨入。
关于python:如何找到一组中最接近另一组的子集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37445418/
我正在执行大型 ffdf 对象的子集,我注意到当我使用 subset.ff 时,它会生成大量 NA。我通过使用 ffwhich 尝试了另一种方法,结果要快得多,并且没有生成 NA。这是我的测试: li
我对 Prolog 有点陌生。我正在尝试编写一个函数subset(Set, Subset) 来确定Subset 是否是Set 的子集(duh)。另外,如果第二个参数没有实例化,它应该输出每个可能的子集
一、题目 给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。 解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。 二、示例 输入:nums = [1,2
我想从数据帧的操作中排除一列。当然,我可以在没有要排除的列的情况下复制数据框,但这似乎是一种解决方法。我认为必须有一种更简单的方法来进行子集化。 所以这个示例代码应该显示我在做什么。 df colMe
我有一个 SpatialPolygonsDataFrame我通过使用 readOGR 读取 shapefile 创建的在 rgdal包裹。我正在尝试使用它来使用 spsample 生成采样网格在 sp
我想解决一个简单的问题,但即使我尝试了很多不同的方法,我也找不到解决方案。我正在使用 SICStus Prolog (如果这很重要),并且我想获取列表的所有子列表/子集(我不知道哪个术语是正确的),其
我目前正在使用 shinyTable,它是 HandsonTable (https://github.com/trestletech/shinyTable) 的 shiny 兼容实现。巧合的是,我意识
我正在 Delphi 中构建一个表单,其中包含服务下拉列表和用于选择服务的附加组件网格。我获取的数据来自 API,并且我将服务的数据存储在 ADODataSet 中,如下所示: ID (integer
subset() 函数有问题。如何通过观察次数对我的数据框的一个因子进行子集化? NAME CLASS COLOR VALUE antonio
我想知道是否有任何简单的算法来比较一个散列是否是另一个散列的子集。 例如,如果 $HASH{A} = B; $HASH{B} = C; $HASH{C} = D; $HASH2{A} = B; $HA
这个问题在这里已经有了答案: Array combinations without repetition (1 个回答) 关闭 8 年前。 给定一个数组,如何在 postgresql 中找到一定大小
我有下一个程序。我应该如何在 main 中使用迭代器以显示总和为 0 的子集? 我的程序应该打印: 2 -2 5 -5 # include # include using namespace st
我正在寻找一个可以自定义的 Markdown 解析器,最好是 Javascript。特别是,我想删除使用实际 HTML 标签的选项。我尝试编辑摊牌的来源,但无法弄清楚。 Jquery 集成也很好,尽管
我有一个包含名称列表的文件(引用文件 1): Apple Bat Cat 我有另一个文件(引用文件 2),其中包含名称列表和详细信息引用: Apple bla blaa aaaaaaaaagggggg
我有两个带有排序行的文件。一个文件 (B) 是另一个文件 (A) 的子集。我想找到 A 中不在 B 中的所有行。理想情况下,我想创建一个包含这些行的文件 (C)。这在 Unix 中可能吗?我正在寻找一
我有一个包含肽序列的列的数据框,我只想保留字符串中没有内部“R”或“K”的行。 df1 <- data.frame( Peptide = c("ABCOIIJUHFSAUJHR", "AOFI
这个问题在这里已经有了答案: How to subset matrix to one column, maintain matrix data type, maintain row/column na
假设我有一个列表向量: library(tidyverse) d 2) # A tibble: 5 x 1 x 1 2 3 4 5 最佳答案 应该是 lengt
我自己从来没有运行过javadoc(无论是在命令行还是ant's javadoc task;我将使用ant)——我需要为我编写的库生成一个javadoc。 问题是我的 java 库被组织成几个包,在
假设一个多方加密方案,类似于答案:Encryption with multiple different keys? . 那是。一组键K可以用来破译密文。 有没有办法过期: K'⊆ K 这样 K \ K
我是一名优秀的程序员,十分优秀!