c# c均值聚类及DBSCAN聚类

本文介绍了如何使用C#进行数据聚类,特别是在WinForm环境中。作者通过创建一个简单的用户界面,展示如何生成0-100范围内的二维样本,并运用C均值(K-means)和DBSCAN两种聚类算法进行处理。实验结果以可视化的方式在界面上呈现。提供了CSDN和GitHub上的代码下载链接以供参考。
摘要由CSDN通过智能技术生成

winform的界面搭建比较简单,首先在界面上的左边产生两堆特征值为 0-100的二维样本。通过 c均值聚类及DBSCAN聚类将聚类结果显示在右边。样本点有x和y值,用List<Point>存储一个类。

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Text;


namespace PatternRecon
{
    public partial class Form1 : Form
    {
   
        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 Form1()
        {
            InitializeComponent();
            InitCombo();
        }
        public void InitCombo()
        {
            this.comboBox1.Items.Add("c均值");
            this.comboBox1.Items.Add("DBSCAN");
            //设置默认值
            this.textBox1.Text = "1";
            this.textBox2.Text = "12";
            //设置背景
            afterCluster1.Clear();
            afterCluster2.Clear();
            this.pictureBox1.Image = (DrawCluster(afterCluster1, afterCluster2, false));
            this.pictureBox2.Image = (DrawCluster(afterCluster1, afterCluster2, false));
        }

        //产生随机数种子
        public static int GetRandomSeed()
        {
            byte[] bytes = new byte[4];
            System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
            rng.GetBytes(bytes);
            return BitConverter.ToInt32(bytes, 0);
        }

        //产生标准正太分布
        public static double[] NormalDistribution()
        {
            Random rand = new Random(GetRandomSeed());
            double[] y;
            double u1, u2, v1=0, v2=0, s = 0, z1=0, z2=0;
            while (s > 1 || s == 0)
            {
                u1 = rand.NextDouble();
                u2 = rand.NextDouble();
                v1 = 2 * u1 - 1;
                v2 = 2 * u2 - 1;
                s = v1 * v1 + v2 * v2;
            }
            z1 = Math.Sqrt(-2 * Math.Log(s) / s) * v1;
            z2 = Math.Sqrt(-2 * Math.Log(s) / s) * v2;
            y = new double[] { z1, z2 };
            return y; //返回两个服从正态分布N(0,1)的随机数z0 和 z1
        }
        //确定
        private void button1_Click(object sender, EventArgs e)
        {
            if (this.comboBox1.Text == "c均值")
            {
                //调用c均值
                Caverage();
                this.pictureBox2.Image = (DrawCluster(afterCluster1, afterCluster2,false));
            }
            else if (this.comboBox1.Text == "DBSCAN")
            {
                //调用DBSCAN
                if (this.textBox1.Text == "" || this.textBox2.Text == "")
                {
                    MessageBox.Show("请输入DBSCAN值");
                }
                else
                {
                    DBSCAN();
                    this.pictureBox2.Image = (DrawCluster(afterCluster1, afterCluster2, false));
                }
            }
            else
            {
                MessageBox
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值