KNN(k-nearest-neighbor)算法的思想是找到在输入新数据时,找到与该数据最接近的k个邻居,在这k个邻居中,找到出现次数最多的类别,对其进行归类。
Iris数据集是常用的分类实验数据集,由Fisher, 1936收集整理。Iris也称鸢尾花卉数据集,是一类多重变量分析的数据集。数据集包含150个数据集,分为3类,每类50个数据,每个数据包含4个属性。可通过花萼长度,花萼宽度,花瓣长度,花瓣宽度4个属性预测鸢尾花卉属于(Setosa,Versicolour,Virginica)三个种类中的哪一类。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
public class Iris
{
//
private double sepalLength;
public double SepalLength
{
get { return sepalLength; }
set { sepalLength = value; }
}
//
private double sepalWidth;
public double SepalWidth
{
get { return sepalWidth; }
set { sepalWidth = value; }
}
//
private double petalLength;
public double PetalLength
{
get { return petalLength; }
set { petalLength = value; }
}
//
private double petalWidth;
public double PetalWidth
{
get { return petalLength; }
set { petalLength = value; }
}
//
private string species;
public string Species
{
get { return species; }
set { species = value; }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
public class KNN
{
/// <summary>
/// 样本数据
/// </summary>
private List<Iris> sampleList;
/// <summary>
/// 未分类数据
/// </summary>
private List<Iris> unclassifyList;
/// <summary>
/// K值
/// </summary>
private int k;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="sampleList">样本数据</param>
/// <param name="unclassifyList">未分类数据</param>
/// <param name="k">k值</param>
public KNN(List<Iris> sampleList, List<Iris> unclassifyList, int k)
{
this.sampleList = sampleList;
this.unclassifyList = unclassifyList;
this.k = k;
}
/// <summary>
/// 分类
/// </summary>
public void Classify()
{
int sampleCount = sampleList.Count;
int unclassifyCount = unclassifyList.Count;
//
for (int i = 0; i < unclassifyCount; i++)
{
Tuple<string, double>[] tupleArray = new Tuple<string, double>[sampleCount];
for (int j = 0; j < sampleCount; j++)
{
double distance = CalculateDistance(sampleList[j], unclassifyList[i]);
string species = sampleList[j].Species;
tupleArray[j] = Tuple.Create(species, distance);
}
//
IEnumerable<Tuple<string, double>> selector = tupleArray.OrderBy(t => t.Item2).Take(k);
Dictionary<string, int> dictionary = new Dictionary<string, int>();
foreach (Tuple<string, double> tuple in selector)
{
if (dictionary.ContainsKey(tuple.Item1))
{
dictionary[tuple.Item1]++;
}
else
{
dictionary.Add(tuple.Item1, 1);
}
}
//
IEnumerable<KeyValuePair<string, int>> keyValuePair = dictionary.OrderByDescending(t => t.Value).Take(1);
foreach (KeyValuePair<string, int> kvp in keyValuePair)
{
unclassifyList[i].Species = kvp.Key;
}
//
sampleList.Add(unclassifyList[i]);
sampleCount++;
}
}
/// <summary>
/// 计算距离
/// </summary>
/// <param name="sample">样本数据</param>
/// <param name="unclassify">未分类数据</param>
/// <returns>两者欧氏距离</returns>
public double CalculateDistance(Iris sample, Iris unclassify)
{
double delta_SepalLength = unclassify.SepalLength - sample.SepalLength;
double delta_SepalWidth = unclassify.SepalWidth - sample.SepalWidth;
double delta_PetalLength = unclassify.PetalLength - sample.PetalLength;
double delta_PetalWidth = unclassify.PetalWidth - sample.PetalWidth;
return Math.Sqrt(delta_SepalLength * delta_SepalLength + delta_SepalWidth * delta_SepalWidth + delta_PetalLength * delta_PetalLength + delta_PetalWidth * delta_PetalWidth);
}
/// <summary>
/// 打印
/// </summary>
public void Print(string filePath)
{
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < sampleList.Count; i++)
{
Iris iris = sampleList[i];
stringBuilder.AppendLine(i.ToString() + "\t" + iris.SepalLength.ToString() + "\t" + iris.SepalWidth.ToString() + "\t" + iris.PetalLength.ToString() + "\t" + iris.PetalWidth.ToString() + "\t" + iris.Species);
}
System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Create);
System.IO.StreamWriter sw = new System.IO.StreamWriter(fs);
sw.Write(stringBuilder.ToString());
sw.Flush();
sw.Close();
fs.Close();
fs.Dispose();
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
List<Iris> sampleList = GetIrisDataset(AppDomain.CurrentDomain.BaseDirectory + "样本.txt");
List<Iris> unclassifyList = GetIrisDataset(AppDomain.CurrentDomain.BaseDirectory + "未分类.txt");
KNN tool = new KNN(sampleList, unclassifyList, 5);
tool.Classify();
tool.Print(@"C:\Users\DSF\Desktop\t.txt");
Console.WriteLine("OK");
}
static List<Iris> GetIrisDataset(string filePath)
{
System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Open);
System.IO.StreamReader sr = new System.IO.StreamReader(fs);
//
List<Iris> list = new List<Iris>();
string readLine = sr.ReadLine();
while (!string.IsNullOrEmpty(readLine))
{
string[] splitArray = readLine.Split(' ');
Iris iris = new Iris();
iris.SepalLength = Convert.ToDouble(splitArray[1]);
iris.SepalWidth = Convert.ToDouble(splitArray[2]);
iris.PetalLength = Convert.ToDouble(splitArray[3]);
iris.PetalWidth = Convert.ToDouble(splitArray[4]);
iris.Species = splitArray[5];
list.Add(iris);
readLine = sr.ReadLine();
}
//
sr.Close();
fs.Close();
fs.Dispose();
return list;
}
}
}