将来自 ckdTree 集群的结果添加到 Pandas 中的 DataFrame
Adding results from ckdTree cluster to DataFrame in Pandas
我正在尝试使用 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 坐标。任何人都可以发现我在哪里犯了错误吗?谢谢!
一位同事找到了这个解决方案。它可能会在更少的行中完成,但它有效。
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
我正在尝试使用 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 坐标。任何人都可以发现我在哪里犯了错误吗?谢谢!
一位同事找到了这个解决方案。它可能会在更少的行中完成,但它有效。
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