赞
踩
算法介绍:
本算法是用来在一堆数据中分离出几类数据,比如一堆钞票,将其颜色、尺寸等数据采集,通过分析数据来区分出100元、50元、20元等。主要方法是假设该数据集是有C个类,此时可以在这堆数据中任选10个数据作为初始聚类中心,计算其他数据到这些点的距离的大小,按最小距离将其归为一类。通过不断的迭代,计算选取新的聚类中心,再计算新的距离,按最小距离划分,直到聚类中心的值不再发生变化。
本次所要分离的数据是wine数据集,已知有一堆酒,这些酒来自三个不同的地方,也已经直到这些酒的特征:口味、色度,含糖量等,共有13种特征,现将这些特征采集为数据,每一条数据代表一瓶酒,来区分这些酒的产地。
下图是酒的数据:共有178行数据,也就是178瓶酒,每一行有13个特征数据(第一个1是矫正用的分类数据)
具体程序:
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WineIde { class Program { static void Main(string[] args) { List<string[]> WineData = new List<string[]>(); WineData = ReadCsv(@"C:\Users\user\Desktop\wine.txt"); List<double> FirstClass = new List<double>(); List<double> SecondClass = new List<double>(); List<double> ThirdClass = new List<double>(); double[,] NumData = new double[WineData.Count, 13]; for (int i = 0; i < WineData.Count; i++) { for (int j = 1; j < 14; j++) { NumData[i, j-1] = Double.Parse(WineData[i][j]); } } //FirstClass.Add(0); //SecondClass.Add(59); //ThirdClass.Add(130); // for (int m = 0; m < 4; m++) //{ for (int i = 0; i < WineData.Count; i++) { double First = 0; double Second = 0; double Third = 0; for (int j = 0; j < 1; j++) { First += (NumData[i, j] - NumData[0, j]) * (NumData[i, j] - NumData[0, j]); Second += (NumData[i, j] - NumData[59, j]) * (NumData[i, j] - NumData[59, j]); Third += (NumData[i, j] - NumData[130, j]) * (NumData[i, j] - NumData[130, j]); } if (First < Second && First < Third) { FirstClass.Add(i); } if (First > Second && Second < Third) { SecondClass.Add(i); } if (First > Third && Second >Third) { ThirdClass.Add(i); } } for (int A = 0; A < 4; A++) { double[,] FirstClass1 = new double[1, 13]; double[,] FirstClass2 = new double[1, 13]; double[,] FirstClass3 = new double[1, 13]; foreach (int i in FirstClass) { for (int k = 0; k < 13; k++) { FirstClass1[0, k] = (FirstClass1[0, k] + NumData[i, k]); } } for (int i = 0; i < 13; i++) { FirstClass1[0, i] = FirstClass1[0, i] / FirstClass.Count; } foreach (int i in SecondClass) { for (int k = 0; k < 13; k++) { FirstClass2[0, k] = (FirstClass2[0, k] + NumData[i, k]); } } for (int i = 0; i < 13; i++) { FirstClass2[0, i] = FirstClass2[0, i] / SecondClass.Count; } foreach (int i in ThirdClass) { for (int k = 0; k < 13; k++) { FirstClass3[0, k] = (FirstClass3[0, k] + NumData[i, k]); } } for (int i = 0; i < 13; i++) { FirstClass3[0, i] = FirstClass3[0, i] / ThirdClass.Count; } FirstClass.Clear(); SecondClass.Clear(); ThirdClass.Clear(); for (int i = 0; i < WineData.Count; i++) { double First = 0; double Second = 0; double Third = 0; for (int j = 0; j < 13; j++) { First += (NumData[i, j] - FirstClass1[0, j]) * (NumData[i, j] - FirstClass1[0, j]); Second += (NumData[i, j] - FirstClass2[0, j]) * (NumData[i, j] - FirstClass2[0, j]); Third += (NumData[i, j] - FirstClass3[0, j]) * (NumData[i, j] - FirstClass3[0, j]); } if (First < Second && First < Third) { FirstClass.Add(i); } if (First > Second && Second < Third) { SecondClass.Add(i); } if (First > Third && Second > Third) { ThirdClass.Add(i); } } } Console.WriteLine("第一类数据:"); for (int i = 0; i < FirstClass.Count; i++) { Console.Write("{0} ", FirstClass[i]); } Console.WriteLine(); Console.WriteLine("第二类数据:"); for (int i = 0; i < SecondClass.Count; i++) { Console.Write("{0} ", SecondClass[i]); } Console.WriteLine(); Console.WriteLine("第三类数据:"); for (int i = 0; i < ThirdClass.Count; i++) { Console.Write("{0} ", ThirdClass[i]); } int num = FirstClass.Count + SecondClass.Count + ThirdClass.Count; Console.WriteLine("总数是:{0}", num); } public static List<string[]> ReadCsv(string PathName) { FileStream fs = new FileStream(PathName, FileMode.Open, FileAccess.Read); StreamReader ReadFile = new StreamReader(fs, Encoding.Default); string strRead = ""; List<string[]> lsFile = new List<string[]>(); while (strRead!=null) { strRead = ReadFile.ReadLine(); if (strRead!=null&&strRead.Length>0) { lsFile.Add(strRead.Split(',')); } } ReadFile.Close(); return lsFile; } } }
结果:
准确率还有待提高。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。