using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
namespace TestOfAspNet.AspForm.Bayes
{
/// <summary>
/// Bayes 的摘要说明。
/// </summary>
public class Bayes : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.TextBox TextBox6;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid DataGrid2;
string strConnection=System.Configuration.ConfigurationSettings.AppSettings["ConnStr"];
float FrontYesProbability=0;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.DropDownList DropDownList2;
protected System.Web.UI.WebControls.DropDownList DropDownList3;
float FrontNoProbability=0;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
BindGrid();
//显示类标号属性
ListClass();
DataSet ds1=BindCredentialDropDwon();
this.DropDownList2.DataSource=ds1;
this.DropDownList2.DataTextField="CreditRating";
this.DropDownList2.DataValueField="CreditRating";
this.DropDownList2.DataBind();
DataSet ds2=BindIncomeDropDwon();
this.DropDownList3.DataSource=ds2;
this.DropDownList3.DataTextField="Income";
this.DropDownList3.DataValueField="Income";
this.DropDownList3.DataBind();
}
//先验概率的计算
FrontProbability(out FrontYesProbability, out FrontNoProbability);
string FrontYesProbility="p"+"(" + "IsBuyComputer="+ "/"yes/"" +")"+"="+FrontYesProbability;
string FrontNoProbility="p"+"(" + "IsBuyComputer="+ "/"no/"" +")"+"="+FrontNoProbability;
this.TextBox1.Text ="先验概率:"+"/n"
+ "================" +"/n"
+ FrontYesProbility +"/n"
+ FrontNoProbility +"/n/n";
}
private DataSet BindCredentialDropDwon()
{
string strQuery="select distinct CreditRating from Bayes";
SqlConnection Connection=new SqlConnection(strConnection);
Connection.Open();
SqlDataAdapter Adapter=new SqlDataAdapter(strQuery,Connection);
DataSet Ds=new DataSet();
Adapter.Fill(Ds);
return Ds;
}
private DataSet BindIncomeDropDwon()
{
string strQuery="select distinct Income from Bayes";
SqlConnection Connection=new SqlConnection(strConnection);
Connection.Open();
SqlDataAdapter Adapter=new SqlDataAdapter(strQuery,Connection);
DataSet Ds=new DataSet();
Adapter.Fill(Ds);
return Ds;
}
private void BindGrid()
{
SqlConnection Connection=new SqlConnection(strConnection);
string Query="select * from Bayes";
SqlDataAdapter Adapter = new SqlDataAdapter(Query,Connection);
DataSet Ds=new DataSet();
Adapter.Fill(Ds);
this.DataGrid1.DataSource=Ds.Tables[0].DefaultView;
this.DataGrid1.DataBind();
}
//计算每个类的先验概率
private void FrontProbability(out float FrontYesProbability, out float FrontNoProbability)
{
string strYesClassCount="select count(*) from Bayes where IsBuyComputer='yes'";
string strNoClassCount="select count(*) from Bayes where IsBuyComputer='no'";
SqlConnection Connection=new SqlConnection(strConnection);
Connection.Open();
SqlCommand YesCommand=new SqlCommand(strYesClassCount,Connection);
SqlCommand NoCommand=new SqlCommand(strNoClassCount,Connection);
float YesClassCount=float.Parse(YesCommand.ExecuteScalar().ToString());
float NoClassCount=float.Parse(NoCommand.ExecuteScalar().ToString());
FrontYesProbability=YesClassCount/(YesClassCount+NoClassCount);
FrontNoProbability=NoClassCount/(YesClassCount+NoClassCount);
}
//计算年龄的条件概率
private void AgeFrontProbability(out float InputAgeProbability,out float InputNoAgeProbability)
{
string strInputAgeCount="select count(*) from Bayes where Age='<=30'";
string strRecordCount="select count(*) from Bayes";
SqlConnection Connection=new SqlConnection(strConnection);
Connection.Open();
SqlCommand InputAgeCommand=new SqlCommand(strInputAgeCount,Connection);
SqlCommand RecordCommand=new SqlCommand(strRecordCount,Connection);
float InputAgeCount=float.Parse(InputAgeCommand.ExecuteScalar().ToString());
float RecordCount=float.Parse(RecordCommand.ExecuteScalar().ToString());
InputAgeProbability=InputAgeCount/RecordCount;
InputNoAgeProbability=(RecordCount-InputAgeCount)/RecordCount;
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
this.DataGrid2.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid2_PageIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void ListClass()
{
ArrayList Arr=GetClass();
this.DataGrid2.DataSource=Arr;
this.DataGrid2.DataBind();
}
private ArrayList GetClass()
{//获取类标号属性
SqlConnection Connection=new SqlConnection(strConnection);
Connection.Open();
string Query="select distinct IsBuyComputer from Bayes";
SqlCommand Command=new SqlCommand(Query,Connection);
SqlDataReader Dr=Command.ExecuteReader();
ArrayList Arr=new ArrayList();
while(Dr.Read())
{
Arr.Add(Dr["IsBuyComputer"].ToString());
}
return Arr;
}
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
this.BindGrid();
}
private void DataGrid2_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.DataGrid2.CurrentPageIndex=e.NewPageIndex;
ListClass();
}
private void Button1_Click(object sender, System.EventArgs e)
{
if(this.TextBox2.Text.Trim()==null||this.TextBox2.Text.Trim()=="")
{
Response.Write("<script>alert('必须要填写姓名!')</script>");
}
//判断所要训练的实例是否符合样本规范
//年龄的条件概率的计算
float pAge=0;
float _pAge=0;
//string strCondition="";
pFrontCalculate(out pAge,out _pAge,"Age","<=30");
string AgeFrontYesProbility="p"+"(" + "Age="+ "/"<30/"" +"|"+"IsBuyComputer="+ "/"yes/""+")"+"="+pAge;
string AgeFrontNoProbility="p"+"(" + "Age="+ "/"<30/"" +"|"+"IsBuyComputer="+ "/"no/""+")"+"="+_pAge;
//收入的条件概率的计算
float pIncome=0;
float _pIncome=0;
pFrontCalculate(out pIncome,out _pIncome,"Income","high");
string IncomeFrontYesProbility="p"+"(" + "Income="+ "/"high/"" +"|"+"IsBuyComputer="+ "/"yes/""+")"+"="+pIncome;
string IncomeFrontNoProbility="p"+"(" + "Income="+ "/"high/"" +"|"+"IsBuyComputer="+ "/"no/""+")"+"="+_pIncome;
//是否是学生的条件概率计算
float pStudent=0;
float _pStudent=0;
pFrontCalculate(out pStudent,out _pStudent,"Student","yes");
string StudentFrontYesProbility="p"+"(" + "Student="+ "/"yes/"" +"|"+"IsBuyComputer="+ "/"yes/""+")"+"="+pStudent;
string StudentFrontNoProbility="p"+"(" + "Student="+ "/"yes/"" +"|"+"IsBuyComputer="+ "/"no/""+")"+"="+_pStudent;
//可信等级条件概率的计算
float pCreditRating=0;
float _pCreditRating=0;
pFrontCalculate(out pCreditRating,out _pCreditRating,"CreditRating","yes");
string CreditRatingFrontYesProbility="p"+"(" + "CreditRating="+ "/"fair/"" +"|"+"IsBuyComputer="+ "/"yes/""+")"+"="+pCreditRating;
string CreditRatingFrontNoProbility="p"+"(" + "CreditRating="+ "/"fair/"" +"|"+"IsBuyComputer="+ "/"no/""+")"+"="+_pCreditRating;
this.TextBox1.Text+="先验条件概率:" +"/n"
+ "================" +"/n"
+ AgeFrontYesProbility +"/n"
+ AgeFrontNoProbility +"/n/n"
+ IncomeFrontYesProbility +"/n"
+ IncomeFrontNoProbility +"/n/n"
+ StudentFrontYesProbility +"/n"
+ StudentFrontNoProbility +"/n/n"
+ CreditRatingFrontYesProbility +"/n"
+ CreditRatingFrontNoProbility +"/n";
float BuyComputers=pAge*pIncome*pStudent*pCreditRating;
float NotBuyComputers=_pAge*_pIncome*_pStudent*_pCreditRating;
string p_x_y="p"+"(" + "X|"+"IsBuyComputer="+ "/"yes/""+")"+"="+BuyComputers;
string p_x_n="p"+"(" + "X|"+"IsBuyComputer="+ "/"no/""+")"+"="+NotBuyComputers;
float _p1=FrontYesProbability*BuyComputers;
float _p2=FrontNoProbability*NotBuyComputers;
string p1="p"+"(" + "X|"+"IsBuyComputer="+ "/"yes/""+")"+"p"+"("+"IsBuyComputer="+ "/"yes/""+")"+"="+_p1;
string p2="p"+"(" + "X|"+"IsBuyComputer="+ "/"no/""+")"+"p"+"("+"IsBuyComputer="+ "/"yes/""+")"+"="+_p2;
this.TextBox6.Text="结论概率:" +"/n"
+"================" +"/n"
+p1+"/n"
+p2+"/n";
this.Label1.Text=Compare(_p1,_p2);
}
private string Compare(float p1,float p2)
{
return p1>p2?"Buy Computer!":"Do not Buy Computer!";
}
private void pFrontCalculate(out float p1,out float p2,string Field,string Condition)
{ //此处需要修改下
//string strInputCount="select count(*) from Bayes where +'"+Field+"'+"="+'"+Condition+"'";
string strInputCount="select count(*) from Bayes where Age='"+Condition+"'";
string strRecordCount="select count(*) from Bayes";
SqlConnection Connection=new SqlConnection(strConnection);
Connection.Open();
SqlCommand Command=new SqlCommand(strInputCount,Connection);
SqlCommand RecordCommand=new SqlCommand(strRecordCount,Connection);
float InputCount=float.Parse(Command.ExecuteScalar().ToString());
float RecordCount=float.Parse(RecordCommand.ExecuteScalar().ToString());
p1=InputCount/RecordCount;
p2=(RecordCount-InputCount)/RecordCount;
}
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.SelectedItem ||
e.Item.ItemType == ListItemType.AlternatingItem ||
e.Item.ItemType == ListItemType.EditItem)
{
//添加自定义属性,当鼠标移过来时设置该行的背景色为"6699ff",并保存原背景色
e.Item.Attributes.Add("onmouseover","currentcolor=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
//添加自定义属性,当鼠标移走时还原该行的背景色
e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=currentcolor");
for (int i = 0; i< DataGrid1.Columns.Count; i++ )
{
e.Item.Cells[i].Attributes.Add("onmouseover","this.oldcolor=this.style.backgroundColor;this.style.backgroundColor='#99ccff'");
e.Item.Cells[i].Attributes.Add("onmouseout", "this.style.backgroundColor=this.oldcolor");
}
}
}
}
}