【问题标题】:Adding results from ckdTree cluster to DataFrame in Pandas在 Pandas 中将 ckdTree 集群的结果添加到 DataFrame
【发布时间】:2018-11-06 00:41:26
【问题描述】:

我正在尝试使用 ckdTree 查找指定距离(1500 m)内的所有数据点。我有一个中心数据框和一个原始数据数据框。我的计划是使用从集群中提取的 x 和 y 坐标来构建符合特定标准的数据点的新数据框。这是我所拥有的:

import numpy as np
import scipy.spatial as spatial
import matplotlib.pyplot as plt

points = perfed[['X', 'Y']].values
centres = producers[['X', 'Y']].values

x_list = []
y_list = []

point_tree = spatial.cKDTree(points)

cmap = plt.get_cmap('rainbow')
colors = cmap(np.linspace(0, 1, len(centres)))
for center, group, color  in zip(centres, point_tree.query_ball_point(centres, 1500), colors):
    cluster = point_tree.data[group]
    x, y = cluster[:, 0], cluster[:, 1]
    x_list.append(pd.Series(x))
    y_list.append(pd.Series(y))
    plt.scatter(x, y, c=color, s=10)

d = {'X': [x_list],
     'Y': [y_list]}

output = pd.DataFrame.from_dict(d,orient='index').transpose()


# output = output.merge(producers, how='left', left_on='X', right_on='X')

plt.show()

输入数据集只是 UTM x 和 y 坐标。谁能发现我在哪里犯了错误?谢谢!

【问题讨论】:

    标签: python pandas scipy scipy-spatial


    【解决方案1】:

    一位同事找到了这个解决方案。它可能会在更少的行中完成,但它确实有效。

    count = 0
    merge_x_list = []
    cluster_x_list = []
    for a in x_list:
        for b in a:
            merge_x_list.append(b)
            cluster_x_list.append(count)
            
        count+=1
        
    count = 0
    merge_y_list = []
    cluster_y_list = []
    for a in y_list:
        for b in a:
            merge_y_list.append(b)
            cluster_y_list.append(count)
            
        count+=1
        
    output = pd.DataFrame(columns=['X', 'Y', 'cluster'])
        
    output['X'] = pd.Series(merge_x_list).values
    output['Y'] = pd.Series(merge_y_list).values
    output['cluster'] = pd.Series(cluster_x_list).values

    【讨论】:

      猜你喜欢
      • 2018-03-17
      • 1970-01-01
      • 2012-12-13
      • 1970-01-01
      • 2012-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多