我正在尝试使用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坐标。谁能发现我在哪里出错?谢谢!
答案 0 :(得分:0)
一个同事找到了这个解决方案。可能可以用更少的行来完成,但是行得通。
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