private DecisionAttributeOccurence GetRowsFromPath(List <int> path) { DecisionColumn currentDecisionColumn = null; DecisionAttributeOccurence currentAttribute = null; var positiveRows = new List <int>(); var negativeRows = new List <int>(); for (var i = 0; i < path.Count; i++) { if (i == 0 || i % 2 == 0) { currentDecisionColumn = DecisionColumns[path[i]]; } else { currentAttribute = currentDecisionColumn?.Attributes[path[i]]; } if (i <= 0) { continue; } if (positiveRows.Count < 1 && negativeRows.Count < 1) { positiveRows = currentAttribute.PositiveRowNumbers; negativeRows = currentAttribute.NegativeRowNumbers; } else { positiveRows = positiveRows.Intersect(currentAttribute.PositiveRowNumbers).ToList(); negativeRows = negativeRows.Intersect(currentAttribute.NegativeRowNumbers).ToList(); } } return(new DecisionAttributeOccurence(currentAttribute.Value, positiveRows, negativeRows)); }
public List <DecisionColumn> CalculateDecisions() { DecisionColumns = new List <DecisionColumn>(); for (int i = 0; i < DecisionRows.First().Length - 1; i++) { var decisionColumn = new DecisionColumn(); var uniqueValues = DecisionRows.Select(value => value[i]).Distinct().ToList(); foreach (var uniqueValue in uniqueValues) { var decisionAttributeOccurence = new DecisionAttributeOccurence(uniqueValue); for (int j = 0; j < DecisionRows.Count; j++) { if (DecisionRows[j][i] == uniqueValue) { var lastColumnDecision = LastRowOccurences.Where(occurence => occurence.RowNumbers.Contains(j + 1)).Select(occurence => occurence.Decision).First(); if (lastColumnDecision == DecisionType.Positive) { decisionAttributeOccurence.PositiveRowNumbers.Add(j + 1); } else { decisionAttributeOccurence.NegativeRowNumbers.Add(j + 1); } } } decisionColumn.Attributes.Add(decisionAttributeOccurence); } DecisionColumns.Add(decisionColumn); } CalculateColumnsEntropies(); CalculateColumnInformationGain(); var rejectedList = DecisionColumns.Where(column => AlreadyUsedIndexes.Contains((int)column.ColumnIndex)); var bestOne = DecisionColumns.Except(rejectedList).MaxBy(column => column.InformationGain); //Nieużyta z największym przyrostem informacji var indexes = new List <int> { (int)bestOne.ColumnIndex }; DoUnknownThing(indexes, null); return(DecisionColumns); }