长度不同的元素列表之间的相似性演算

发布于 2025-01-27 03:57:46 字数 172 浏览 1 评论 0原文

我需要生成一个与python不同长度(显示矩阵相似性以外的相似性)的元素列表之间的数据框或矩阵。

x=[(15,7),(12,7),(80,3),(10,8)]   y=[(12,4),(11,9)]    z=[(1,1),(16,7),(0,3),(10,5),(3,44)]

I need to generate a dataframe or matrix with distance between lists of tuples which are of different lengths (any distance showing similarity other than matrix similarity) with Python.

x=[(15,7),(12,7),(80,3),(10,8)]   y=[(12,4),(11,9)]    z=[(1,1),(16,7),(0,3),(10,5),(3,44)]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅听莫相离 2025-02-03 03:57:46

每个列表似乎都是2D点的集合。计算每组质量的中心。计算每对质量中心之间的欧几里得距离,并将其放在方形矩阵中。

import math

def center_of_mass(points):
    x = 0
    y = 0
    
    for point in points:
        x += point[0]
        y += point[1]
    
    x /= len(points)
    y /= len(points)
    
    return x, y

def euclidian_distance(point1, point2):
    return math.sqrt((point1[0] - point2[0])*(point1[0] - point2[0]) + (point1[1] - point2[1])*(point1[1] - point2[1]))
    
def get_distance_matrix(data):
    centers_of_mass = [center_of_mass(points) for points in data]
    distance_matrix = [[0.0 for i in range(len(centers_of_mass))] for j in range(len(centers_of_mass))]
    
    for i in range(len(centers_of_mass)):
        for j in range(len(centers_of_mass)):
            distance_matrix[i][j] = euclidian_distance(centers_of_mass[i], centers_of_mass[j])
    
    return distance_matrix  

Each list can be seem as a set of 2D points. Calculate the center of mass of each set. Calculate the euclidian distance between every pair of center of mass and put it in a square matrix.

import math

def center_of_mass(points):
    x = 0
    y = 0
    
    for point in points:
        x += point[0]
        y += point[1]
    
    x /= len(points)
    y /= len(points)
    
    return x, y

def euclidian_distance(point1, point2):
    return math.sqrt((point1[0] - point2[0])*(point1[0] - point2[0]) + (point1[1] - point2[1])*(point1[1] - point2[1]))
    
def get_distance_matrix(data):
    centers_of_mass = [center_of_mass(points) for points in data]
    distance_matrix = [[0.0 for i in range(len(centers_of_mass))] for j in range(len(centers_of_mass))]
    
    for i in range(len(centers_of_mass)):
        for j in range(len(centers_of_mass)):
            distance_matrix[i][j] = euclidian_distance(centers_of_mass[i], centers_of_mass[j])
    
    return distance_matrix  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文