// This function uses a Question to partition a Dataset // into different sub-datasets for the true and falso branch. public static (Dataset trueRows, Dataset falseRows) PartitionDataset(Dataset dataset, Question question) { // We must use the same Headers, that's why we copy them over // to the new Dataset Dataset trueRows = new Dataset(dataset.Headers); Dataset falseRows = new Dataset(dataset.Headers); foreach (Row row in dataset) { if (question.Match(row)) { trueRows.Add(row); } else { falseRows.Add(row); } } return(trueRows, falseRows); }
private void fileSelectBtn_Click(object sender, EventArgs e) { if (csvFileLoader.ShowDialog() == DialogResult.OK) { // Force disable the CheckBox useHeaderCheckbox.Enabled = false; useHeaderCheckbox.Checked = false; // Force disable Buttons startTrainingBtn.Enabled = false; showTreeBtn.Enabled = false; predictionsBtn.Enabled = false; // Clear any previous training data tree = null; trainingData = new Dataset(); CsvConfiguration config = new CsvConfiguration(CultureInfo.InvariantCulture) { HasHeaderRecord = false }; // Wrap all this code in a try-catch block to catch parsing errors try { // Read the contents of the file into a stream using (Stream fileStream = csvFileLoader.OpenFile()) using (StreamReader reader = new StreamReader(fileStream)) using (CsvReader csv = new CsvReader(reader, config)) { int maxFeatureCount = 0; // Read every line individually while (csv.Read()) { // Parse the current record var record = csv.GetRecord <dynamic>(); Row featureRow = Row.ParseFromCsv(record); if (featureRow != null) { trainingData.Add(featureRow); if (featureRow.Count > maxFeatureCount) { maxFeatureCount = featureRow.Count; } } } // Add default headers for (int i = 0; i < maxFeatureCount; ++i) { string header = $"Field{i + 1}"; trainingData.Headers.Add(header); } } // Get the path of the specified file csvFilePath = csvFileLoader.SafeFileName; fileNameLabel.Text = $"Seleccionado: {csvFilePath}"; // Display the fields in the UI CreateFieldsFromDataset(fieldsPanel, trainingData); // Reset the CheckBox state to enabled useHeaderCheckbox.Enabled = true; // Re-enable the training button startTrainingBtn.Enabled = true; } catch (Exception) { // This will catch any type of Exception (for now) // Clear any fields that were added fieldsPanel.Controls.Clear(); // Clear any changes made to trainingData trainingData.Clear(); // Inform the user parsing has failed string title = "¡Uh-oh!"; string message = "Ha sucedido un problema al intentar leer el archivo CSV. Revisa que este tenga una sintaxis válida."; _ = MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Error); } } }