python实现空间数据聚类-DBSCAN

    xiaoxiao2022-07-04  116

    主要思想就是读取坐标值进行聚类,坐标来源包括shp,txt,数据库

    # encoding: utf-8 import arcpy import numpy as np import pandas as pd from sklearn.cluster import DBSCAN outgdb=r"D:\84.gdb" lsls=[] with open(r"C:\Users\h\Desktop\d2.txt",'r')as df: for line in df: line=line.split(',') ls=[] for value in line: ls.append(value) lsls.append(ls) data=pd.DataFrame(lsls,columns=[ "vehicle", "sj", "wj","x","y","wz","speed","dir","state","carstate","md"]) df1 = data.ix[:, 3:5] convert = df1.values print "聚类" y_pred = DBSCAN(eps=5, min_samples=5).fit(convert) lable = y_pred.labels_ lableC = lable.reshape(lable.size, 1) uniqueValue=np.unique(lable) res = np.hstack((convert , lableC)) arr_res=[] arr_clu=[] for lb in uniqueValue: if(lb!=-1): arr_new = res[res[:, res.shape[1]-1] ==lb, :res.shape[1]-1] lsls=[] for row in arr_new: ls=[] ls.append(float(row[0])) ls.append(float(row[1])) lsls.append(ls) arr_new=np.array(lsls) arr_mean=np.mean(arr_new, axis=0) arr_res.append(arr_mean) arr_clu.append(lb) print "导出" if(len(arr_res)>-1): df=pd.DataFrame(arr_res,columns=["x","y"]) df.to_excel("1.xls")
    最新回复(0)