📄 form1.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
namespace kNN
{
public partial class Form1 : Form
{
private DataTable table = new DataTable();
private DataTable teachingTable = new DataTable();
private DataTable testTable = new DataTable();
private DataTable classifiedTable = new DataTable();
private ArrayList TargetAttr = new ArrayList();
private ArrayList trainingSamples = new ArrayList();
private ArrayList testSamples = new ArrayList();
private ArrayList answers = new ArrayList();
public Form1()
{
InitializeComponent();
}
public void OpenSample()
{
this.table.Clear();
this.table.Rows.Clear();
this.table.Columns.Clear();
this.comboBox1.Items.Clear();
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
StreamReader reader = new StreamReader(openFileDialog1.FileName);
string[] attributes = reader.ReadLine().Split(',');
DataRow row = this.table.NewRow();
for (int i = 0; i < attributes.Length; i++)
//foreach (string value in attributes)
{
this.table.Columns.Add();
this.teachingTable.Columns.Add();
this.testTable.Columns.Add();
row[i] = attributes[i];
this.table.Columns[i].ColumnName = ("Attribute" + (i + 1).ToString());
}
// default target attribute is the last one
int j = 0;
foreach (string value in attributes)
{
comboBox1.Items.Add("Attribute " + (++j).ToString());
}
comboBox1.SelectedIndex = comboBox1.Items.Count - 1;
this.table.Rows.Add(row);
string line;
while ((line = reader.ReadLine()) != null)
{
row = this.table.NewRow();
string[] sample = line.Split(',');
for (int i = 0; i < sample.Length; i++)
{
row[i] = sample[i];
}
this.table.Rows.Add(row);
}
}
}
public void InitializeTarget()
{
for (int i = 0; i < this.table.Rows.Count; i++)
{
this.TargetAttr.Add(this.table.Rows[i].ItemArray[comboBox1.SelectedIndex]);
}
}
public void DevideSample()
{
double n = 0;
switch (comboBox2.SelectedIndex)
{
case 0:
n = 0.9;
break;
case 1:
n = 0.8;
break;
case 2:
n = 0.7;
break;
case 3:
n = 0.6;
break;
case 4:
n = 0.5;
break;
case 5:
n = 0.4;
break;
case 6:
n = 0.3;
break;
case 7:
n = 0.2;
break;
case 8:
n = 0.1;
break;
default:
n=0.9;
break;
}
//printing teaching sample
int num = Convert.ToInt32(this.table.Rows.Count * n);
this.teachingTable = this.table.Clone();
for (int i = 0; i < num; i++)
{
this.teachingTable.ImportRow(this.table.Rows[i]);
}
dataGridView1.DataSource = this.teachingTable;
//initializing training sample
foreach (DataRow row in this.teachingTable.Rows)
{
ArrayList itemsArray = new ArrayList();
{
foreach (string value in row.ItemArray)
{
itemsArray.Add(value);
}
this.trainingSamples.Add(new Sample(itemsArray, comboBox1.SelectedIndex));
}
}
//printing test sample
this.testTable = this.table.Clone();
for (int i = 0; i < this.table.Rows.Count - num; i++)
{
this.testTable.ImportRow(this.table.Rows[this.table.Rows.Count-1-i]);
}
foreach (DataRow row in this.testTable.Rows)
{
answers.Add(row[comboBox1.SelectedIndex]);
row[comboBox1.SelectedIndex] = "?";
}
dataGridView2.DataSource = this.testTable;
//initializing test sample
foreach (DataRow row in this.testTable.Rows)
{
ArrayList itemsArray = new ArrayList();
foreach (string value in row.ItemArray)
{
itemsArray.Add(value);
}
this.testSamples.Add(new Sample(itemsArray, comboBox1.SelectedIndex));
}
}
public ArrayList GetClosestNeighbours(int k, int j)
{
ArrayList tempp = new ArrayList();
ArrayList closestNeighbours = new ArrayList();
double eDistance = 100;
{
ArrayList TempSample = this.trainingSamples.Clone() as ArrayList;
for (int i = k; i > 0; i--)
{
ArrayList neighbour = new ArrayList();
foreach (Sample trainingSample in TempSample)
{
double temp = (this.testSamples[j]as Sample).CountDistance(trainingSample);
if (temp < eDistance)
{
eDistance = temp;
ArrayList distance = new ArrayList();
distance.Add(trainingSample.GetClass());
distance.Add(eDistance);
neighbour.Clear();
neighbour.Add(distance);
}
}
tempp.Add((neighbour[0]as ArrayList)[0]);
closestNeighbours.Add(neighbour);
TempSample.Remove((neighbour[0] as ArrayList)[0]);
eDistance = 100;
}
}
return tempp;
}
public string GetClass(ArrayList closestNeighbours)//:)
{
ArrayList goal = new ArrayList();
ArrayList targetValues = new ArrayList();
foreach (string value in this.TargetAttr)
{
if (!targetValues.Contains(value))
targetValues.Add(value);
}
foreach (string value in targetValues)
{
ArrayList temp1 = new ArrayList();
goal.Add(temp1);
}
foreach (string class_ in closestNeighbours)
{
for (int i = 0; i < targetValues.Count; i++)
{
if (class_.Equals(targetValues[i]))
(goal[i] as ArrayList).Add(1);
}
}
int a =0;
for (int i = 0; i < goal.Count; i++)
{
if ((goal[i] as ArrayList).Count > a)
a = i;
}
return targetValues[a].ToString();
}
public void Randomize()
{
Random rnd = new Random();
int count = this.table.Rows.Count;
for (int i = 0; i < 4*count; i++)
{
DataRow row = table.NewRow();
int rownum = rnd.Next(0, count - 1);
row.ItemArray = table.Rows[rownum].ItemArray;
table.Rows.RemoveAt(rownum);
table.Rows.Add(row);
}
}
public void Classify()
{
this.classifiedTable = this.testTable.Clone();
for (int i = 0; i < this.testTable.Rows.Count; i++)
{
this.classifiedTable.ImportRow(this.testTable.Rows[i]);
}
for (int i = 0; i < this.testSamples.Count; i++)
{
ArrayList closestNeighbours = GetClosestNeighbours(Convert.ToInt32(textBox1.Text), i);
this.classifiedTable.Rows[i][this.classifiedTable.Rows[0].ItemArray.Length - 1] = GetClass(closestNeighbours);
}
}
public void GetAccuracy()
{
int match = 0;
for (int i = 0; i < this.classifiedTable.Rows.Count; i++)
{
string w = this.classifiedTable.Rows[i][this.classifiedTable.Rows[0].ItemArray.Length - 1].ToString();
if (this.classifiedTable.Rows[i][this.classifiedTable.Rows[0].ItemArray.Length - 1].Equals(answers[i]))
match++;
}
double accuracy = Convert.ToDouble(match) / Convert.ToDouble(this.classifiedTable.Rows.Count);
label1.Text = "Точность классификатора = " + (accuracy * 100).ToString() + "%";
label1.Visible = true;
}
public void ShowSamples()
{
dataGridView1.DataSource = this.teachingTable;
dataGridView2.DataSource = this.testTable;
}
private void button1_Click(object sender, EventArgs e)
{
OpenSample();
InitializeTarget();
dataGridView1.DataSource = this.table;
button4.Enabled = true;
groupBox1.Enabled = true;
groupBox2.Enabled = true;
button3.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
Classify();
label1.Visible = true;
dataGridView2.DataSource = this.classifiedTable;
GetAccuracy();
}
private void button3_Click(object sender, EventArgs e)
{
this.testSamples.Clear();
this.trainingSamples.Clear();
this.answers.Clear();
this.teachingTable.Clear();
this.teachingTable.Rows.Clear();
this.teachingTable.Columns.Clear();
this.testTable.Clear();
this.testTable.Rows.Clear();
this.testTable.Columns.Clear();
DevideSample();
ShowSamples();
button2.Enabled = true;
groupBox3.Enabled = true;
this.classifiedTable.Clear();
this.classifiedTable.Rows.Clear();
this.classifiedTable.Columns.Clear();
}
private void button4_Click(object sender, EventArgs e)
{
Randomize();
dataGridView1.DataSource = this.table;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -