⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 数据挖掘中的一个聚类算法k-means。
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Timers;
using System.Text;
using System.IO;
using System.Data.SqlClient;
using System.Data.SqlTypes;

/*namespace Farproc.Text
{/// 用于取得一个文本文件的编码方式(Encoding)。
	public class TxtFileEncoding
	{ 
		
		public static Encoding GetEncoding(string fileName)
		{
			return GetEncoding(fileName, Encoding.Default);
		}
		public static Encoding GetEncoding(FileStream stream)
		{
			return GetEncoding(stream, Encoding.Default);
		}
		public static Encoding GetEncoding(string fileName, Encoding defaultEncoding)
		{
			FileStream fs = new FileStream(fileName, FileMode.Open);
			Encoding targetEncoding = GetEncoding(fs, defaultEncoding);
			fs.Close();
			return targetEncoding;
		}
		public static Encoding GetEncoding(FileStream stream, Encoding defaultEncoding)
		{
			Encoding targetEncoding = defaultEncoding;
			if(stream != null && stream.Length >= 2)
			{
				//保存文件流的前4个字节
				byte byte1 = 0;
				byte byte2 = 0;
				byte byte3 = 0;
				byte byte4 = 0;
				//保存当前Seek位置
				long origPos = stream.Seek(0, SeekOrigin.Begin);
				stream.Seek(0, SeekOrigin.Begin);
				int nByte = stream.ReadByte();
				byte1 = Convert.ToByte(nByte);
				byte2 = Convert.ToByte(stream.ReadByte());
				if(stream.Length >= 3)
				{
					byte3 = Convert.ToByte(stream.ReadByte());
				}
				if(stream.Length >= 4)
				{
					byte4 = Convert.ToByte(stream.ReadByte());
				}
				//根据文件流的前4个字节判断Encoding
				//Unicode {0xFF, 0xFE};
				//BE-Unicode {0xFE, 0xFF};
				//UTF8 = {0xEF, 0xBB, 0xBF};
				if(byte1 == 0xFE && byte2 == 0xFF)//UnicodeBe
				{
					targetEncoding = Encoding.BigEndianUnicode;
				}
				if(byte1 == 0xFF && byte2 == 0xFE && byte3 != 0xFF)//Unicode
				{
					targetEncoding = Encoding.Unicode;
				}
				if(byte1 == 0xEF && byte2 == 0xBB && byte3 == 0xBF)//UTF8
				{
					targetEncoding = Encoding.UTF8;
				}
				//恢复Seek位置       
				stream.Seek(origPos, SeekOrigin.Begin);
			}
			return targetEncoding;
		}
	}
}

namespace name1
{
	using Farproc.Text;
	class Class1
	{
	
		static int N=625;
		static void kmeans(float a1,float a2,float a3,float a4,float b1,float b2,float b3,float b4,float c1,float c2,float c3,float c4)
		{
			string fileName;
			Encoding fileEncoding;
			string str;
			int i;
			int j;
			int n;
			int xun;
			int xun1;
			int zongshu;
			float zuizhi;
			float E;
			float zuiE;
			float[,] shuzhi=new float[N,4];
			float[,] paper=new float[3,4];
			float[] a=new float[4];
			float[,] juli=new float[3,N];
			int[] flag=new int[N];
			float[,] shu=new float[3,4];
			int[] cishu=new int[3];
			try
			{
					paper[0,0]=a1;
				paper[0,1]=a2;
				paper[0,2]=a3;
				paper[0,3]=a4;

				paper[1,0]=b1;
				paper[1,1]=b2;
				paper[1,2]=b3;
				paper[1,3]=b4;

				paper[2,0]=c1;
				paper[2,1]=c2;
				paper[2,2]=c3;
				paper[2,3]=c4;

				xun=0;
				E=0;
				zuiE=1000000;
				while(xun<100)
				{
					for(i=0;i<3;i++)   //得到文档中每个数与三个中心点的距离
					{
						j=0;
						n=0;
						fileName=@"C:\balance-scale.data";   //文档名称
						fileEncoding = TxtFileEncoding.GetEncoding(fileName, Encoding.GetEncoding("GB2312"));//取得这txt文件的编码
						StreamReader sr = new StreamReader(fileName, fileEncoding);//用该编码创建StreamReader
						while((str=sr.ReadLine())!=null)
						{   
							a[0]=str[0]-48;
							a[1]=str[2]-48;
							a[2]=str[4]-48;
							a[3]=str[6]-48;
							for(int m=0;m<4;m++)
								shuzhi[n,m]=a[m];
							n++;
							juli[i,j]=(float)Math.Sqrt((paper[i,0]-a[0])*(paper[i,0]-a[0])+(paper[i,1]-a[1])*(paper[i,1]-a[1])+(paper[i,2]-a[2])*(paper[i,2]-a[2])+(paper[i,3]-a[3])*(paper[i,3]-a[3]));
							//	Console.WriteLine("{0}",juli[i,j]);
							j++;
						}
						//Console.WriteLine("#####################");
						sr.Close();
					}
					//###################################
					//下面比较每个数距离三个中心点哪个最近,将该中心点代表的类别分配给该数
					for(j=0;j<N;j++)
					{
						zuizhi=100000;
						for(i=0;i<3;i++)
							if(juli[i,j]<zuizhi)
							{
								zuizhi=juli[i,j];
								flag[j]=i+1;
							}

						//	Console.WriteLine("{0}",flag[j]);
					}
					//###################################

					//计算标准测度函数
					E=0;
					for(j=0;j<N;j++)
					{
						if(flag[j]==1)
							E=E+(float)(shuzhi[j,0]-paper[0,0])*(shuzhi[j,0]-paper[0,0])+(float)(shuzhi[j,1]-paper[0,1])*(shuzhi[j,1]-paper[0,1])+(float)(shuzhi[j,2]-paper[0,2])*(shuzhi[j,2]-paper[0,2])+(float)(shuzhi[j,3]-paper[0,3])*(shuzhi[j,3]-paper[0,3]);
						if(flag[j]==2)
							E=E+(float)(shuzhi[j,0]-paper[1,0])*(shuzhi[j,0]-paper[1,0])+(float)(shuzhi[j,1]-paper[1,1])*(shuzhi[j,1]-paper[1,1])+(float)(shuzhi[j,2]-paper[1,2])*(shuzhi[j,2]-paper[1,2])+(float)(shuzhi[j,3]-paper[1,3])*(shuzhi[j,3]-paper[1,3]);
						if(flag[j]==3)
							E=E+(float)(shuzhi[j,0]-paper[2,0])*(shuzhi[j,0]-paper[2,0])+(float)(shuzhi[j,1]-paper[2,1])*(shuzhi[j,1]-paper[2,1])+(float)(shuzhi[j,2]-paper[2,2])*(shuzhi[j,2]-paper[2,2])+(float)(shuzhi[j,3]-paper[2,3])*(shuzhi[j,3]-paper[2,3]);
					}
					//Console.WriteLine("{0}",E);
					if(zuiE>E)
					{
						zuiE=E;
						xun=0;
					}
					else
						xun++;
					if(xun==100)
					{
						zongshu=0;	
						j=0;
						fileName=@"F:\1.data";   //文档名称
						fileEncoding = TxtFileEncoding.GetEncoding(fileName, Encoding.GetEncoding("GB2312"));//取得这txt文件的编码
						StreamReader sr = new StreamReader(fileName, fileEncoding);//用该编码创建StreamReader
						while((str=sr.ReadLine())!=null)
						{
							Console.Write("{0}---",str[8]);
							Console.WriteLine("{0}",flag[j]);
							xun1=str[8]-48;
							if(xun1==flag[j])
								zongshu++;
							j++;
						}
						sr.Close();
						Console.WriteLine("zongshu:{0}",zongshu);
						Console.WriteLine("baifenbi:{0}",(float)zongshu/N);
					}
					//###################################
			 
					for(j=0;j<N;j++)
					{
						if(flag[j]==1)
						{
							shu[0,0]+=shuzhi[j,0];
							shu[0,1]+=shuzhi[j,1];
							shu[0,2]+=shuzhi[j,2];
							shu[0,3]+=shuzhi[j,3];
							cishu[0]++;
						}
						if(flag[j]==2)
						{
							shu[1,0]+=shuzhi[j,0];
							shu[1,1]+=shuzhi[j,1];
							shu[1,2]+=shuzhi[j,2];
							shu[1,3]+=shuzhi[j,3];
							cishu[1]++;
						}
						if(flag[j]==3)
						{
							shu[2,0]+=shuzhi[j,0];
							shu[2,1]+=shuzhi[j,1];
							shu[2,2]+=shuzhi[j,2];
							shu[2,3]+=shuzhi[j,3];
							cishu[2]++;
						}
					}
					paper[0,0]=(float)(shu[0,0]/cishu[0]);
					paper[0,1]=(float)(shu[0,1]/cishu[0]);
					paper[0,2]=(float)(shu[0,2]/cishu[0]);
					paper[0,3]=(float)(shu[0,3]/cishu[0]);

					paper[1,0]=(float)(shu[1,0]/cishu[1]);
					paper[1,1]=(float)(shu[1,1]/cishu[1]);
					paper[1,2]=(float)(shu[1,2]/cishu[1]);
					paper[1,3]=(float)(shu[1,3]/cishu[1]);

					paper[2,0]=(float)(shu[2,0]/cishu[2]);
					paper[2,1]=(float)(shu[2,1]/cishu[2]);
					paper[2,2]=(float)(shu[2,2]/cishu[2]);
					paper[2,3]=(float)(shu[2,3]/cishu[2]);
					  
				}
			}
			
			catch(Exception e)
			{
				Console.WriteLine("An exception was thrown.Message was"+e.Message);
			}
				
		}
		
	}
}*/
namespace k_means
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	
	public class Form1 : System.Windows.Forms.Form
	{
		/// <summary>
		/// 必需的设计器变量。
		
		/// //
		static int N=625;
		public struct Paper
		{
			public float a1;
			public float a2;
			public float a3;
			public float a4;
			public int lable;//标记
			public int cla;//
		}
		 int tm=0;
		
		public Paper[] paper=new Paper[6250];
		public struct Center
		{
			public float a1;
			public float a2;
			public float a3;
			public float a4;
		}
		public Center[] center=new Center[3];

		private System.Windows.Forms.OpenFileDialog openFileDialog1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.TextBox textBox4;
		private System.Windows.Forms.Timer timer1;
		private System.Windows.Forms.TextBox textBox5;
		private System.ComponentModel.IContainer components;



		public Form1()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();
			
			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
			this.button1 = new System.Windows.Forms.Button();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.textBox4 = new System.Windows.Forms.TextBox();
			this.timer1 = new System.Windows.Forms.Timer(this.components);
			this.textBox5 = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// openFileDialog1
			// 
			this.openFileDialog1.FileOk += new System.ComponentModel.CancelEventHandler(this.openFileDialog1_FileOk);
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(24, 56);
			this.button1.Name = "button1";
			this.button1.TabIndex = 0;
			this.button1.Text = "button1";
			this.button1.Click += new System.EventHandler(this.button1_Click_1);
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(144, 32);
			this.textBox1.Multiline = true;
			this.textBox1.Name = "textBox1";
			this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
			this.textBox1.Size = new System.Drawing.Size(200, 88);
			this.textBox1.TabIndex = 1;
			this.textBox1.Text = "";
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(48, 200);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(464, 21);
			this.textBox2.TabIndex = 2;
			this.textBox2.Text = "";
			this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
			// 
			// textBox3
			// 
			this.textBox3.Location = new System.Drawing.Point(48, 272);
			this.textBox3.Name = "textBox3";
			this.textBox3.Size = new System.Drawing.Size(464, 21);
			this.textBox3.TabIndex = 3;
			this.textBox3.Text = "";
			// 
			// textBox4
			// 
			this.textBox4.Location = new System.Drawing.Point(48, 240);
			this.textBox4.Name = "textBox4";
			this.textBox4.Size = new System.Drawing.Size(464, 21);
			this.textBox4.TabIndex = 4;
			this.textBox4.Text = "";
			this.textBox4.TextChanged += new System.EventHandler(this.textBox4_TextChanged);
			// 
			// timer1
			// 
			this.timer1.Enabled = true;
			this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
			// 
			// textBox5
			// 
			this.textBox5.Location = new System.Drawing.Point(400, 88);
			this.textBox5.Name = "textBox5";
			this.textBox5.Size = new System.Drawing.Size(120, 21);
			this.textBox5.TabIndex = 5;
			this.textBox5.Text = "textBox5";
			this.textBox5.TextChanged += new System.EventHandler(this.textBox5_TextChanged);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(592, 349);
			this.Controls.Add(this.textBox5);
			this.Controls.Add(this.textBox4);
			this.Controls.Add(this.textBox3);
			this.Controls.Add(this.textBox2);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.button1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
			//myTimer.Tick += new EventHandler(TimerEventProcessor);


		}

		private void button1_Click(object sender, System.EventArgs e)
		{
		
		}

		private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
		{
		
		}

		private void button1_Click_1(object sender, System.EventArgs e)
		{
			
			string str;
			int i,j,k;
		    i=0;
			float dis,temp,temp1;
			//读入文件,存入数组
			
				//timer1.Enabled=true;
				System.IO.StreamReader sr = new 
					System.IO.StreamReader(@"C:\balance-scale.data");
				//timer1.Enabled=true;
				
                timer1.Enabled =true;
				
				while((str=sr.ReadLine())!=null)
				{					
					paper[i].a1=(float)(Double.Parse(str.Substring(0,1)));//str.Substring(0,1);
					paper[i].a2=(float)(Double.Parse(str.Substring(2,1)));
					paper[i].a3=(float)(Double.Parse(str.Substring(4,1)));
					paper[i].a4=(float)(Double.Parse(str.Substring(6,1)));
					paper[i].lable=0;
					paper[i].cla=0;
					textBox1.Text=textBox1.Text+paper[i].a1.ToString()+paper[i].a2.ToString()+paper[i].a3.ToString()+paper[i].a4.ToString()+"\r\n";
				  
					i++;
				}
				sr.Close();
					
			
			//计算距离
			  float[,] disarray=new float[i,i];
            for(j=0;j<i;j++)
				for(k=0;k<i;k++)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -