public static TableData LoadDataFromfCSV(string fileName, string delimeter = null, bool hasHeaderRecord = true, bool ignoreQuotes = true, int[] columnIndexes = null, int classIndex = -1) { var configuration = new CsvConfiguration(); configuration.Delimiter = delimeter ?? "\t"; configuration.HasExcelSeparator = false; configuration.IgnoreQuotes = ignoreQuotes; configuration.HasHeaderRecord = hasHeaderRecord; configuration.QuoteNoFields = true; using (var reader = new CsvReader(new StreamReader(fileName), configuration)) { var data = new TableData(); var index = 0; while (reader.Read()) { if (index == 0) { var noOfAttributes = hasHeaderRecord ? reader.FieldHeaders.Length : reader.CurrentRecord.Length; if (columnIndexes == null) { columnIndexes = new int[noOfAttributes]; for (var j = 0; j < columnIndexes.Length; j++) { columnIndexes[j] = j; } } for (int column = 0; column < columnIndexes.Length; column++) { var columnName = column == classIndex ? "Class" : hasHeaderRecord ? reader.FieldHeaders[columnIndexes[column]] : "Column" + column; data.AddAttribute(columnName); } index++; } var row = data.NewRow(); var attributes = data.Attributes.ToArray(); for (var columnIndex = 0; columnIndex < columnIndexes.Length; columnIndex++) { var columnName = attributes[columnIndex]; row[columnName] = reader.GetField(columnIndexes[columnIndex]); } data.AddRow(row); } return data; } }
public static TableData LoadDataFromfCSV(string fileName, string delimeter = null, bool hasHeaderRecord = true, bool ignoreQuotes = true, int[] columnIndexes = null, int classIndex = -1) { var configuration = new CsvConfiguration(); configuration.Delimiter = delimeter ?? "\t"; configuration.HasExcelSeparator = false; configuration.IgnoreQuotes = ignoreQuotes; configuration.HasHeaderRecord = hasHeaderRecord; configuration.QuoteNoFields = true; using (var reader = new CsvReader(new StreamReader(fileName), configuration)) { var data = new TableData(); var index = 0; while (reader.Read()) { if (index == 0) { var noOfAttributes = hasHeaderRecord ? reader.FieldHeaders.Length : reader.CurrentRecord.Length; if (columnIndexes == null) { columnIndexes = new int[noOfAttributes]; for (var j = 0; j < columnIndexes.Length; j++) { columnIndexes[j] = j; } } for (int column = 0; column < columnIndexes.Length; column++) { var columnName = column == classIndex ? "Class" : hasHeaderRecord ? reader.FieldHeaders[columnIndexes[column]] : "Column" + column; data.AddAttribute(columnName); } index++; } var row = data.NewRow(); var attributes = data.Attributes.ToArray(); for (var columnIndex = 0; columnIndex < columnIndexes.Length; columnIndex++) { var columnName = attributes[columnIndex]; row[columnName] = reader.GetField(columnIndexes[columnIndex]); } data.AddRow(row); } return(data); } }
private static void Test() { var data = new TableData(); Random random = new Random(321); const int rows = 50; const int columns = 3; var matrix = new int[rows, columns]; for (int currentColumn = 0; currentColumn < columns; currentColumn++) { data.AddAttribute("attribute" + (currentColumn + 1)); } for (var currentRow = 0; currentRow < rows; currentRow++) { var row = data.NewRow(); for (var currentColumn = 0; currentColumn < columns; currentColumn++) { var attribute = "attribute" + (currentColumn + 1); matrix[currentRow, currentColumn] = random.Next(); row[attribute] = matrix[currentRow, currentColumn]; } data.AddRow(row); } for (var currentRow = 0; currentRow < rows; currentRow++) { for (var currentColumn = 0; currentColumn < columns; currentColumn++) { var attribute = "attribute" + (currentColumn + 1); //Convert.ChangeType() //var matrix[currentRow, currentColumn] //data[currentRow][attribute] if (matrix[currentRow, currentColumn] != (int)data[currentRow][attribute]) { throw new Exception("InvalidData"); } } } }
public static TableData LoadDataFromfCSV(string fileName) { var configuration = new CsvHelper.Configuration.CsvConfiguration(); configuration.Delimiter = "\t"; configuration.HasExcelSeparator = false; configuration.IgnoreQuotes = true; configuration.QuoteNoFields = true; using (var reader = new CsvReader(new StreamReader(fileName), configuration)) { var data = new TableData(); var index = 0; string[] headers = null; while (reader.Read()) { if (index == 0) { headers = reader.FieldHeaders; for (var columnIndex = 0; columnIndex < headers.Length; columnIndex++) { var columnName = headers[columnIndex]; data.AddAttribute(columnName); } index++; continue; } var row = data.NewRow(); for (var columnIndex = 0; columnIndex < headers.Length; columnIndex++) { var columnName = headers[columnIndex]; row[columnName] = reader.GetField(columnIndex); } data.AddRow(row); } return data; } }
private static void Test() { var data = new TableData(); Random random = new Random(321); const int rows = 50; const int columns = 3; var matrix = new int[rows, columns]; for (int currentColumn = 0; currentColumn < columns; currentColumn++) { data.AddAttribute("attribute" + (currentColumn + 1)); } for (var currentRow = 0; currentRow < rows; currentRow++) { var row = data.NewRow(); for (var currentColumn = 0; currentColumn < columns; currentColumn++) { var attribute = "attribute" + (currentColumn + 1); matrix[currentRow, currentColumn] = random.Next(); row[attribute] = matrix[currentRow, currentColumn]; } data.AddRow(row); } for (var currentRow = 0; currentRow < rows; currentRow++) { for (var currentColumn = 0; currentColumn < columns; currentColumn++) { var attribute = "attribute" + (currentColumn + 1); //Convert.ChangeType() //var matrix[currentRow, currentColumn] //data[currentRow][attribute] if (matrix[currentRow, currentColumn] != (int)data[currentRow][attribute]) { throw new Exception("InvalidData"); } } } }