/// <summary> /// Calculates a sorted list of information gains by splitting /// on each axes and value and measuring the resulting disorder /// </summary> /// <param name="disorderType">The disorder calculation algorithm to use</param> /// <returns></returns> public IDictionary <int, Number> GetInformationGain(DisorderType disorderType) { var grid = new Dictionary <int, Number>(_matrix.Rows); var baseline = GetDisorder(disorderType, _matrix); var axes = _matrix.Columns; for (var i = 0; i < axes; i++) { var row = _matrix[i]; var values = row.Distinct(); var disorder = 0.0; foreach (var value in values) { var split = Split(i, value); var probability = split.Rows / (float)_matrix.Rows; disorder += probability * GetDisorder(disorderType, split); } var gain = baseline - disorder; grid.Add(i, gain); } var sorted = grid.OrderBy(kv => kv.Value).ToDictionary(x => x.Key, x => x.Value);; return(sorted); }
/// <summary> /// Calculates a sorted list of information gains by splitting /// on each axes and value and measuring the resulting disorder /// </summary> /// <param name="disorderType">The disorder calculation algorithm to use</param> /// <returns></returns> public IDictionary<int, Number> GetInformationGain(DisorderType disorderType) { var grid = new Dictionary<int, Number>(_matrix.Rows); var baseline = GetDisorder(disorderType, _matrix); var axes = _matrix.Columns; for (var i = 0; i < axes; i++) { var row = _matrix[i]; var values = row.Distinct(); var disorder = 0.0; foreach(var value in values) { var split = Split(i, value); var probability = split.Rows / (float)_matrix.Rows; disorder += probability * GetDisorder(disorderType, split); } var gain = baseline - disorder; grid.Add(i, gain); } var sorted = grid.OrderBy(kv => kv.Value).ToDictionary(x => x.Key, x => x.Value); ; return sorted; }
private static ITreeNode CreateTree(Matrix dataSet, IList<string> labels, DisorderType disorderType) { var outcomes = dataSet.GetOutcomes(); if(outcomes.Distinct().Count().Equals(1)) { return new DecisionTreeLeafNode(outcomes[0]); } var outcome = dataSet.GetMajorityOutcome(); if(dataSet.Columns == 1) { return new DecisionTreeLeafNode(outcome); } ITreeNode node = new DecisionTreeNode(dataSet, outcome); var casted = (DecisionTreeNode) node; var best = casted.GetInformationGain(disorderType).First(); var label = labels[best.Key]; casted.Label = label; labels = labels.Where(l => !l.Equals(label)).ToList(); var subset = dataSet.Select(row => row[best.Key]).ToList(); var values = subset.Distinct().ToList(); foreach(var value in values) { var copy = new List<string>(labels); var split = casted.Split(best.Key, value); var child = CreateTree(split, copy, disorderType); casted.AddChild(child); } ReverseNodeOrder(casted); return node; }
private static ITreeNode CreateTree(Matrix dataSet, IList <string> labels, DisorderType disorderType) { var outcomes = dataSet.GetOutcomes(); if (outcomes.Distinct().Count().Equals(1)) { return(new DecisionTreeLeafNode(outcomes[0])); } var outcome = dataSet.GetMajorityOutcome(); if (dataSet.Columns == 1) { return(new DecisionTreeLeafNode(outcome)); } ITreeNode node = new DecisionTreeNode(dataSet, outcome); var casted = (DecisionTreeNode)node; var best = casted.GetInformationGain(disorderType).First(); var label = labels[best.Key]; casted.Label = label; labels = labels.Where(l => !l.Equals(label)).ToList(); var subset = dataSet.Select(row => row[best.Key]).ToList(); var values = subset.Distinct().ToList(); foreach (var value in values) { var copy = new List <string>(labels); var split = casted.Split(best.Key, value); var child = CreateTree(split, copy, disorderType); casted.AddChild(child); } ReverseNodeOrder(casted); return(node); }
protected override void PrepareMenuCategories() { int categoryTypes = disordersSceneManager.DisordersCategories.DisordersCategoriesList.Count; for (int i = 0; i < categoryTypes; i++) { GameObject category = (GameObject)Instantiate(categoryWholePrefab, transform.position, Quaternion.identity); category.SetActive(true); category.name = disordersSceneManager.DisordersCategories.DisordersCategoriesList [i].Type.ToString(); category.transform.SetParent(menuContent.transform, true); category.transform.localScale = Vector3.one; category.transform.Find("ToggleCategory").GetComponentInChildren <TextMeshProUGUI> ().text = disordersSceneManager.DisordersCategories.DisordersCategoriesList [i].Name; categoryList.Add(category); DisorderType statement = disordersSceneManager.DisordersCategories.DisordersCategoriesList [i].Type; int elements = 0; float height = 0; float spacing = category.GetComponent <VerticalLayoutGroup> ().spacing; switch (statement) { case DisorderType.REFRACTIVE_ERRORS: elements = disordersSceneManager.DisordersElements.RefractiveErrorsList.Count; height = elements * (toggleElementPrefab.GetComponent <RectTransform> ().sizeDelta.y + spacing) + category.GetComponent <RectTransform> ().sizeDelta.y + spacingCategory; category.GetComponent <RectTransform> ().sizeDelta = new Vector2(category.GetComponent <RectTransform> ().sizeDelta.x, height); break; case DisorderType.DISEASES: elements = disordersSceneManager.DisordersElements.DiseasesList.Count; height = elements * (toggleElementPrefab.GetComponent <RectTransform> ().sizeDelta.y + spacing) + category.GetComponent <RectTransform> ().sizeDelta.y + spacingCategory; category.GetComponent <RectTransform> ().sizeDelta = new Vector2(category.GetComponent <RectTransform> ().sizeDelta.x, height); break; default: break; } } }
/// <summary> /// Calculates the disorder of given matrix of values /// </summary> /// <param name="disorderType">The disorder calculation algorithm to use</param> /// <param name="matrix">The data set</param> /// <returns></returns> public static Number GetDisorder(DisorderType disorderType, Matrix matrix) { Number baseline; switch (disorderType) { case DisorderType.ShannonEntropy: baseline = Disorder.ShannonEntropy(matrix); break; case DisorderType.GiniImpurity: baseline = Disorder.GiniImpurity(matrix); break; case DisorderType.ClassificationError: baseline = Disorder.ClassificationError(matrix); break; default: throw new ArgumentOutOfRangeException("disorderType"); } return(baseline); }
/// <summary> /// Calculates the disorder of given matrix of values /// </summary> /// <param name="disorderType">The disorder calculation algorithm to use</param> /// <param name="matrix">The data set</param> /// <returns></returns> public static Number GetDisorder(DisorderType disorderType, Matrix matrix) { Number baseline; switch (disorderType) { case DisorderType.ShannonEntropy: baseline = Disorder.ShannonEntropy(matrix); break; case DisorderType.GiniImpurity: baseline = Disorder.GiniImpurity(matrix); break; case DisorderType.ClassificationError: baseline = Disorder.ClassificationError(matrix); break; default: throw new ArgumentOutOfRangeException("disorderType"); } return baseline; }
/// <summary> /// Builds a tree using the Iterative Dichotimiser 3 (ID3) algorithm /// <see href="http://en.wikipedia.org/wiki/ID3_algorithm" /> /// </summary> /// <returns></returns> public static ITreeNode ID3(Matrix dataSet, IList<string> labels, DisorderType disorderType) { return CreateTree(dataSet, labels, disorderType); }
/// <summary> /// Builds a tree using the Iterative Dichotimiser 3 (ID3) algorithm /// <see href="http://en.wikipedia.org/wiki/ID3_algorithm" /> /// </summary> /// <returns></returns> public static ITreeNode ID3(Matrix dataSet, IList <string> labels, DisorderType disorderType) { return(CreateTree(dataSet, labels, disorderType)); }