C#,基于密度的噪声应用空间聚类算法(DBSCAN Algorithm)源代码

news/2024/5/20 6:23:11 标签: 聚类, 算法, 机器学习

1 聚类算法

聚类分析或简单聚类基本上是一种无监督的学习方法,它将数据点划分为若干特定的批次或组,使得相同组中的数据点具有相似的属性,而不同组中的数据点在某种意义上具有不同的属性。它包括许多基于差分进化的不同方法。

E、 g.K-均值(点之间的距离)、亲和传播(图距离)、均值偏移(点之间的距离)、DBSCAN(最近点之间的距离)、高斯混合(到中心的马氏距离)、谱聚类(图距离)等。

基本上,所有聚类方法都使用相同的方法,即首先计算相似度,然后使用相似度将数据点聚类为组或批。在这里,我们将重点讨论基于密度的应用程序空间聚类和噪声(DBSCAN)聚类方法。

簇是数据空间中的密集区域,由点密度较低的区域隔开。DBSCAN算法基于“簇”和“噪声”这一直观概念。关键思想是,对于簇的每个点,给定半径的邻域必须至少包含最少数量的点。

2 DBSCAN

DBSCAN全称为Density-based Spatial Clustering of Applications with Noise。翻译中文即:基于密度的噪声应用空间聚类(DBSCAN)

基于密度的噪声应用空间聚类是一种基于密度的聚类技术。在基于密度的聚类中,数据按数据点高度集中的区域分组,数据点高度集中的区域被数据点高度集中的区域包围。基本上,该算法会找到数据点密集的地方,并调用这些集群。

DBSCAN是基于密度的聚类的基本算法。它可以从包含噪声和异常值的大量数据中发现不同形状和大小的聚类。DBSCAN集群最吸引人的特性是它对异常值的鲁棒性。该算法只需要两个参数,即minPoints和epsilon。对于一个区域,聚集在一起的最小点数(阈值)是minpoint,其中作为epsilon的是用于在任何点的邻域中定位点的距离度量。DBSCAN围绕每个数据点创建一个epsilon半径的圆,并将其分类为核心点、边界点和噪声。

 3 源程序

using System;
using System.Data;
using System.Collections.Generic;
using System.Drawing;

namespace Legalsoft.Truffer.Algorithm
{
    public class K_Means_DBSCAN_Algorithm
    {
        public List<Point> Cluster1 = new List<Point>();
        public List<Point> Cluster2 = new List<Point>();

        public List<Point> afterCluster1 = new List<Point>();
        public List<Point> afterCluster2 = new List<Point>();

        public void Caverage()
        {
            afterCluster1.Clear();
            afterCluster2.Clear();

            List<Point> Cluster = new List<Point>();
            foreach (Point m1Point in Cluster1)
            {
                Cluster.Add(m1Point);
            }
            foreach (Point m2Point in Cluster2)
            {
                Cluster.Add(m2Point);
            }
            Point C1 = Cluster[0];
            Point C2 = Cluster[1];

            bool flag = true;
            while (flag)
            {
                int N1 = 0;
                int N2 = 0;
                int C1x = 0;
                int C2x = 0;
                int C1y = 0;
                int C2y = 0;
                foreach (Point point in Cluster)
                {
                    int s1 = Math.Abs(point.X - C1.X) + Math.Abs(point.Y - C1.Y);
                    int s2 = Math.Abs(point.X - C2.X) + Math.Abs(point.Y - C2.Y);
                    if (s1 < s2)
                    {
                        N1++;
                        C1x += point.X;
                        C1y += point.Y;
                    }
                    else
                    {
                        N2++;
                        C2x += point.X;
                        C2y += point.Y;
                    }
                }
                if (C1x / N1 == C1.X && C2.X == C2x / N2 && C1.Y == C1y / N1 && C2.Y == C2y / N2)
                {
                    flag = false;
                }
                C1.X = C1x / N1;
                C2.X = C2x / N2;
                C1.Y = C1y / N1;
                C2.Y = C2y / N2;
            }

            foreach (Point point in Cluster)
            {
                int s1 = Math.Abs(point.X - C1.X) + Math.Abs(point.Y - C1.Y);
                int s2 = Math.Abs(point.X - C2.X) + Math.Abs(point.Y - C2.Y);
                if (s1 < s2)
                {
                    afterCluster1.Add(point);
                }
                else
                {
                    afterCluster2.Add(point);
                }
            }
        }

