using Accord.MachineLearning; using ExcelDataReader; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace MachineLearning { public partial class Form1 : Form { private string filePath = @"C:\Users\Duy Nguyen\Downloads\DTB_IT.xlsx"; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read); IExcelDataReader excelReader; //1. Reading Excel file if (Path.GetExtension(filePath).ToUpper() == ".XLS") { //1.1 Reading from a binary Excel file ('97-2003 format; *.xls) excelReader = ExcelReaderFactory.CreateBinaryReader(stream); } else { //1.2 Reading from a OpenXml Excel file (2007 format; *.xlsx) excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); } //2. DataSet - The result of each spreadsheet will be created in the result.Tables DataSet result = excelReader.AsDataSet(new ExcelDataSetConfiguration() { // Gets or sets a value indicating whether to set the DataColumn.DataType // property in a second pass. UseColumnDataType = true, // Gets or sets a callback to determine whether to include the current sheet // in the DataSet. Called once per sheet before ConfigureDataTable. FilterSheet = (tableReader, sheetIndex) => true, // Gets or sets a callback to obtain configuration options for a DataTable. ConfigureDataTable = (tableReader) => new ExcelDataTableConfiguration() { // Gets or sets a value indicating the prefix of generated column names. EmptyColumnNamePrefix = "Column", // Gets or sets a value indicating whether to use a row from the // data as column names. UseHeaderRow = true, } }); DataTable dt = result.Tables[0]; List data = new List(); foreach (DataRow dtRow in dt.Rows) { List row = new List(); for(int i = 4; i <= 12; i++) { double val = Double.Parse(dtRow[i].ToString()); row.Add(val); } data.Add(row.ToArray()); } double[][] input = data.ToArray(); KMeans kmeans = new KMeans(k: 2); //Hoc huan luyen var clusters = kmeans.Learn(input); // int[] labels = clusters.Decide(input); dt.Columns.Add("Cluster", typeof(System.Int32)); foreach (DataRow dtRow in dt.Rows) { int index = dt.Rows.IndexOf(dtRow); dtRow["Cluster"] = labels[index]; } dataGridView1.DataSource = dt; DataTable dt_lb1 = dt.AsEnumerable().Where(row => row.Field("Cluster") == 0).CopyToDataTable(); DataTable dt_lb2 = dt.AsEnumerable().Where(row => row.Field("Cluster") == 1).CopyToDataTable(); dataGridView2.DataSource = dt_lb1; dataGridView3.DataSource = dt_lb2; } } }