Python 如何在运行DBSCAN后提取x和y值作为新数组?

Python 如何在运行DBSCAN后提取x和y值作为新数组?,python,pandas,scikit-learn,dbscan,Python,Pandas,Scikit Learn,Dbscan,我使用ArcGIS中创建的点列表在python中运行DBSCAN算法。我应用了一个循环来查看增加eps和min_特征值的差异。它工作得很好,我可以将结果绘制在图表上。但我必须再次在ArcGIS Pro上使用集群中的结果点。为了能够做到这一点,我想提取集群中每个点的x和y值——我可以在图表上看到它——并将其保存到.csv或.xlsx文件中。然后我可以可视化地图上我目前正在工作的所有点和簇。问题是我无法打印x和y值,老实说,我是个新手,不知道在哪里可以找到它们。因为我不使用底图,只处理矢量数据,所以

我使用ArcGIS中创建的点列表在python中运行DBSCAN算法。我应用了一个循环来查看增加eps和min_特征值的差异。它工作得很好,我可以将结果绘制在图表上。但我必须再次在ArcGIS Pro上使用集群中的结果点。为了能够做到这一点,我想提取集群中每个点的x和y值——我可以在图表上看到它——并将其保存到.csv或.xlsx文件中。然后我可以可视化地图上我目前正在工作的所有点和簇。问题是我无法打印x和y值,老实说,我是个新手,不知道在哪里可以找到它们。因为我不使用底图,只处理矢量数据,所以我找不到任何其他方法。如果有人能帮助解决这个问题,我将不胜感激。先谢谢你。这是我的密码

import numpy as np

from sklearn.cluster import DBSCAN
from sklearn import metrics
from sklearn.datasets import make_blobs
from sklearn.preprocessing import StandardScaler

import arcpy
import pandas as pd
import xlsxwriter as xlsw
import matplotlib.pyplot as plt

in_workspace = r'D:\00_Experiment\DWG_Participants\Database'
arcpy.env.workspace = in_workspace
arcpy.env.overwriteOutput = True
# #############################################################################
# Generate sample data

input = r"D:\00_Experiment\DWG_Participants\Database\DbScan.gdb\FP_Centroids"
arr = arcpy.da.TableToNumPyArray(input, ("Longitude", "Latitude"))

cen_df = pd.DataFrame(arr)

centers = [[1, 1], [-1, -1], [1, -1]]
cen_df, labels_true = make_blobs(n_samples=len(cen_df), centers=centers, cluster_std=0.4, 
随机_状态=0)


对不起,我不知道我是否理解正确。您已经创建了x和y值的绘图,您想知道已绘制的x和y值吗?这意味着您要查找的值位于变量
xy
?非常感谢您的评论@tomsal。当我提取变量xy时,它是完全空的,但我仍然可以看到图表上的值。我不知道那里缺少什么,我仍然不知道你想做什么。是否要存储包含x、y和标签的表格?一个旁注:我不知道这是否是由于格式,但可能是您运行的代码没有正确的缩进?在我在你的帖子中看到的位置,它只会作用于最后一组
xy
。谢谢@tomsal。我用完全不同的方式重写了剧本。问题在于以错误的方式将make_blob和dbscan一起使用。就我的情况而言,不需要make_blobs,我完全删除了该部分。它工作得很好。对不起,我不确定我是否理解正确。您已经创建了x和y值的绘图,您想知道已绘制的x和y值吗?这意味着您要查找的值位于变量
xy
?非常感谢您的评论@tomsal。当我提取变量xy时,它是完全空的,但我仍然可以看到图表上的值。我不知道那里缺少什么,我仍然不知道你想做什么。是否要存储包含x、y和标签的表格?一个旁注:我不知道这是否是由于格式,但可能是您运行的代码没有正确的缩进?在我在你的帖子中看到的位置,它只会作用于最后一组
xy
。谢谢@tomsal。我用完全不同的方式重写了剧本。问题在于以错误的方式将make_blob和dbscan一起使用。就我的情况而言,不需要make_blobs,我完全删除了该部分。它工作得很好。
cen_df = StandardScaler().fit_transform(cen_df)

# #############################################################################
# Compute DBSCAN
a = 0.2
b = 0.5

for i in range(1):
    a = a + 0.1
    b = b + 0.5
    db = DBSCAN(eps=a, min_samples=b).fit(cen_df)
    core_samples_mask = np.zeros_like(db.labels_, dtype=bool)
    core_samples_mask[db.core_sample_indices_] = True
    labels = db.labels_

# Number of clusters in labels, ignoring noise if present.
    n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
    n_noise_ = list(labels).count(-1)

    print('Estimated number of clusters: %d' % n_clusters_)
    print('Estimated number of noise points: %d' % n_noise_)
    print("Homogeneity: %0.3f" % metrics.homogeneity_score(labels_true, labels))
    print("Completeness: %0.3f" % metrics.completeness_score(labels_true, labels))
    print("V-measure: %0.3f" % metrics.v_measure_score(labels_true, labels))
    print("Adjusted Rand Index: %0.3f" % metrics.adjusted_rand_score(labels_true, labels))
    print("Adjusted Mutual Information: %0.3f" % metrics.adjusted_mutual_info_score(labels_true, labels))
    print("Silhouette Coefficient: %0.3f" % metrics.silhouette_score(cen_df, labels))

# Black removed and is used for noise instead.
    unique_labels = set(labels)
    colors = [plt.cm.Spectral(each)
    for each in np.linspace(0, 1, len(unique_labels))]
    for k, col in zip(unique_labels, colors):
        if k == -1:
        # Black used for noise.
            col = [0, 0, 0, 1]

        class_member_mask = (labels == k)

        xy = cen_df[class_member_mask & core_samples_mask]
        plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col), markeredgecolor='k', markersize=14)

        xy = cen_df[class_member_mask & ~core_samples_mask]
        plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col), markeredgecolor='k', markersize=6)

    ###################
    #I tried this code for xy values and labels but the excel file does not contain what I am looking for 
    #new_array = np.array(labels)
    #df = pd.DataFrame(labels)
    #df = df.transpose()
    #xlsfile = r"D:\00_Experiment\DWG_Participants\Database\output_dbscan.gdb\fp.xlsx"
    #writer = pd.ExcelWriter(xlsfile, engine="xlsxwriter")
    #df.to_excel(writer, sheet_name="table_fp",startrow=1, startcol=1, header=False, index=False)
    #writer.save()
    #######################

    plt.title('Estimated number of clusters: %d' % n_clusters_)
    plt.show()enter code here