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

📄 form2.cs

📁 基于贝叶斯的数据挖掘算法的实现,使用visual stutio2005编程实现。
💻 CS
📖 第 1 页 / 共 2 页
字号:
		/// <returns></returns>
		private DataSet GetDataSet(string path,System.Data.DataTable db)
		{			
			string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ;"+
                            "Extended Properties=Excel 8.0;"+
                            "Data Source = path";
			OleDbConnection myConn = new OleDbConnection(strCon );

			feature = db.Rows.Count  - 1;//获取特征数目
//			addControl(feature);
			//			string strCom = " SELECT v5,count(v5) as total,stdev(v1) as dev1,avg(v1) as avg1,stdev(v2) as dev2,avg(v2) as avg2,stdev(v3) as dev3," +
			//				"avg(v3) as avg3,stdev(v3) as dev4,avg(v4) as avg4 FROM [sheet1$] group by v5 having count(*)>2";
			/*-----------------------------
			 * 预处理,将列名转化为固定的列名
			 * 
			 ------------------------------*/
			StringBuilder sb = new StringBuilder("select ");

			int count = db.Rows.Count ;                               
			string s = db.Rows[count-1][0].ToString();

			sb.Append(s + " as v" + count.ToString() + ",count(" + s +") as total");

			count--;

			for(int i = count;i>0;i--)
			{
				string ms = ",stdev(" + db.Rows[i-1][0].ToString() + ") as dev" + i + ",avg(" +  db.Rows[i-1][0].ToString() + ") as avg" + i;
				sb.Append(ms); 
			}
			sb.Append(" from [sheet1$] group by " + s + " having count(*) > 1");
			//Console.WriteLine("sb={0}",sb);

			try
			{ 
				myConn.Open ( ) ;
				OleDbDataAdapter da = new OleDbDataAdapter(sb.ToString(),myConn);
				da.Fill(ds);
				kind = ds.Tables[0].Rows.Count;//分类数目

				myConn.Close();
			}
			catch(Exception e)
			{
				Console.WriteLine(e.Message);
			}
			return ds;
		}

		/// <summary>
		/// 获取训练数据的元素据
		/// </summary>
		/// <param name="path"></param>
		/// <returns></returns>
		private System.Data.DataTable GetSchema(string path)
		{
			string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ;Extended Properties=Excel 8.0;Data Source = " + path;
			OleDbConnection myConn = new OleDbConnection(strCon );
			string str = "select top 1 * from [sheet1$]";

			OleDbCommand myCommand = new OleDbCommand(str,myConn);
			OleDbDataReader myReader;
			System.Data.DataTable schemaTable = new System.Data.DataTable();

			try
			{ 
				myConn.Open ( ) ;
				myReader = myCommand.ExecuteReader();

				schemaTable = myReader.GetSchemaTable();
				
				myConn.Close();
			}
			catch(Exception e)
			{
				Console.WriteLine(e.Message);
			}
			return schemaTable;
		}

		/// <summary>
		/// 根据文件路径,获取待分类的数据集
		/// </summary>
		/// <param name="path">文件路径</param>
		/// <returns>数据集合</returns>
		 private System.Data.DataTable GetDataTable(string path)
		{
			string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ;Extended Properties=Excel 9.0;Data Source = " + path;
			OleDbConnection myConn = new OleDbConnection(strCon );
            
			string myCon = "select * from [sheet1$]";

			DataSet source = new DataSet();
			try
			{ 
				myConn.Open ( ) ;
				OleDbDataAdapter da = new OleDbDataAdapter(myCon,myConn);
				da.Fill(source);

				myConn.Close();
			}
			catch(Exception e)
			{
				Console.WriteLine(e.Message);
			}
			return source.Tables[0];
		}

		/// <summary>
		/// 将数据分类
		/// </summary>
		/// <param name="db">有待分类的数据</param>
		private void Classify(System.Data.DataTable db)
		{
			if(db.Columns.Count!=feature)
			{
				MessageBox.Show(this,"输入特征值的数目不对!","错误提示");
				return;
			}
		
			//添加结果列
			DataColumn col = new DataColumn();
			col.ColumnName = "result";
			col.DataType = Type.GetType("System.String");
			db.Columns.Add(col);

			double[] input = new double[feature];
			foreach(DataRow dr in db.Rows)
			{
				for(int i=0;i<feature;i++)
				{
					try
					{
						input[i] = double.Parse(dr[i].ToString());
					}
					catch(Exception ex)
					{
						Console.WriteLine(ex.Message);
					}
				}
				dr["result"] = ReturnType(input);
			}

		}
     
		/// <summary>
		/// 计算概率,分布为高斯分布
		/// </summary>
		/// <param name="x">样本值</param>
		/// <param name="avg">样本均值</param>
		/// <param name="dev">样本方差</param>
		/// <returns>概率</returns>
		private double GetPossibility(double x,double avg,double dev)
		{
			double n = -(x-avg)*(x-avg)/(2*dev*dev);
			double m = 1/(dev*Math.Sqrt(2*Math.PI));
			return Math.Exp(n)*m;
		}
		/// <summary>
		/// 根据输入的特征值,返回其所属的类
		/// </summary>
		/// <param name="input">特征值数组</param>
		/// <returns>类名</returns>
		private string ReturnType(double[] input)
		{
			if(input.Length != feature)
			{
				return "ERROR!";
			}

			int total=0;//计算样本的总数
			foreach (DataRow dr in ds.Tables[0].Rows)
			{
				try
				{
					total += int.Parse(dr["total"].ToString());
				}
				catch(Exception ex)
				{
					MessageBox.Show(this,ex.Message,"ERROR");
				}
			}

			int index = -1;//所属类的索引
			int c = -1;
			double p = 0;//属于某类的概率

			foreach (DataRow dr in ds.Tables[0].Rows)
			{
				c++;
				double sp = double.Parse(dr["total"].ToString())/total;

				//计算P(X|Ci)
				double temp = 1;
				for(int i=1;i<=feature;i++)
				{
					double avg = double.Parse(dr["avg" + i].ToString());
					double dev = double.Parse(dr["dev" + i].ToString());
					temp *= GetPossibility(input[i-1],avg,dev);	
				}

				temp *= sp;//P(X|Ci)P(Ci)

				if(p<temp)
				{
					p = temp;
					index = c;
				}
			}
			
			return ds.Tables[0].Rows[index]["v" + (feature+1)].ToString();     
		}

		private void btnCompute_Click(object sender, System.EventArgs e)
		{
			if(ds.Tables.Count < 1)
			{
			    MessageBox.Show(this," 您还没有训练数据","Error");
				return;
			}

			OpenFileDialog openFileDialog1 = new OpenFileDialog();

			openFileDialog1.InitialDirectory = "c:\\" ;
			openFileDialog1.Filter = "txt files (*.xls)|*.xls" ;
			openFileDialog1.FilterIndex = 1 ;
			openFileDialog1.RestoreDirectory = true ;

			if(openFileDialog1.ShowDialog() == DialogResult.OK)
			{
				string sourcePath = openFileDialog1.FileName;
				System.Data.DataTable db = GetDataTable(sourcePath);
                Classify(db);
				dgData.DataSource = db;
			}
		}

		private void btnSave_Click(object sender, System.EventArgs e)
		{
		   CreateExcel((System.Data.DataTable)dgData.DataSource);
		}

		private void CreateExcel(System.Data.DataTable dt)
		{
			if(dt==null||dt.Rows.Count < 1)
			{
				MessageBox.Show(this," 没有可保存的数据","Error");
				return;
			}
			Microsoft.Office.Interop.Excel.Application excel =new Microsoft.Office.Interop.Excel.ApplicationClass();
			if(excel==null)   
			{   
				MessageBox.Show(this,"请按装Excel!","ERROR");
			}
			Microsoft.Office.Interop.Excel.Workbooks   workbooks=excel.Workbooks; 
			Microsoft.Office.Interop.Excel.Workbook   workbook=workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);   
			Microsoft.Office.Interop.Excel.Worksheet   worksheet=(Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];   
			//Excel.Range   myRange=null; 
			int row=1;
			int col=0;

			foreach(DataColumn cl in dt.Columns)
			{
				col++;
				worksheet.Cells[1,col]=cl.ColumnName;
			}

			foreach(DataRow rw in dt.Rows)
			{
				row++;
				col=0;
				foreach(DataColumn cl in dt.Columns)
				{
					col++;
					worksheet.Cells[row,col]=rw[cl.ColumnName].ToString();
				}
			}
			 
			excel.Visible=true;
			//excel.Save("result.xls");
			try
			{
				excel.Application.Workbooks.Close();
			}
			catch(Exception e)
			{
			  MessageBox.Show(this,e.Message,"Error");
			}
			
			excel.Application.Quit();
			excel.Quit();
			System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
			GC.Collect();
		} 

		private void addControl(int n)
		{
			int x = groupBox1.Location.X ;
			int y = groupBox1.Location.Y ;

			for(int j=1;j<=n;j++)
			{
				x += 10;
				y += 10;
				System.Windows.Forms.Label l1 = new System.Windows.Forms.Label();
				l1.Text = "特征" + j.ToString();
				if( y + 21 > groupBox1.Location.Y + groupBox1.Height)
				{
				   x = x + 162;//162 = 100 + 50 + 10(间隔) + 2
				   y = groupBox1.Location.Y + 10;
				}
				l1.Location = new System.Drawing.Point(x,y);
				l1.Size = new Size(50,21);
				groupBox1.Controls.Add(l1);
				x = x + 2 + l1.Width; 
                
				System.Windows.Forms.TextBox tb = new System.Windows.Forms.TextBox();
				if( x + tb.Width > groupBox1.Location.X + groupBox1.Width)
				{
					MessageBox.Show(this,"特征值太多,容器无法容下!","tip");
					return;
				}
				tb.Location = new System.Drawing.Point(x,y);
				tb.Name = "tb" + j.ToString();
				//MessageBox.Show(this,tb.Name,"tip");
				groupBox1.Controls.Add(tb);
				x = x - 12 - l1.Width;
				y  = y  + tb.Height;    
			}
		}

		private void tabControl1_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if(tabControl1.SelectedIndex ==2)
			{
			   addControl(feature);
//				addControl(4);
			}
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			if(feature<=0)
			{
			  MessageBox.Show(this,"您还没有训练数据","ERROR");
			  return;
			}
			double[] input = new double[feature];
			for(int i=0;i<feature;i++)
			{	
				try
				{
					string temp = ((System.Windows.Forms.TextBox)groupBox1.Controls[ 2*i+1]).Text;
					input[i] = double.Parse(temp); 
				}
				catch(Exception ex)
				{
				   MessageBox.Show(this,ex.Message,"ERROR");
				}

			}

			tbResult.Text = ReturnType(input);
		}

		
	}
}

⌨️ 快捷键说明

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