栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

两个不同的Numpy数组中的点之间的最小欧几里得距离,不在

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

两个不同的Numpy数组中的点之间的最小欧几里得距离,不在

(数月后)

scipy.spatial.distance.cdist( X, Y )
给出了X和Y的所有距离对,2个暗角,3个暗角……
它也有22个不同的范数,在此处详细介绍

# cdist example: (nx,dim) (ny,dim) -> (nx,ny)from __future__ import divisionimport sysimport numpy as npfrom scipy.spatial.distance import cdist#...............................................................................dim = 10nx = 1000ny = 100metric = "euclidean"seed = 1    # change these params in sh or ipython: run this.py dim=3 ...for arg in sys.argv[1:]:    exec( arg )np.random.seed(seed)np.set_printoptions( 2, threshold=100, edgeitems=10, suppress=True )title = "%s  dim %d  nx %d  ny %d  metric %s" % (        __file__, dim, nx, ny, metric )print "n", title#...............................................................................X = np.random.uniform( 0, 1, size=(nx,dim) )Y = np.random.uniform( 0, 1, size=(ny,dim) )dist = cdist( X, Y, metric=metric )  # -> (nx, ny) distances#...............................................................................print "scipy.spatial.distance.cdist: X %s Y %s -> %s" % (        X.shape, Y.shape, dist.shape )print "dist average %.3g +- %.2g" % (dist.mean(), dist.std())print "check: dist[0,3] %.3g == cdist( [X[0]], [Y[3]] ) %.3g" % (        dist[0,3], cdist( [X[0]], [Y[3]] ))# (trivia: how do pairwise distances between uniform-random points in the unit cube# depend on the metric ? With the right scaling, not much at all:# L1 / dim      ~ .33 +- .2/sqrt dim# L2 / sqrt dim ~ .4 +- .2/sqrt dim# Lmax / 2      ~ .4 +- .2/sqrt dim


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/624045.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号