- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试使用 DBSCAN(scikit 学习实现)和位置数据进行聚类。我的数据是 np 数组格式,但要将 DBSCAN 与 Haversine 公式一起使用,我需要创建一个距离矩阵。当我尝试执行此操作时出现以下错误(“模块”不可调用错误。)根据我在网上阅读的内容,这是一个导入错误,但我很确定我不是这种情况。我已经创建了自己的半正弦距离公式,但我确信错误不在于此。
这是我的输入数据,一个 np 数组 (ResultArray)。
[[ 53.3252628 -6.2644198 ]
[ 53.3287395 -6.2646543 ]
[ 53.33321202 -6.24785807]
[ 53.3261015 -6.2598324 ]
[ 53.325291 -6.2644105 ]
[ 53.3281323 -6.2661467 ]
[ 53.3253074 -6.2644483 ]
[ 53.3388147 -6.2338417 ]
[ 53.3381102 -6.2343826 ]
[ 53.3253074 -6.2644483 ]
[ 53.3228188 -6.2625379 ]
[ 53.3253074 -6.2644483 ]]
这是出错的代码行。
distance_matrix = sp.spatial.distance.squareform(sp.spatial.distance.pdist
(ResultArray,(lambda u,v: haversine(u,v))))
这是错误信息:
File "Location.py", line 48, in <module>
distance_matrix = sp.spatial.distance.squareform(sp.spatial.distance.pdist
(ResArray,(lambda u,v: haversine(u,v))))
File "/usr/lib/python2.7/dist-packages/scipy/spatial/distance.py", line 1118, in pdist
dm[k] = dfun(X[i], X[j])
File "Location.py", line 48, in <lambda>
distance_matrix = sp.spatial.distance.squareform(sp.spatial.distance.pdist
(ResArray,(lambda u,v: haversine(u,v))))
TypeError: 'module' object is not callable
我将 scipy 导入为 sp。 (将 scipy 导入为 sp)
最佳答案
使用 Scipy,您可以按照此 link 上的文档的建议定义自定义距离函数为方便起见,在此报告:
Y = pdist(X, f)
Computes the distance between all pairs of vectors in X using the user supplied 2-arity function f. For example, Euclidean distance between the vectors could be computed as follows:
dm = pdist(X, lambda u, v: np.sqrt(((u-v)**2).sum()))
在这里,我报告了我的代码版本,其灵感来源于此 link 中的代码。 :
from numpy import sin,cos,arctan2,sqrt,pi # import from numpy
# earth's mean radius = 6,371km
EARTHRADIUS = 6371.0
def getDistanceByHaversine(loc1, loc2):
'''Haversine formula - give coordinates as a 2D numpy array of
(lat_denter link description hereecimal,lon_decimal) pairs'''
#
# "unpack" our numpy array, this extracts column wise arrays
lat1 = loc1[1]
lon1 = loc1[0]
lat2 = loc2[1]
lon2 = loc2[0]
#
# convert to radians ##### Completely identical
lon1 = lon1 * pi / 180.0
lon2 = lon2 * pi / 180.0
lat1 = lat1 * pi / 180.0
lat2 = lat2 * pi / 180.0
#
# haversine formula #### Same, but atan2 named arctan2 in numpy
dlon = lon2 - lon1
dlat = lat2 - lat1
a = (sin(dlat/2))**2 + cos(lat1) * cos(lat2) * (sin(dlon/2.0))**2
c = 2.0 * arctan2(sqrt(a), sqrt(1.0-a))
km = EARTHRADIUS * c
return km
并通过以下方式调用:
D = spatial.distance.pdist(A, lambda u, v: getDistanceByHaversine(u,v))
在我的实现中,矩阵 A 的第一列是经度值,第二列是以十进制表示的纬度值。
关于python - 使用带有 pdist 和 squareform 的 nparray 创建距离矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22081503/
目前我正在使用 Matlab 中的 pdist 函数来计算三维笛卡尔系统中各点之间的欧几里得距离。我这样做是因为我想知道哪个点到所有其他点(中心点)的平均距离最小。 pdist 的语法如下所示: %
scipy.spatial.distance.squareform 有反函数吗?如果不是,编写它来处理巨大距离矩阵的最佳方法是什么? 最佳答案 根据文档,squareform 是它自己的逆: http
我正在尝试使用 DBSCAN(scikit 学习实现)和位置数据进行聚类。我的数据是 np 数组格式,但要将 DBSCAN 与 Haversine 公式一起使用,我需要创建一个距离矩阵。当我尝试执行此
我使用pandas对于我所有的平方距离/相似性/相异性矩阵,直到最近它都工作得很好。我一直在处理越来越大的数据集,创建/存储这些具有 100k 属性的成对数据点更是一项艰巨的任务(1e5**2 = 1
我是一名优秀的程序员,十分优秀!