Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python列表中查找类似项_Python_Python 3.x_Performance_Time Complexity - Fatal编程技术网

在python列表中查找类似项

在python列表中查找类似项,python,python-3.x,performance,time-complexity,Python,Python 3.x,Performance,Time Complexity,我有两个元组列表list1=[(1.332,3.23344,3.22),(2.122,2.11,2.33),…(1,2,3)]和list2=[(4.23,12.2,3.333),(1.234,3.21,4.342),…(1.1,2.2,3.3)]。这两个列表都很长,两个列表都有数百万个。对于上下文,这些数据点中的每一个都是对两个不同数据集中位置的某种度量。现在我想将list1中的每个条目与list2中的条目对应起来,如果它“足够接近”。“足够近”是指位置之间的距离小于某个阈值(例如,1)。我最初

我有两个元组列表
list1=[(1.332,3.23344,3.22),(2.122,2.11,2.33),…(1,2,3)]
list2=[(4.23,12.2,3.333),(1.234,3.21,4.342),…(1.1,2.2,3.3)]
。这两个列表都很长,两个列表都有数百万个。对于上下文,这些数据点中的每一个都是对两个不同数据集中位置的某种度量。现在我想将
list1
中的每个条目与
list2
中的条目对应起来,如果它“足够接近”。“足够近”是指位置之间的距离小于某个阈值(例如,1)。我最初的想法是对
list1
中的每个条目使用
min
函数。即:

import numpy as np
import random

def dist(pt1, pt2): 
    return np.sqrt( ((pt2[0] - pt1[0]) ** 2) + ((pt2[1] - pt1[1]) ** 2) + ((pt2[2] - pt1[2]) ** 2) ) 

list1 = [(random.random(), random.random(), random.random()) for _ in range(25)]                                                                                              

list2 = [(random.random(), random.random(), random.random()) for _ in range(20)]   

threshold = .5
linker = []
for i, entry in enumerate(list1): 
    m = min(list2, key=lambda x: dist(entry, x)) 
    if dist(entry, m) < threshold: 
         linker.append((i, list2.index(m))
将numpy导入为np
随机输入
def区(pt1、pt2):
返回np.sqrt(((pt2[0]-pt1[0])**2+((pt2[1]-pt1[1])**2+((pt2[2]-pt1[2])**2))
列表1=[(random.random(),random.random(),random.random())表示范围(25)]
列表2=[(random.random(),random.random(),random.random())表示范围(20)]
阈值=.5
链接器=[]
对于i,枚举中的条目(列表1):
m=min(列表2,键=lambda x:dist(条目,x))
如果距离(输入,m)<阈值:
linker.append((i,list2.index(m))

因此,这会将
list1
中的每个索引链接到
list2
中的索引。但我觉得一定有一些专门为这项任务开发的算法,速度要快得多,是吗?

是的,这绝对是一种耗时的方法,因为第一个python并没有针对这些计算进行优化(对于数据类型等)第二,这些计算需要用任何语言进行优化。 必须使用库来操作矩阵,例如numpy和pandas。 例如,在您的案例中,我建议使用以下解决方案: 第一:将您的数据转换为熊猫的数据帧,如本文所示: 第二:在与熊猫进行转换后,这是一个常规且易于计算的过程。 例如:


pandas使用numpy,并且numpy针对这些计算进行了优化。

是的,这绝对是一种耗时的方法,因为第一种python没有针对这些计算(针对数据类型等)进行优化,第二种计算需要用任何语言进行优化。 必须使用库来操作矩阵,例如numpy和pandas。 例如,在您的案例中,我建议使用以下解决方案: 第一:将您的数据转换为熊猫的数据帧,如本文所示: 第二:在与熊猫进行转换后,这是一个常规且易于计算的过程。 例如:


pandas使用numpy,numpy针对这些计算进行了优化。

一个简单的解决方案是保留一个3d单元格数组以将条目分组。例如,
(1.332,3.23344,3.22)
可能会分组到单元格
(13,32,32)
。数据结构打包后,您可以找到
附近的所有点(1.332,3.23344,3.22)
,通过查看
(13,32,32)
(及其26个邻居中的一些子集)


如果你真的需要这样做的话,你需要学习一套称为“空间分割器”的算法。你可以研究一种名为“kd树”的东西,它非常适合以超紧凑的方式存储点的非均匀分布(并且是为在某个位置检索邻域中的点而优化的)一个简单的解决方案是保留一个3d单元格数组,以便将条目分组。例如,
(1.332,3.23344,3.22)
可以分组到单元格
(13,32,32)
。数据结构打包后,您可以通过查看
(13,32,32)
找到
(1.332,3.23344,3.22)
附近的所有点(以及它26个邻居中的一些子集。)


如果你真的需要这样做的话,你需要学习一套称为“空间分割器”的算法。你可以研究一种名为“kd树”的东西,它非常适合以超紧凑的方式存储点的非均匀分布(并且是为在某个位置检索邻域中的点而优化的)

您正在查找数据集中每个点到第二个数据集中的最近邻居

  • 您发布的方法的复杂性为O(N^2)
  • 因为N~100万,这就变得站不住脚了
  • 对于大型数据集,由于其复杂性为O(N*log(N))而更好

    Python中两个流行的是

    使用BallTree解决此问题的示例

    输出

    Y index 0, closest index X is [3], dist [0.14046915]
    Y index 1, closest index X is [1], dist [0.40653272]
    Y index 2, closest index X is [7], dist [0.29291477]
    Y index 3, closest index X is [1], dist [0.25785655]
    Y index 4, closest index X is [1], dist [0.39477652]
    Y index 5, closest index X is [9], dist [0.50373484]
    Y index 6, closest index X is [1], dist [0.24894356]
    Y index 7, closest index X is [4], dist [0.14716665]
    Y index 8, closest index X is [5], dist [0.25875381]
    Y index 9, closest index X is [8], dist [0.24204497]
    

    您正在查找数据集中每个点到第二个数据集中的最近邻居

  • 您发布的方法的复杂性为O(N^2)
  • 因为N~100万,这就变得站不住脚了
  • 对于大型数据集,由于其复杂性为O(N*log(N))而更好

    Python中两个流行的是

    使用BallTree解决此问题的示例

    输出

    Y index 0, closest index X is [3], dist [0.14046915]
    Y index 1, closest index X is [1], dist [0.40653272]
    Y index 2, closest index X is [7], dist [0.29291477]
    Y index 3, closest index X is [1], dist [0.25785655]
    Y index 4, closest index X is [1], dist [0.39477652]
    Y index 5, closest index X is [9], dist [0.50373484]
    Y index 6, closest index X is [1], dist [0.24894356]
    Y index 7, closest index X is [4], dist [0.14716665]
    Y index 8, closest index X is [5], dist [0.25875381]
    Y index 9, closest index X is [8], dist [0.24204497]
    

    感谢您的回答和解释,了解kd树:)感谢您的回答和解释,了解kd树:)