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
{publicpartialclassForm1:Form{privatestring filePath =@"C:\Users\Duy Nguyen\Downloads\DTB_IT.xlsx";publicForm1(){InitializeComponent();}privatevoidbutton1_Click(object sender,EventArgs e){FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);IExcelDataReader excelReader;//1. Reading Excel fileif(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.TablesDataSet result = excelReader.AsDataSet(newExcelDataSetConfiguration(){// 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)=>newExcelDataTableConfiguration(){// 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<double[]> data =newList<double[]>();foreach(DataRow dtRow in dt.Rows){
List<double> row =newList<double>();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 =newKMeans(k:2);//Hoc huan luyenvar 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<Int32>("Cluster")==0).CopyToDataTable();DataTable dt_lb2 = dt.AsEnumerable().Where(row => row.Field<Int32>("Cluster")==1).CopyToDataTable();
dataGridView2.DataSource = dt_lb1;
dataGridView3.DataSource = dt_lb2;}}}