Ejemplo n.º 1
0
        //Hàm dùng để tại ra các khoảng giá trị đã rời rạc
        public static void TaoBayesObject(String colName, int khoang, DataTable dt)
        {
            BayesObjectTableAdapter BayesObjectTA = new BayesObjectTableAdapter();
            //DataSetTableAdapter dataSetTA = new DataSetTableAdapter();
            //DataTable dt = dataSetTA.GetData();
            decimal minValue              = Convert.ToDecimal(dt.Compute("min(" + colName + ")", string.Empty));
            decimal maxValue              = Convert.ToDecimal(dt.Compute("max(" + colName + ")", string.Empty));
            decimal giaTriMoi             = Math.Round((minValue + maxValue) / khoang, 3);
            decimal giaTriTrungBinhKhoang = giaTriMoi;

            for (int i = 1; i <= khoang; i++)
            {
                String khoangRoiRac = "";
                if (i == 1)
                {
                    khoangRoiRac = "[0," + giaTriMoi.ToString("0.###") + ")";
                }
                else if (i == khoang)
                {
                    khoangRoiRac = "[" + giaTriMoi.ToString("0.###") + ",+)";
                }
                else
                {
                    khoangRoiRac = "[" + (giaTriMoi - giaTriTrungBinhKhoang).ToString("0.###") + ","
                                   + giaTriMoi.ToString("0.###") + ")";
                }
                BayesObjectTA.Insert(colName, khoangRoiRac, 0, "True");
                BayesObjectTA.Insert(colName, khoangRoiRac, 0, "False");
                if (i != khoang - 1)
                {
                    giaTriMoi = giaTriMoi + giaTriTrungBinhKhoang;
                }
            }
        }
Ejemplo n.º 2
0
        //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);
        }
Ejemplo n.º 3
0
        //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));
            }
        }
Ejemplo n.º 4
0
        //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);
        }
Ejemplo n.º 5
0
        private void buttonXDataDiscretization_Click(object sender, EventArgs e)
        {
            int illegalInputCount = 0;
            BayesObjectTableAdapter bayesObjectTableAdapter = new BayesObjectTableAdapter();
            DataSetTempTableAdapter dataSetTempTableAdapter = new DataSetTempTableAdapter();
            DataSetTableAdapter     dataSetTableAdapter     = new DataSetTableAdapter();


            for (int i = listIntervalValue.Count - 1; i > 0; i--)
            {
                double currentValue  = listIntervalValue[i];
                double previousValue = listIntervalValue[i - 1];
                if (currentValue <= previousValue)
                {
                    illegalInputCount++;
                    break;
                }
            }
            if (illegalInputCount > 0)
            {
                MessageBox.Show("Giá trị của từng khoảng nhập vào không hợp lệ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                bayesObjectTableAdapter.DeleteByOne(this.ColName);
                DataTable dtDataSetForPreProcessing = dataSetTableAdapter.GetData();
                DiabetesDataSet.DataSetTempDataTable dtDataSetTemp = dataSetTempTableAdapter.GetData();

                int rowIndex = 0;
                foreach (DataRow dtRow in dtDataSetForPreProcessing.Rows)
                {
                    decimal colValue            = Convert.ToDecimal(dtRow[this.ColName]);
                    decimal maBn                = Convert.ToDecimal(dtRow["MaBn"]);
                    String  discretizationValue = DataDiscretization(colValue, this.listIntervalValue);

                    dtDataSetTemp.Rows[rowIndex][this.ColName] = discretizationValue;
                    rowIndex++;
                }
                dataSetTempTableAdapter.Update(dtDataSetTemp);
                CreateBayesObject(this.ColName, this.listIntervalValue);
            }
            this.Close();
        }
        private void buttonXDataDiscretization_Click(object sender, EventArgs e)
        {
            int illegalInputCount = 0;
            BayesObjectTableAdapter bayesObjectTableAdapter = new BayesObjectTableAdapter();
            DataSetTempTableAdapter dataSetTempTableAdapter = new DataSetTempTableAdapter();
            DataSetTableAdapter dataSetTableAdapter = new DataSetTableAdapter();


            for (int i = listIntervalValue.Count - 1; i > 0; i--)
            {
                double currentValue = listIntervalValue[i];
                double previousValue = listIntervalValue[i - 1];
                if (currentValue <= previousValue)
                {
                    illegalInputCount++;
                    break;
                }
                
            }
            if (illegalInputCount > 0)
                MessageBox.Show("Giá trị của từng khoảng nhập vào không hợp lệ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
            {
                bayesObjectTableAdapter.DeleteByOne(this.ColName);
                DataTable dtDataSetForPreProcessing = dataSetTableAdapter.GetData();
                DiabetesDataSet.DataSetTempDataTable dtDataSetTemp = dataSetTempTableAdapter.GetData();

                int rowIndex = 0;
                foreach (DataRow dtRow in dtDataSetForPreProcessing.Rows)
                {
                    decimal colValue = Convert.ToDecimal(dtRow[this.ColName]);
                    decimal maBn = Convert.ToDecimal(dtRow["MaBn"]);
                    String discretizationValue = DataDiscretization(colValue, this.listIntervalValue);

                    dtDataSetTemp.Rows[rowIndex][this.ColName] = discretizationValue;
                    rowIndex++;
                }
                dataSetTempTableAdapter.Update(dtDataSetTemp);
                CreateBayesObject(this.ColName, this.listIntervalValue);
            }
            this.Close();
        }
Ejemplo n.º 7
0
        //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;
        }
Ejemplo n.º 8
0
        //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));
            }
        }
Ejemplo n.º 9
0
        private void CreateBayesObject(String colName, List <double> listInputIntervalValue)
        {
            BayesObjectTableAdapter bayesObjectTableAdapter = new BayesObjectTableAdapter();

            for (int i = 0; i < listInputIntervalValue.Count; i++)
            {
                String InputIntervalValue = "";
                double currentValue       = listInputIntervalValue[i];
                double nextValue          = 0;
                if (i == listInputIntervalValue.Count - 1)
                {
                    InputIntervalValue = "[" + currentValue.ToString("0.###") + ",+)";
                }
                else
                {
                    nextValue          = listInputIntervalValue[i + 1];
                    InputIntervalValue = "[" + currentValue.ToString("0.###") + "," + nextValue.ToString("0.###") + ")";
                }
                bayesObjectTableAdapter.Insert(colName, InputIntervalValue, 0, TableMetaData.PositiveString);
                bayesObjectTableAdapter.Insert(colName, InputIntervalValue, 0, TableMetaData.NegativeString);
            }
        }
Ejemplo n.º 10
0
 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;
     }
 }
 private void CreateBayesObject(String colName, List<double> listInputIntervalValue)
 {
     BayesObjectTableAdapter bayesObjectTableAdapter = new BayesObjectTableAdapter();
     for (int i = 0; i < listInputIntervalValue.Count; i++)
     {
         String InputIntervalValue = "";
         double currentValue=listInputIntervalValue[i];
         double nextValue = 0;
         if (i == listInputIntervalValue.Count - 1)
             InputIntervalValue = "[" + currentValue.ToString("0.###") + ",+)";
         else
         {
             nextValue = listInputIntervalValue[i + 1];
             InputIntervalValue = "[" + currentValue.ToString("0.###") + "," + nextValue.ToString("0.###") + ")";
         }
         bayesObjectTableAdapter.Insert(colName, InputIntervalValue, 0, TableMetaData.PositiveString);
         bayesObjectTableAdapter.Insert(colName, InputIntervalValue, 0, TableMetaData.NegativeString);
     }
 }
        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;
            }
        }