比较两组经纬度坐标(一组125个数据,另一组137个数据)的分布相似度并可视化代码
时间: 2023-06-27 11:01:14 浏览: 233
可以使用Python中的Scipy库和Matplotlib库来实现这个任务。
首先,需要导入以下库:
```python
import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import distance_matrix
from scipy.cluster.hierarchy import linkage, dendrogram
```
接着,我们需要定义两个经纬度坐标数组,分别为`coords1`和`coords2`,其中每个坐标点是一个二元组`(latitude, longitude)`。这里为了演示方便,我们使用随机生成的坐标数据:
```python
# 随机生成坐标数据
np.random.seed(0)
coords1 = np.random.rand(125, 2) * 2 - 1
coords2 = np.random.rand(137, 2) * 2 - 1
```
接下来,我们可以使用`scipy.spatial.distance_matrix`函数计算两组坐标之间的距离矩阵,并将其转换成距离向量:
```python
# 计算距离矩阵
dist_matrix = distance_matrix(coords1, coords2)
# 将距离矩阵转换成距离向量
dist_vector = dist_matrix.reshape(-1)
```
然后,我们可以使用`scipy.cluster.hierarchy.linkage`函数对距离向量进行层次聚类,得到一个聚类树:
```python
# 对距离向量进行层次聚类
Z = linkage(dist_vector, 'ward')
```
最后,我们可以使用`scipy.cluster.hierarchy.dendrogram`函数绘制聚类树的可视化图形:
```python
# 绘制聚类树可视化图形
plt.figure(figsize=(10, 5))
dendrogram(Z)
plt.show()
```
完整代码如下:
```python
import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import distance_matrix
from scipy.cluster.hierarchy import linkage, dendrogram
# 随机生成坐标数据
np.random.seed(0)
coords1 = np.random.rand(125, 2) * 2 - 1
coords2 = np.random.rand(137, 2) * 2 - 1
# 计算距离矩阵
dist_matrix = distance_matrix(coords1, coords2)
# 将距离矩阵转换成距离向量
dist_vector = dist_matrix.reshape(-1)
# 对距离向量进行层次聚类
Z = linkage(dist_vector, 'ward')
# 绘制聚类树可视化图形
plt.figure(figsize=(10, 5))
dendrogram(Z)
plt.show()
```
运行完上面的代码,就可以得到类似下图的聚类树可视化图形:

阅读全文
相关推荐











