private void bFlatBtnExecute_Click_1(object sender, EventArgs e) { List <Attribute> attributes = createAttribute(); List <DecisionQuery> decision = createDecisionQuery(); if (decision.Count == 0 || attributes.Count == 0) { MessageBox.Show("File imported is empty!"); return; } DecisionQuery result = decision[decision.Count - 1]; decision.Remove(result); int total = attributes.Count; List <float> calcultaHS = new List <float>(); foreach (Answer question in result.possibleAnswers) { int count = 0; foreach (Attribute attribute in attributes) { if (question.answer == attribute.answers[attribute.answers.Count - 1]) { count++; } } calcultaHS.Add(count); } hs = -calcultaHS[0] / total * (float)Math.Log(calcultaHS[0] / total) - calcultaHS[1] / total * (float)Math.Log(calcultaHS[1] / total); richTextBox1.Text += "HS: " + -calcultaHS[0] + "/" + total + "*log(" + calcultaHS[0] + "/" + total + ")" + -calcultaHS[1] + "/" + total + "*log(" + calcultaHS[1] + "/" + total + ")="; richTextBox1.Text += hs + "\n\n"; decisionTrees = new Node(null, decision, attributes); levels = partition(decisionTrees, result, 1, String.Empty) + 1; graph_Panel.Refresh(); return; }
public Node(DecisionQuery partition_question, List <DecisionQuery> questions, List <Attribute> attributes) { this.partition_question = partition_question; this.questions = questions; this.attributes = attributes; }
private int partition(Node parent, DecisionQuery result, int level, string possibleAnswerBranch) { if (parent.attributes.Count == 0 || parent.questions.Count == 0 || checkPartition(parent.attributes)) { return(level); } List <List <List <KeyValuePair <float, float> > > > featurePairsVectors = new List <List <List <KeyValuePair <float, float> > > >(); List <List <List <float> > > featureVectors = computeFeatureVectors(parent.questions, parent.attributes, result, ref featurePairsVectors); List <float> hsList = new List <float>(); for (int i = 0; i < featureVectors.Count; i++) { List <List <KeyValuePair <float, float> > > hs_question = new List <List <KeyValuePair <float, float> > >(); List <KeyValuePair <float, float> > hs_question1 = new List <KeyValuePair <float, float> >(); for (int j = 0; j < featureVectors[i].Count; j++) { richTextBox1.Text += "H(S" + possibleAnswerBranch + "_" + parent.questions[i].possibleAnswers[j].answer + "):"; float hs = 0; for (int k = 0; k < featureVectors[i][j].Count; k++) { float hs1 = -featurePairsVectors[i][j][k].Key / featurePairsVectors[i][j][k].Value * (float)Math.Log(featurePairsVectors[i][j][k].Key / featurePairsVectors[i][j][k].Value); richTextBox1.Text += -featurePairsVectors[i][j][k].Key + "/" + featurePairsVectors[i][j][k].Value; richTextBox1.Text += "*log(" + featurePairsVectors[i][j][k].Key + "/" + featurePairsVectors[i][j][k].Value + ")"; if (featurePairsVectors[i][j][k].Key == 0) { hs1 = 0; } hs += hs1; } hs_question1.Add(new KeyValuePair <float, float>(hs, featurePairsVectors[i][j][0].Value)); richTextBox1.Text += "=" + hs + "\n"; } float hs2 = 0; float total = 0; richTextBox1.Text += "H(S" + parent.questions[i].question + "):"; for (int t = 0; t < hs_question1.Count; t++) { total += hs_question1[t].Value; } for (int t = 0; t < hs_question1.Count; t++) { hs2 += hs_question1[t].Key * hs_question1[t].Value / total; richTextBox1.Text += hs_question1[t].Value + "/" + total + "*" + hs_question1[t].Key; if (t != hs_question1.Count - 1) { richTextBox1.Text += "+"; } } richTextBox1.Text += "=" + hs2 + "\n\n"; hsList.Add(hs2); } //int bestFeature = getBestFeature(featureVectors); int bestFeature = getBestFeature(hsList); richTextBox1.Text += " Ta chọn đặc tính tốt nhất: " + parent.questions[bestFeature].question.ToString() + "\n"; parent.partition_question = parent.questions[bestFeature]; List <DecisionQuery> brandClauses = new List <DecisionQuery>(); for (int i = 0; i < parent.questions.Count; i++) { if (i != bestFeature) { brandClauses.Add(parent.questions[i]); } } List <List <Attribute> > brandsAttributes = new List <List <Attribute> >(); for (int i = 0; i < parent.questions[bestFeature].possibleAnswers.Count; i++) { List <Attribute> brandAttribute = new List <Attribute>(); foreach (Attribute attribute in parent.attributes) { if (attribute.answers[bestFeature] == parent.questions[bestFeature].possibleAnswers[i].answer) { Attribute temp = attribute.copy(); temp.answers.RemoveAt(bestFeature); brandAttribute.Add(temp); } } brandsAttributes.Add(brandAttribute); } parent.branch = new List <Node>(); int maxlv = level; for (int i = 0; i < parent.questions[bestFeature].possibleAnswers.Count; i++) { // richTextBox1.Text += "-----Xét " + parent.questions[bestFeature].possibleAnswers.ToString() + "------"; Node brand = new Node(null, brandClauses, brandsAttributes[i]); if (featureVectors[bestFeature][i].Max() != 1) { int lv = partition(brand, result, level + 1, parent.questions[bestFeature].possibleAnswers[i].answer); if (lv > maxlv) { maxlv = lv; } } parent.branch.Add(brand); } return(maxlv); }
private void result_Algrithm(List <Attribute> attributes, DecisionQuery result, List <DecisionQuery> decisions) { float total = attributes.Count; List <float> ratioHS = new List <float>(); for (int j = 0; j < result.possibleAnswers.Count; j++) { float count = 0; for (int i = 0; i < attributes.Count; i++) { { if (result.possibleAnswers[j].answer == attributes[i].answers[attributes[i].answers.Count - 1]) { count++; } } } ratioHS.Add(count); } float HS = -ratioHS[0] / total * (float)Math.Log(ratioHS[0] / total) - ratioHS[1] / total * (float)Math.Log(ratioHS[1] / total); richTextBox1.Text += "Entropy tại root node của bài toán là:\n"; richTextBox1.Text += "=> H(S)=" + -ratioHS[0] + "/" + total + ".log(" + ratioHS[0] + "/" + total + ")" + -ratioHS[1] + "/" + total + ".log(" + ratioHS[1] + "/" + total + ")="; richTextBox1.Text += HS + "\n"; float Hmin = 0; List <float> H = new List <float>(); int temp = 0; for (int i = 0; i < decisions.Count; i++) { richTextBox1.Text += "Xét " + decisions[i].question.ToString() + "\n"; float Hsum = 0; for (int j = 0; j < decisions[i].possibleAnswers.Count; j++) { float yes = decisions[i].possibleAnswers[j].yes; float no = decisions[i].possibleAnswers[j].no; float total1 = yes + no; float HS1 = -yes / total1 * (float)Math.Log(yes / total1) - no / total1 * (float)Math.Log(no / total1); if (yes == 0 || no == 0) { HS1 = 0; } richTextBox1.Text += "=> H(S" + decisions[i].possibleAnswers[j].answer + ")=" + -yes + "/" + total1 + ".log(" + yes + "/" + total1 + ")" + -no + "/" + total1 + ".log(" + no + "/" + total1 + ")="; richTextBox1.Text += HS1 + "\n"; Hsum += HS1 * total1 / total; } H.Add(Hsum); richTextBox1.Text += "==>H(" + decisions[i].question + ",S)=" + Hsum + "\n"; } Hmin = H[0]; for (int k = 1; k < H.Count; k++) { if (H[k] < Hmin) { Hmin = H[k]; temp = k; } } for (int i = 0; i < decisions.Count; i++) { if (i == temp) { richTextBox1.Text += "H(" + decisions[i].question + ",S) nhỏ nhất nên Gain lớn nhất." + "==> Chọn " + decisions[i].question + " làm node gốc \n"; } } for (int i = 0; i < decisions[temp].possibleAnswers.Count; i++) { } }
private List <List <List <float> > > computeFeatureVectors(List <DecisionQuery> decisionQueries, List <Attribute> attributes, DecisionQuery result, ref List <List <List <KeyValuePair <float, float> > > > featurePairsVectors) { List <List <List <float> > > featureVectors = new List <List <List <float> > >(); for (int c = 0; c < decisionQueries.Count; c++) { List <List <float> > featureVector = new List <List <float> >(); List <List <KeyValuePair <float, float> > > lKeyValuePairs = new List <List <KeyValuePair <float, float> > >(); for (int i = 0; i < decisionQueries[c].possibleAnswers.Count; i++) { List <float> vector = new List <float>(); List <KeyValuePair <float, float> > keyValue = new List <KeyValuePair <float, float> >(); for (int j = 0; j < result.possibleAnswers.Count; j++) { int count = 0; int total = 0; foreach (Attribute attribute in attributes) { if (attribute.answers[c] == decisionQueries[c].possibleAnswers[i].ANSWER) { total++; if (attribute.answers[attribute.answers.Count - 1] == result.possibleAnswers[j].answer) { count++; } } } vector.Add((float)count / total); keyValue.Add(new KeyValuePair <float, float>(count, total)); } lKeyValuePairs.Add(keyValue); featureVector.Add(vector); //richTextBox1.Text += "H(S" + decisionQueries[c].possibleAnswers[i].answer +featureVector[i]..ToString()+ ")="+"\n"; } featureVectors.Add(featureVector); featurePairsVectors.Add(lKeyValuePairs); } return(featureVectors); }