public frmMain() { InitializeComponent(); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.MaximizeBox = false; this.MinimizeBox = false; trainfile = "customer.txt"; this.lblStatus.Visible = false; //read information in customer file string line; // Read the file and display it line by line. System.IO.StreamReader file = new System.IO.StreamReader(trainfile); while ((line = file.ReadLine()) != null) { string[] split = line.Split(' '); Customer cus = new Customer(); cus.setName(split[0]); cus.setAge(Int32.Parse(split[1])); cus.setGender(Int32.Parse(split[2])); cus.setIncoming(Int32.Parse(split[3])); cus.setNumcard(Int32.Parse(split[4])); cus.setResponse(Int32.Parse(split[5])); lCustomer.Add(cus); counter = counter + 1; } file.Close(); this.lblTotal.Text = "/ " + counter; }
public Normalize( Customer cus, int maxAge, int maxIncome, int maxNumCard ) { age = (float) cus.getAge() / (float)maxAge; incoming = (float) cus.getIncoming() / (float)maxIncome; numCard = (float) cus.getNumcard() / (float)maxNumCard; }
private void btnOpen_Click(object sender, EventArgs e) { OpenFileDialog cFile = new OpenFileDialog();//open file windows cFile.Filter = "classify customer file|*.txt";//filter only open .txt file cFile.ShowDialog(); customerfile = cFile.FileName; this.txtFile.Text = cFile.FileName;//get path file //read data into unknown customerset //read information in customer file string line; // Read the file and display it line by line. System.IO.StreamReader file = new System.IO.StreamReader( customerfile ); while ((line = file.ReadLine()) != null) { string[] split = line.Split(' '); Customer cus = new Customer(); cus.setName(split[0]); cus.setAge(Int32.Parse(split[1])); cus.setGender(Int32.Parse(split[2])); cus.setIncoming(Int32.Parse(split[3])); cus.setNumcard(Int32.Parse(split[4])); cus.setResponse(-1); uCustomer.Add(cus); } file.Close(); }
private void insertRow(int index, Customer cus) { index = index + 1; string[] row = new string[7]; row[0] = index + ""; row[1] = cus.getName(); row[2] = cus.getAge() + ""; row[3] = cus.getGender() + ""; row[4] = cus.getIncoming() + ""; row[5] = cus.getNumcard() + ""; row[6] = cus.getResponse() + ""; dataGrid.Rows.Add(row); }
public void setResponse( Customer cus ) { //normalize cus Normalize ncus = new Normalize(cus, maxAge, maxIncome, maxNumCard); //calculate all distances for (int i = 0; i < this.totalTrainset; i++) { distances[i] = new Distance(); distances[i].distance = 0; distances[i].index = i; //normalize element Normalize tmp = new Normalize(this.trainset[i], maxAge, maxIncome, maxNumCard); //distance between two age normalized distances[i].distance = distances[i].distance + getDistance( ncus.age , tmp.age); //distance between two gender distances[i].distance = distances[i].distance + getDistance(cus.getGender(), this.trainset[i].getGender()); //distance between two incoming normalized distances[i].distance = distances[i].distance + getDistance(ncus.incoming, tmp.incoming); //distance between two number of card normalized distances[i].distance = distances[i].distance + getDistance( ncus.numCard, tmp.numCard); }// end loop //sort for (int i = 0; i < totalTrainset - 1; i++) { for (int j = i + 1; j < totalTrainset; j++) { if (distances[i].distance > distances[j].distance) { Distance tmp = distances[i]; distances[i] = distances[j]; distances[j] = tmp; }//swap }//end j loop }//end i loop //select k nearest neighbor int yesCount = 0; int noCount = 0; for (int i = 0; i < kNN; i++) { Customer tmp = trainset[ distances[i].index ]; if (tmp.getResponse() == 0) { noCount = noCount + 1; } else if ( tmp.getResponse() == 1 ) { yesCount = yesCount + 1; } } //set response value for unknown customer if (yesCount > noCount) { cus.setResponse(1); } else if (yesCount < noCount) { cus.setResponse(0); } }