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