# -*- coding: UTF-8 -*-
"""
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@File : dbscan.py
@Contact : ffzzyy@126.com
@License : (C)Copyright 2017-2019
@Author : ffzzyy
@Version : 0.1
@Modify Time : 2019/3/28 22:10
@Desciption
dbscan 算法实现
"""
import pandas as pd
import matplotlib.pyplot as plt
import sklearn.cluster as skc
import numpy as np
import math
import seaborn as sns
def dist(a, b):
"""
输入:向量A, 向量B
输出:两个向量的欧式距离
"""
return math.sqrt(np.power(a - b, 2).sum())
class dbscan():
"""dbscan 类
Parameters
----------
eps,min_samples
Attributes
----------
core_object_index:list,核心对象序列,对应训练集索引
_train_set:ndarray,训练集
labels:ndarray,对应训练集索引的,聚类 id
_k:int,聚类簇id,从 0 开始
"""
def __init__(self, eps, min_samples):
self.eps = eps
self.min_samples = min_samples
self.core_object_index = []
self._train_set = None
self.labels = []
self._k = -1
self.components = []
def fit(self, train_set):
self._train_set = train_set
self.init_core_objects()
self.labels = np.array([-1] * len(train_set))
self._k = -1
unvisited_object_index = range(len(train_set))
unvisited_core_index = self.core_object_index.copy() # 深度复制
while (len(unvisited_core_index) != 0):
"""
从 未被访问的核心对象中 随机选择一个 queue 序列中,queue
来保存 通过第一个核心对象,一个接一个找出其密度可达的 核心对象
Python实现机器算法-01-DBSCAN
最新推荐文章于 2024-07-18 15:30:34 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)