        /// <summary>
        /// K-Means-DBSCAN算法
        /// </summary>
        public void DBSCAN(int radius = 1, int MinPts = 12)
        {
            double pow = 2.0;
            afterCluster1.Clear();
            afterCluster2.Clear();
            List<Point> Cluster = new List<Point>();
            List<Point> temp1 = new List<Point>();
            List<Point> temp2 = new List<Point>();

            foreach (Point m1Point in Cluster1)
            {
                Cluster.Add(m1Point);
            }
            foreach (Point m2Point in Cluster2)
            {
                Cluster.Add(m2Point);
            }
            Point C1;
            Point C2;

            bool isC1Get = false;
            bool isC2Get = false;
            foreach (Point mm in Cluster)
            {
                if (isC1Get == false)
                {
                    int count = 0;
                    temp1.Clear();
                    foreach (Point mm1 in Cluster)
                    {
                        double banjing = Math.Sqrt(Math.Pow(mm1.X - mm.X, pow) + Math.Pow(mm1.Y - mm.Y, pow));
                        if (banjing < radius)
                        {
                            count++;
                            temp1.Add(mm1);
                        }
                    }
                    if (count >= MinPts)
                    {
                        C1 = mm;
                        isC1Get = true;
                        foreach (Point mm2 in temp1)
                        {
                            foreach (Point mm3 in Cluster)
                            {
                                double banjing = Math.Sqrt(Math.Pow(mm3.X - mm2.X, pow) + Math.Pow(mm3.Y - mm2.Y, pow));
                                if (banjing < radius && (afterCluster1.Contains(mm3) == false))
                                {
                                    afterCluster1.Add(mm3);
                                }
                            }
                        }
                    }
                }
                else if (isC2Get == false)
                {
                    if (afterCluster1.Contains(mm) == false)
                    {
                        int count = 0;
                        temp2.Clear();
                        foreach (Point mm1 in Cluster)
                        {
                            double banjing = Math.Sqrt(Math.Pow(mm1.X - mm.X, pow) + Math.Pow(mm1.Y - mm.Y, pow));
                            if (banjing < radius)
                            {
                                count++;
                                temp2.Add(mm1);
                            }
                        }
                        if (count >= MinPts)
                        {
                            C2 = mm;
                            isC2Get = true;
                            foreach (Point mm2 in temp2)
                            {
                                foreach (Point mm3 in Cluster)
                                {
                                    double banjing = Math.Sqrt(Math.Pow(mm3.X - mm2.X, pow) + Math.Pow(mm3.Y - mm2.Y, pow));
                                    if (banjing < radius && (afterCluster1.Contains(mm3) == false) && afterCluster2.Contains(mm3) == false)
                                    {
                                        afterCluster2.Add(mm3);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        continue;
                    }
                }
                else
                {
                    break;
                }
            }
        }
    }
}

4 源代码

using System;
using System.Data;
using System.Collections.Generic;
using System.Drawing;

namespace Legalsoft.Truffer.Algorithm
{
    public class K_Means_DBSCAN_Algorithm
    {
        public List<Point> Cluster1 = new List<Point>();
        public List<Point> Cluster2 = new List<Point>();

        public List<Point> afterCluster1 = new List<Point>();
        public List<Point> afterCluster2 = new List<Point>();

        public void Caverage()
        {
            afterCluster1.Clear();
            afterCluster2.Clear();

            List<Point> Cluster = new List<Point>();
            foreach (Point m1Point in Cluster1)
            {
                Cluster.Add(m1Point);
            }
            foreach (Point m2Point in Cluster2)
            {
                Cluster.Add(m2Point);
            }
            Point C1 = Cluster[0];
            Point C2 = Cluster[1];

            bool flag = true;
            while (flag)
            {
                int N1 = 0;
                int N2 = 0;
                int C1x = 0;
                int C2x = 0;
                int C1y = 0;
                int C2y = 0;
                foreach (Point point in Cluster)
                {
                    int s1 = Math.Abs(point.X - C1.X) + Math.Abs(point.Y - C1.Y);
                    int s2 = Math.Abs(point.X - C2.X) + Math.Abs(point.Y - C2.Y);
                    if (s1 < s2)
                    {
                        N1++;
                        C1x += point.X;
                        C1y += point.Y;
                    }
                    else
                    {
                        N2++;
                        C2x += point.X;
                        C2y += point.Y;
                    }
                }
                if (C1x / N1 == C1.X && C2.X == C2x / N2 && C1.Y == C1y / N1 && C2.Y == C2y / N2)
                {
                    flag = false;
                }
                C1.X = C1x / N1;
                C2.X = C2x / N2;
                C1.Y = C1y / N1;
                C2.Y = C2y / N2;
            }

            foreach (Point point in Cluster)
            {
                int s1 = Math.Abs(point.X - C1.X) + Math.Abs(point.Y - C1.Y);
                int s2 = Math.Abs(point.X - C2.X) + Math.Abs(point.Y - C2.Y);
                if (s1 < s2)
                {
                    afterCluster1.Add(point);
                }
                else
                {
                    afterCluster2.Add(point);
                }
            }
        }

        /// <summary>
        /// K-Means-DBSCAN算法
        /// </summary>
        public void DBSCAN(int radius = 1, int MinPts = 12)
        {
            double pow = 2.0;
            afterCluster1.Clear();
            afterCluster2.Clear();
            List<Point> Cluster = new List<Point>();
            List<Point> temp1 = new List<Point>();
            List<Point> temp2 = new List<Point>();

            foreach (Point m1Point in Cluster1)
            {
                Cluster.Add(m1Point);
            }
            foreach (Point m2Point in Cluster2)
            {
                Cluster.Add(m2Point);
            }
            Point C1;
            Point C2;

            bool isC1Get = false;
            bool isC2Get = false;
            foreach (Point mm in Cluster)
            {
                if (isC1Get == false)
                {
                    int count = 0;
                    temp1.Clear();
                    foreach (Point mm1 in Cluster)
                    {
                        double banjing = Math.Sqrt(Math.Pow(mm1.X - mm.X, pow) + Math.Pow(mm1.Y - mm.Y, pow));
                        if (banjing < radius)
                        {
                            count++;
                            temp1.Add(mm1);
                        }
                    }
                    if (count >= MinPts)
                    {
                        C1 = mm;
                        isC1Get = true;
                        foreach (Point mm2 in temp1)
                        {
                            foreach (Point mm3 in Cluster)
                            {
                                double banjing = Math.Sqrt(Math.Pow(mm3.X - mm2.X, pow) + Math.Pow(mm3.Y - mm2.Y, pow));
                                if (banjing < radius && (afterCluster1.Contains(mm3) == false))
                                {
                                    afterCluster1.Add(mm3);
                                }
                            }
                        }
                    }
                }
                else if (isC2Get == false)
                {
                    if (afterCluster1.Contains(mm) == false)
                    {
                        int count = 0;
                        temp2.Clear();
                        foreach (Point mm1 in Cluster)
                        {
                            double banjing = Math.Sqrt(Math.Pow(mm1.X - mm.X, pow) + Math.Pow(mm1.Y - mm.Y, pow));
                            if (banjing < radius)
                            {
                                count++;
                                temp2.Add(mm1);
                            }
                        }
                        if (count >= MinPts)
                        {
                            C2 = mm;
                            isC2Get = true;
                            foreach (Point mm2 in temp2)
                            {
                                foreach (Point mm3 in Cluster)
                                {
                                    double banjing = Math.Sqrt(Math.Pow(mm3.X - mm2.X, pow) + Math.Pow(mm3.Y - mm2.Y, pow));
                                    if (banjing < radius && (afterCluster1.Contains(mm3) == false) && afterCluster2.Contains(mm3) == false)
                                    {
                                        afterCluster2.Add(mm3);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        continue;
                    }
                }
                else
                {
                    break;
                }
            }
        }
    }
}


http://www.niftyadmin.cn/n/5405256.html

相关文章

每天一道leetcode:20.有效的括号(简单;栈的经典题目)

⭐今日份题目 给定一个只包括 (&#xff0c;)&#xff0c;{&#xff0c;}&#xff0c;[&#xff0c;] 的字符串 s &#xff0c;判断字符串是否有效。 有效字符串需满足&#xff1a; 左括号必须用相同类型的右括号闭合。 左括号必须以正确的顺序闭合。 每个右括号都有一个对…

Arduino应用开发——使用GUI-Guider制作LVGL UI并导入ESP32运行

Arduino应用开发——使用GUI-Guider制作LVGL UI并导入ESP32运行 目录 Arduino应用开发——使用GUI-Guider制作LVGL UI并导入ESP32运行前言1 使用GUI-Guider设计UI1.1 创建工程1.2 设计UI 2 ESP工程导入UI2.1 移植LVGL2.2 移植UI文件2.3 调用UI文件2.4 烧录测试 结束语 前言 GU…

07OpenCV 图像模糊

文章目录 图像掩膜操作模糊原理均值滤波高斯滤波中值滤波双边滤波算子代码 图像掩膜操作 图像掩膜操作 模糊原理 Smooth/Blur是图像处理中最简单和常用的操作之一 使用操作的原因之一就是为了给图像预处理时候减低噪声 图像噪声是指存在于图像数据中的不必要的或多余的干扰信…

贪心 Leetcode 55 跳跃游戏

跳跃游戏 Leetcode 55 学习记录自代码随想录 给你一个非负整数数组 nums &#xff0c;你最初位于数组的 第一个下标 。数组中的每个元素代表你在该位置可以跳跃的最大长度。 判断你是否能够到达最后一个下标&#xff0c;如果可以&#xff0c;返回 true &#xff1b;否则&…

Ansible简介及使用

Ansible简介及使用 一、Ansible简介 1. 什么是Ansible Ansible是新出现的自动化运维工具&#xff0c;基于Python开发&#xff0c;集合了众多运维工具&#xff08;puppet、cfengine、chef、func、fabric&#xff09;的优点&#xff0c;实现了批量系统配置、批量程序部署、批量…

【LeetCode打卡】Day24|回溯算法理论基础、77. 组合

学习目标&#xff1a; 理论基础 77. 组合 学习内容&#xff1a; 理论基础 文章讲解 回溯法本质是穷举&#xff0c;纯暴力搜索。回溯法解决的问题都可以抽象为树形结构&#xff0c;集合的大小就构成了树的宽度&#xff0c;递归的深度&#xff0c;都构成的树的深度。 组合问题&…

docker commit构建镜像时环境变量不生效问题解决

描述 当使用 docker commit 命令提交容器镜像时&#xff0c;环境变量的配置并不会自动生效。 原因 这是因为 docker commit 命令只是将容器的当前状态保存为一个新的镜像&#xff0c;并不会修改容器的运行时配置。 甚至在容器修改profile配置文件后进行docker commit都不会…

岭回归算法

回归分析方法是利用数理统计方法分析数据&#xff0c;建立自变量和因变量间的回归模型&#xff0c;用于预测因变量变化的分析方法。其中比较经典的是HoerI和Kennard提出的岭回归算法。岭回归算法是在最小二乘法的基础上引|入正则项&#xff0c;使回归模型具有较好泛化能力和稳定…