?? form2.cs
字號:
/// <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 + -