资源简介
下载了其他童鞋的kmeans算法,发现有好几处bug,特此修正。分享给大家
代码片段和文件信息
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
namespace KMeansClusterConsole
{
class KMeansCluster
{
///
/// Required designer variable.
///
private static int k = 4; // 类数,此例题为2类
private static int total = 25; // 点个数
private int test = 0;
private PointF[] unknown = new PointF[total]; // 点集合
private int[] type = new int[total]; // 每个点暂时的类??
public PointF[] z = new PointF[k]; // 保存新的聚类中心
public PointF[] z0 = new PointF[k]; // 保存上一次的聚类中心
private PointF sum;
private int l = 0; // 迭代次数
public KMeansCluster()
{
// 初始化待分类的点集合
unknown[0] = new Point(0 0);
unknown[1] = new Point(1 0);
unknown[2] = new Point(0 1);
unknown[3] = new Point(1 1);
unknown[4] = new Point(2 1);
unknown[5] = new Point(1 2);
unknown[6] = new Point(2 2);
unknown[7] = new Point(3 2);
unknown[8] = new Point(6 6);
unknown[9] = new Point(7 6);
unknown[10] = new Point(8 6);
unknown[11] = new Point(6 7);
unknown[12] = new Point(7 7);
unknown[13] = new Point(8 7);
unknown[14] = new Point(9 7);
unknown[15] = new Point(7 8);
unknown[16] = new Point(8 8);
unknown[17] = new Point(9 8);
unknown[18] = new Point(8 9);
unknown[19] = new Point(9 9);
unknown[20] = new Point(15 15);
unknown[21] = new Point(13 13);
unknown[22] = new Point(14 16);
unknown[23] = new Point(12 15);
unknown[24] = new Point(17 14);
test = 0;
//选k个初始聚类中心 z[i]
for (int i = 0; i < k; i++)
{ z[i] = unknown[i]; z0[i] = unknown[i]; }//选择头两个点作为聚类中心
for (int i = 0; i < total; i++)//每个点暂时的类,初始时一个点一个类
type[i] = 0;
}
// 程序结构
public void main()
{
Console.WriteLine(“共有如下个未知样本:“);
for (int i = 0; i < total; i++)
{
Console.WriteLine(unknown[i]);
}
for (int i = 0; i < k; i++)
Console.WriteLine(“初始时,第{0}类中心{1}“ i z[i]);
while (test != k)//test代表迭代前后旧中心点跟新中心点相同的个数
// while (l != 6)
{
Console.WriteLine(“************************************“);
test = 0;
order();//进行迭代,对total个样本根据聚类中心进行分类
for (int i = 0; i < k; i++)
{
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 8192 2018-04-11 16:56 KMeans\KMeansClusterConsole\bin\Debug\KMeansClusterConsole.exe
文件 19968 2018-04-11 16:56 KMeans\KMeansClusterConsole\bin\Debug\KMeansClusterConsole.pdb
文件 11608 2018-04-11 09:07 KMeans\KMeansClusterConsole\bin\Debug\KMeansClusterConsole.vshost.exe
文件 6609 2018-04-11 17:01 KMeans\KMeansClusterConsole\KMeansCluster.cs
文件 2855 2018-04-11 09:04 KMeans\KMeansClusterConsole\KMeansClusterConsole.csproj
文件 5893 2018-04-11 16:56 KMeans\KMeansClusterConsole\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
文件 1082 2018-04-11 15:16 KMeans\KMeansClusterConsole\obj\Debug\KMeansClusterConsole.csproj.FileListAbsolute.txt
文件 8192 2018-04-11 16:56 KMeans\KMeansClusterConsole\obj\Debug\KMeansClusterConsole.exe
文件 19968 2018-04-11 16:56 KMeans\KMeansClusterConsole\obj\Debug\KMeansClusterConsole.pdb
文件 332 2010-01-05 10:26 KMeans\KMeansClusterConsole\Program.cs
文件 1476 2010-01-05 10:09 KMeans\KMeansClusterConsole\Properties\AssemblyInfo.cs
文件 950 2018-04-11 09:04 KMeans\KMeansClusterConsole.sln
..A..H. 19968 2018-04-11 17:01 KMeans\KMeansClusterConsole.suo
文件 1180 2018-04-11 09:04 KMeans\UpgradeLog.xm
文件 3348 2018-04-11 09:04 KMeans\_UpgradeReport_Files\UpgradeReport.css
文件 12505 2010-05-04 01:19 KMeans\_UpgradeReport_Files\UpgradeReport.xslt
文件 69 2018-04-11 09:04 KMeans\_UpgradeReport_Files\UpgradeReport_Minus.gif
文件 71 2018-04-11 09:04 KMeans\_UpgradeReport_Files\UpgradeReport_Plus.gif
目录 0 2010-01-05 10:09 KMeans\KMeansClusterConsole\obj\Debug\TempPE
目录 0 2018-04-11 15:10 KMeans\KMeansClusterConsole\bin\Debug
目录 0 2018-04-11 09:04 KMeans\KMeansClusterConsole\bin\Release
目录 0 2018-04-11 16:56 KMeans\KMeansClusterConsole\obj\Debug
目录 0 2018-04-11 09:04 KMeans\KMeansClusterConsole\bin
目录 0 2010-01-05 10:09 KMeans\KMeansClusterConsole\obj
目录 0 2010-01-05 10:09 KMeans\KMeansClusterConsole\Properties
目录 0 2010-01-06 10:51 KMeans\KMeansClusterConsole
目录 0 2018-04-11 09:04 KMeans\_UpgradeReport_Files
目录 0 2018-04-11 09:04 KMeans
----------- --------- ---------- ----- ----
124266 28
............此处省略1个文件信息
- 上一篇:C#获取本地计算机的硬件信息源代码
- 下一篇:C#实现微信企业号签到功能
评论
共有 条评论