轨迹聚类DBSCAN方法代码

轨迹聚类

#encoding:utf-8
# 导入包
import os
import matplotlib
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.cluster import DBSCAN
from sklearn.preprocessing import StandardScaler
import numpy as np

# 获取需要遍历的文件夹中的所有数据名
filename_list = []
for filename in os.listdir('data'):
    filename_list.append(filename)
# 获取所有的数据并合并成一个dataframe
dataframe_all = pd.DataFrame()
for filename in filename_list:
    dataframe = pd.read_excel('data/'+filename,usecols = [1,2,3,6,7,8])
    dataframe_all = dataframe_all.append(dataframe)

# 根据原始绘图结果可以看到,存在两个异常点,大致分布为LAT小于28.84,在前面数据处理的时候删除这两个异常点,重新绘图
# 如果换数据的话,该异常点不一定存在,直接对其进行处理即可
dataframe_all = dataframe_all[~(dataframe_all['LAT'] <= 28.84)]
# 通过观察,如果船speed<10,说明船没有行进,对于研究目标来说属于无效数据,予以删除。
dataframe_all = dataframe_all[~(dataframe_all['SAILSPEED'] <= 10)]

# 提取所有的经纬度
position 
  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是基于轨迹聚类DBSCAN算法的Python代码: ``` import numpy as np from sklearn.metrics.pairwise import haversine_distances def dbscan_trajectory_clustering(X, epsilon, min_samples, metric='haversine'): """ Perform DBSCAN clustering on a set of trajectory segments. Parameters ---------- X : array-like, shape (n_samples, n_features) The input data representing the trajectory segments. Each row corresponds to a single trajectory segment and should contain at least two columns representing latitude and longitude. epsilon : float The maximum distance between two trajectory segments for them to be considered as belonging to the same cluster. min_samples : int The minimum number of trajectory segments required for a cluster to be considered valid. metric : string, optional (default='haversine') The distance metric to use. Should be one of ['haversine', 'euclidean']. Returns ------- labels : array-like, shape (n_samples,) A label array where each element indicates the cluster number of the corresponding trajectory segment. -1 indicates an outlier. """ # Compute pairwise distances between trajectory segments if metric == 'haversine': X_rad = np.radians(X[:, :2]) dist_matrix = haversine_distances(X_rad, X_rad) * 6371 * 1000 # Earth radius in meters elif metric == 'euclidean': dist_matrix = np.sqrt(np.sum((X[:, :2] - X[:, :2][:, np.newaxis]) ** 2, axis=2)) else: raise ValueError(f"Unsupported metric: {metric}") # Perform DBSCAN clustering labels = np.zeros(X.shape[0], dtype=int) visited = np.zeros(X.shape[0], dtype=bool) current_cluster = -1 for i in range(X.shape[0]): if visited[i]: continue visited[i] = True neighbor_indices = np.where(dist_matrix[i] < epsilon)[0] if len(neighbor_indices) < min_samples: labels[i] = -1 # Mark as outlier else: current_cluster += 1 labels[i] = current_cluster j = 0 while j < len(neighbor_indices): neighbor_index = neighbor_indices[j] if not visited[neighbor_index]: visited[neighbor_index] = True new_neighbor_indices = np.where(dist_matrix[neighbor_index] < epsilon)[0] if len(new_neighbor_indices) >= min_samples: neighbor_indices = np.union1d(neighbor_indices, new_neighbor_indices) if labels[neighbor_index] == 0: labels[neighbor_index] = current_cluster j += 1 return labels ``` 此代码实现了基于轨迹聚类DBSCAN算法,其中输入数据为表示轨迹段的(lat, lon)对,输出一个标签数组表示每个轨迹段所属的簇。该算法可用于抽取轨迹中的行程信息,例如起点、终点、路线等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值