private static Model.Row CalculateNewMean(int classIndex, List <RowClass> rowClasses, Model.Table table) { Model.Row newMean = new Model.Row(); for (int i = 0; i < table.Rows[0].Cells.Count - 1; i++) { double sum = 0; double counter = 0; for (int j = 0; j < table.Rows.Count; j++) { if (rowClasses[j].ClassIndex == classIndex) { sum += double.Parse(table.Rows[j].Cells[i].Value); counter++; } } double cellMean = sum / counter; newMean.Cells.Add(new Model.Cell(cellMean.ToString())); } foreach (RowClass rowClass in rowClasses) { } return(newMean); }
private static bool CheckIfMeanExists(Model.Row newRandomValue, List <Model.Row> means) { if (means.Count == 0) { return(false); } foreach (Model.Row mean in means) { bool allCellsAreTheSame = true; for (int i = 0; i < newRandomValue.Cells.Count; i++) { if (newRandomValue.Cells[i].Value != mean.Cells[i].Value) { allCellsAreTheSame = false; break; } } if (allCellsAreTheSame) { return(true); } } return(false); }
public void Load(string filename) { using (StreamReader reader = new StreamReader(filename)) { bool first = true; while (!reader.EndOfStream) { string[] line = reader.ReadLine().Split(';'); if (first) { this.Header = new List <string>(line); first = false; continue; } if (line.Length != this.Header.Count) { throw new InvalidOperationException("Header and data row columns count mismatch"); } Row r = new Model.Row(line); this.Rows.Add(r); } } }
public static double GetManhattanDistance(Model.Row rowA, Model.Row rowB) { double sum = 0; for (int i = 0; i < rowA.Cells.Count - 1; i++) { sum += Math.Abs(double.Parse(rowA.Cells[i].Value) - double.Parse(rowB.Cells[i].Value)); } return(sum); }
//public static double GetEuclideanDistance(double pointOneX, double pointOneY, double pointTwoX, double pointTwoY) //{ // return Math.Sqrt((Math.Pow(pointOneX - pointTwoX, 2) + (Math.Pow(pointOneY - pointTwoY, 2)))); //} private static double GetEuclidesDistance(Model.Row rowA, Model.Row rowB) { double sum = 0; for (int i = 0; i < rowA.Cells.Count - 1; i++) { sum += Math.Pow(double.Parse(rowA.Cells[i].Value) - double.Parse(rowB.Cells[i].Value), 2); } return(Math.Sqrt(sum)); }
private void ViewFilterResults(Filter filter, Model.Table table) { if (filter.Condition == 1) { Gtk.ListStore store = treeView.Model as Gtk.ListStore; // Empty the store on every new table-selection. if (store != null) { foreach (Gtk.TreeViewColumn col in treeView.Columns) { treeView.RemoveColumn(col); } store.Clear(); } // Construct the array of types which is the size of the columns. Type[] types = new Type[table.Columns.Count]; for (int i = 0, e = table.Columns.Count; i < e; ++i) { types[i] = typeof(string); } // The list store must take array of types which it will hold. store = new Gtk.ListStore(types); treeView.Model = store; // Put columns and cell renderers in the ListStore. There we will put the actual values. for (int i = 0, e = table.Columns.Count; i < e; ++i) { Model.Column dcol = table.Columns[i]; Gtk.TreeViewColumn col = new Gtk.TreeViewColumn(); Gtk.CellRendererText cell = new Gtk.CellRendererText(); col.PackStart(cell, true); col.AddAttribute(cell, "text", i); col.Title = dcol.Meta.Name; treeView.AppendColumn(col); } // Now fill in the rows. for (int i = 0; i < table.GetAsRows().Count; i++) { Model.Row r = table.GetAsRows()[i]; if (table.Columns[0].Values[i].ToString().Contains("1")) { store.AppendValues(r.GetAsStringArray()); } } treeView.ShowAll(); } }
private static Model.Row GetRandomValue(Model.Table table, Random random) { Model.Row randomRow = new Model.Row(); // Random random = new Random(); for (int i = 0; i < table.Headers.Cells.Count - 1; i++) { int randomRowIndex = random.Next(table.Rows.Count()); randomRow.Cells.Add(new Model.Cell(table.Rows[randomRowIndex].Cells[i].Value)); } return(randomRow); }
public static bool ClassifyRow(Model.Table table, Model.Row rowToClassification, int rowIndex) { List <Leaf> leafs = GenerateLeafs(table.Rows[0].Cells.Count - 1); List <Leaf> _leafsTmp; for (int i = 0; i < table.Rows.Count; i++) { _leafsTmp = leafs; for (int j = 0; j < table.Rows[i].Cells.Count - 1; j++) { _leafsTmp = _leafsTmp.Where(l => l.ArgValues[j] == int.Parse(table.Rows[i].Cells[j].Value)).ToList(); } int leafIndexToIncreaseClassIndex = _leafsTmp.SingleOrDefault().LeafIndex; int classIndex = int.Parse(table.Rows[i].Cells.Last().Value); leafs.Where(l => l.LeafIndex == leafIndexToIncreaseClassIndex).SingleOrDefault().ClassIndexCounts[classIndex]++; } foreach (Leaf leaf in leafs) { int maxCount = leaf.ClassIndexCounts.Max(); for (int i = 0; i < leaf.ClassIndexCounts.Count; i++) { if (leaf.ClassIndexCounts[i] == maxCount) { leaf.ClassIndex = i; break; } } } List <Leaf> _leafsTmpToClassification = leafs; for (int i = 0; i < rowToClassification.Cells.Count - 1; i++) { _leafsTmpToClassification = _leafsTmpToClassification.Where(l => l.ArgValues[i] == double.Parse(rowToClassification.Cells[i].Value)).ToList(); } int leafClassificationIndex = _leafsTmpToClassification.SingleOrDefault().LeafIndex; _classes.Add(leafs[leafClassificationIndex].ClassIndex); if (leafs[leafClassificationIndex].ClassIndex == double.Parse(rowToClassification.Cells[rowToClassification.Cells.Count - 1].Value)) { return(true); } return(false); }
public static double GetInfinityDistance(Model.Row rowA, Model.Row rowB) // chebyshev distance // odległość czebyszewa { double result = Math.Abs(double.Parse(rowA.Cells[0].Value) - double.Parse(rowB.Cells[0].Value)); for (int i = 1; i < rowA.Cells.Count - 1; i++) { double distance_i = Math.Abs(double.Parse(rowA.Cells[i].Value) - double.Parse(rowB.Cells[i].Value)); if (distance_i > result) { result = distance_i; } } return(result); }
private static RowClass GetRowClass(Model.Row row, int rowIndex, List <Model.Row> means) { RowClass rowClass = new RowClass(); rowClass.RowIndex = rowIndex; rowClass.ClassIndex = 0; double currentDistance = GetInfinityDistance(row, means[0]); for (int i = 1; i < means.Count; i++) { double distance = GetInfinityDistance(row, means[i]); if (distance < currentDistance) { rowClass.ClassIndex = i; currentDistance = distance; } } return(rowClass); }
public static Model.Table Group(Model.Table table, int decisionalColumnIndex, int numberOfGroups) { if ((table.Rows.Count() - 1) != decisionalColumnIndex) { //zamienic kolejnosc kolumn, potem sie zrobi xd } Random random = new Random(); List <Model.Row> means = new List <Model.Row>(); for (int i = 0; i < numberOfGroups; i++) { Model.Row newRandomValue = GetRandomValue(table, random); while (CheckIfMeanExists(newRandomValue, means)) { newRandomValue = GetRandomValue(table, random); } means.Add(newRandomValue); // System.Threading.Thread.Sleep(217); } //Model.Row row1 = new Model.Row(); // row1.Cells.Add(new Model.Cell("5")); // row1.Cells.Add(new Model.Cell("6")); //Model.Row row2 = new Model.Row(); // row2.Cells.Add(new Model.Cell("2")); // row2.Cells.Add(new Model.Cell("7")); //Model.Row row3 = new Model.Row(); // row3.Cells.Add(new Model.Cell("1")); // row3.Cells.Add(new Model.Cell("3")); //Model.Row row4 = new Model.Row(); // row4.Cells.Add(new Model.Cell("4")); // row4.Cells.Add(new Model.Cell("3")); //means.Add(row1); means.Add(row2); means.Add(row3); means.Add(row4); List <RowClass> rowClasses = new List <RowClass>(); for (int i = 0; i < table.Rows.Count; i++) { rowClasses.Add(GetRowClass(table.Rows[i], i, means)); } List <RowClass> rowClasses_prev = new List <RowClass>(); int safetyCounter = 0; while (safetyCounter < 1000 || (CheckIfMeansChanged(rowClasses_prev, rowClasses))) //while (safetyCounter < 1000) { rowClasses_prev = rowClasses; for (int i = 0; i < means.Count; i++) { means[i] = CalculateNewMean(i, rowClasses, table); } rowClasses.Clear(); for (int i = 0; i < table.Rows.Count; i++) { rowClasses.Add(GetRowClass(table.Rows[i], i, means)); } safetyCounter++; } table.Headers.Cells.Add( new Model.Cell( "Grouped" )); for (int i = 0; i < table.Rows.Count; i++) //foreach (Model.Row row in table.Rows) { table.Rows[i].Cells.Add( new Model.Cell( rowClasses[i].ClassIndex.ToString() ) ); } return(table); }