//Hàm dùng để lấy một tập gồm các khoảng rời rạc của một cột public static List <double> GetListIntervalValue(String colName) { List <double> listIntervalValues = new List <double>(); if (colName != "GioiTinh") { BayesObjectTableAdapter bayesObjectTableAdapter = new BayesObjectTableAdapter(); DataRow[] dtIntervalValue = bayesObjectTableAdapter.GetData().Select("TenThuocTinh='" + colName + "'"); foreach (DataRow dtRow in dtIntervalValue) { String[] listString = dtRow[2].ToString().Split(','); String str = listString[0].Substring(1, listString[0].Length - 1); double intervalValue = Convert.ToDouble(str); if (!listIntervalValues.Contains(intervalValue)) { listIntervalValues.Add(intervalValue); } //Dành cho trường hợp thuộc tính chỉ có 1 khoảng if (dtIntervalValue.Count() == 2) { String secondStr = listString[1].Substring(0, listString[1].Length - 1); double secondIntervalValue = Convert.ToDouble(secondStr); if (!listIntervalValues.Contains(secondIntervalValue)) { listIntervalValues.Add(secondIntervalValue); } } } } return(listIntervalValues); }
//Hàm dùng để huấn luyện dữ liệu dành cho thuật toán Naive Bayes public static void HuanLuyenBayes(int row) { BayesObjectTableAdapter dtBayesAdapter = new BayesObjectTableAdapter(); DataSetTempTableAdapter dtSetTempTA = new DataSetTempTableAdapter(); int rowCount = row; DataTable dataForTraining = dtSetTempTA.GetDataByNumber(rowCount); DiabetesDataSet.BayesObjectDataTable dtBayes = dtBayesAdapter.GetData(); foreach (DataRow dtRow in dtBayes.Rows) { String colName = dtRow[1].ToString(); String khoangRoiRac = dtRow[2].ToString(); String tieuDuong = dtRow[4].ToString(); String iQuery = "" + colName + "='" + khoangRoiRac + "' and TieuDuong='" + tieuDuong + "'"; int soLuong = dataForTraining.Select(iQuery).Count(); if (soLuong == 0) { soLuong = 1; } dtRow[3] = soLuong; dtBayesAdapter.Update((dtBayes)); } }
private void buttonXDiscretizationDataStatistics_Click(object sender, EventArgs e) { if (checkedListBoxColumnName.SelectedIndex == -1) { MessageBox.Show("Chưa chọn thuộc tính để xem thống kê", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { String colName = checkedListBoxColumnName.SelectedItem.ToString(); BayesObjectTableAdapter bayesObjectTableAdapter = new BayesObjectTableAdapter(); DataSetTempTableAdapter dataSetTempTableAdapter = new DataSetTempTableAdapter(); DataTable dtBayesForStatistics = bayesObjectTableAdapter.GetData(); DataTable dtSetTempForStatistics = dataSetTempTableAdapter.GetData(); DataRow[] bayesRows = bayesObjectTableAdapter.GetData().Select("TenThuocTinh='" + colName + "'"); dtBayesForStatistics.Clear(); int iCount = 0; foreach (DataRow bayesRow in bayesRows) { String tenThuocTinh = bayesRow["TenThuocTinh"].ToString(); String khoangRoiRac = bayesRow["KhoangRoiRac"].ToString(); String tieuDuong = bayesRow["TieuDuong"].ToString(); int tongSoLuong = 0; String iQuery = "" + tenThuocTinh + "='" + khoangRoiRac + "' and TieuDuong='" + tieuDuong + "'"; tongSoLuong = dtSetTempForStatistics.Select(iQuery).Count(); DataRow newRow = dtBayesForStatistics.NewRow(); iCount++; newRow[0] = iCount; newRow[1] = tenThuocTinh; newRow[2] = khoangRoiRac; newRow[3] = tongSoLuong; newRow[4] = tieuDuong; dtBayesForStatistics.Rows.Add(newRow); } dataGridViewXDescriptiveData.DataSource = dtBayesForStatistics; dataGridViewXDescriptiveData.Columns["ID"].Visible = true; } }
//Hàm dùng để tính kết quả của một bảng trong bộ thử nghiệm public static DataTable NaiveBayes(DataTable dtTestSet) { BayesObjectTableAdapter bayesTA = new BayesObjectTableAdapter(); DataSetTempTableAdapter dataSetTempTA = new DataSetTempTableAdapter(); int dataForTrainingRowsCount = (int)dataSetTempTA.CountRows() - dtTestSet.Rows.Count; DataTable dtBayes = bayesTA.GetData(); DataTable dtdataSetTemp = dataSetTempTA.GetDataByNumber(dataForTrainingRowsCount); int possiveNumber = dtdataSetTemp.Select("TieuDuong='True'").Count(); int negativeNumber = dtdataSetTemp.Select("TieuDuong='False'").Count(); int allNumber = possiveNumber + negativeNumber; int indexLastColumn = dtTestSet.Columns.Count - 1; foreach (DataRow dtRow in dtTestSet.Rows) { Decimal pColTrue = 1; Decimal pColFalse = 1; for (int i = 1; i < dtTestSet.Columns.Count - 2; i++) { String colName = dtTestSet.Columns[i].ColumnName; String khoangRoiRac = dtRow[i].ToString(); DataRow possiveRow = dtBayes.Select("TenThuocTinh='" + colName + "' and KhoangRoiRac='" + khoangRoiRac + "' and TieuDuong='True'")[0]; DataRow negativeRow = dtBayes.Select("TenThuocTinh='" + colName + "' and KhoangRoiRac='" + khoangRoiRac + "' and TieuDuong='False'")[0]; Decimal ColPossiveNumber = Convert.ToDecimal(possiveRow[3]); Decimal ColNegativeNumber = Convert.ToDecimal(negativeRow[3]); pColTrue = pColTrue * ColPossiveNumber / possiveNumber; pColFalse = pColFalse * ColNegativeNumber / negativeNumber; } Decimal pRowTrue = pColTrue * possiveNumber / allNumber; Decimal pRowFalse = pColFalse * negativeNumber / allNumber; if (pRowFalse == 0) { dtRow["TieuDuong"] = "False"; } else { if (pRowTrue > pRowFalse) { dtRow["TieuDuong"] = "True"; } else { dtRow["TieuDuong"] = "False"; } } } return(dtTestSet); }
//Hàm dùng để tính kết quả của một bảng trong bộ thử nghiệm public static DataTable NaiveBayes(DataTable dtTestSet) { BayesObjectTableAdapter bayesTA = new BayesObjectTableAdapter(); DataSetTempTableAdapter dataSetTempTA = new DataSetTempTableAdapter(); int dataForTrainingRowsCount = (int)dataSetTempTA.CountRows() - dtTestSet.Rows.Count; DataTable dtBayes = bayesTA.GetData(); DataTable dtdataSetTemp = dataSetTempTA.GetDataByNumber(dataForTrainingRowsCount); int possiveNumber = dtdataSetTemp.Select("TieuDuong='True'").Count(); int negativeNumber = dtdataSetTemp.Select("TieuDuong='False'").Count(); int allNumber = possiveNumber + negativeNumber; int indexLastColumn = dtTestSet.Columns.Count - 1; foreach (DataRow dtRow in dtTestSet.Rows) { Decimal pColTrue = 1; Decimal pColFalse = 1; for (int i = 1; i < dtTestSet.Columns.Count - 2; i++) { String colName = dtTestSet.Columns[i].ColumnName; String khoangRoiRac = dtRow[i].ToString(); DataRow possiveRow = dtBayes.Select("TenThuocTinh='" + colName + "' and KhoangRoiRac='" + khoangRoiRac + "' and TieuDuong='True'")[0]; DataRow negativeRow = dtBayes.Select("TenThuocTinh='" + colName + "' and KhoangRoiRac='" + khoangRoiRac + "' and TieuDuong='False'")[0]; Decimal ColPossiveNumber = Convert.ToDecimal(possiveRow[3]); Decimal ColNegativeNumber = Convert.ToDecimal(negativeRow[3]); pColTrue = pColTrue * ColPossiveNumber / possiveNumber; pColFalse = pColFalse * ColNegativeNumber / negativeNumber; } Decimal pRowTrue = pColTrue * possiveNumber / allNumber; Decimal pRowFalse = pColFalse * negativeNumber / allNumber; if (pRowFalse == 0) dtRow["TieuDuong"] = "False"; else { if (pRowTrue > pRowFalse) dtRow["TieuDuong"] = "True"; else dtRow["TieuDuong"] = "False"; } } return dtTestSet; }
//Hàm dùng để huấn luyện dữ liệu dành cho thuật toán Naive Bayes public static void HuanLuyenBayes(int row) { BayesObjectTableAdapter dtBayesAdapter = new BayesObjectTableAdapter(); DataSetTempTableAdapter dtSetTempTA = new DataSetTempTableAdapter(); int rowCount = row; DataTable dataForTraining = dtSetTempTA.GetDataByNumber(rowCount); DiabetesDataSet.BayesObjectDataTable dtBayes = dtBayesAdapter.GetData(); foreach (DataRow dtRow in dtBayes.Rows) { String colName = dtRow[1].ToString(); String khoangRoiRac = dtRow[2].ToString(); String tieuDuong = dtRow[4].ToString(); String iQuery = "" + colName + "='" + khoangRoiRac + "' and TieuDuong='" + tieuDuong + "'"; int soLuong = dataForTraining.Select(iQuery).Count(); if (soLuong == 0) soLuong = 1; dtRow[3] = soLuong; dtBayesAdapter.Update((dtBayes)); } }
private void buttonXDiscretizationDataStatistics_Click(object sender, EventArgs e) { if (checkedListBoxColumnName.SelectedIndex == -1) MessageBox.Show("Chưa chọn thuộc tính để xem thống kê", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error); else { String colName = checkedListBoxColumnName.SelectedItem.ToString(); BayesObjectTableAdapter bayesObjectTableAdapter = new BayesObjectTableAdapter(); DataSetTempTableAdapter dataSetTempTableAdapter = new DataSetTempTableAdapter(); DataTable dtBayesForStatistics = bayesObjectTableAdapter.GetData(); DataTable dtSetTempForStatistics = dataSetTempTableAdapter.GetData(); DataRow[] bayesRows = bayesObjectTableAdapter.GetData().Select("TenThuocTinh='" + colName + "'"); dtBayesForStatistics.Clear(); int iCount = 0; foreach (DataRow bayesRow in bayesRows) { String tenThuocTinh = bayesRow["TenThuocTinh"].ToString(); String khoangRoiRac = bayesRow["KhoangRoiRac"].ToString(); String tieuDuong = bayesRow["TieuDuong"].ToString(); int tongSoLuong = 0; String iQuery = "" + tenThuocTinh + "='" + khoangRoiRac + "' and TieuDuong='" + tieuDuong + "'"; tongSoLuong = dtSetTempForStatistics.Select(iQuery).Count(); DataRow newRow = dtBayesForStatistics.NewRow(); iCount++; newRow[0] = iCount; newRow[1] = tenThuocTinh; newRow[2] = khoangRoiRac; newRow[3] = tongSoLuong; newRow[4] = tieuDuong; dtBayesForStatistics.Rows.Add(newRow); } dataGridViewXDescriptiveData.DataSource = dtBayesForStatistics; dataGridViewXDescriptiveData.Columns["ID"].Visible = true; } }