资源简介

用VC2008实现的C#源码,控制台程序,举了一个小例子,用于理解K-means聚类算法的核心思想,适用于初学者。

资源截图

代码片段和文件信息

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 = 2;                       // 类数,此例题为2类
        private static int total = 20;                  // 点个数
        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);

            test = 0;
            //选k个初始聚类中心    z[i]     
            for (int i = 0; i < k; i++)
                z[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)
            {
                order();
                for (int i = 0; i < k; i++)
                {
                    z[i] = newCenter(i);
                    Console.WriteLine(“第{0}类新中心{1}“ i z[i]);
                    if (compare(z[i] z0[i]))
                        test = test + 1;
                    else
                        z0[i] = z[i];
                }
                l = l + 1;
                Console.WriteLine(“******已完成第{0}次迭代*******“ l);
                Console.Write(“{0}“ “分类后:“);
                for (int j = 0; j < k; j++)
                {
              

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       7680  2010-01-05 10:26  KMeansClusterConsole\KMeansClusterConsole\bin\Debug\KMeansClusterConsole.exe

     文件      15872  2010-01-05 10:26  KMeansClusterConsole\KMeansClusterConsole\bin\Debug\KMeansClusterConsole.pdb

     文件      14328  2010-01-05 10:28  KMeansClusterConsole\KMeansClusterConsole\bin\Debug\KMeansClusterConsole.vshost.exe

     文件        490  2007-07-21 01:33  KMeansClusterConsole\KMeansClusterConsole\bin\Debug\KMeansClusterConsole.vshost.exe.manifest

     文件       5122  2010-01-06 10:51  KMeansClusterConsole\KMeansClusterConsole\KMeansCluster.cs

     文件       2601  2010-01-05 10:26  KMeansClusterConsole\KMeansClusterConsole\KMeansClusterConsole.csproj

     文件        646  2010-01-05 10:28  KMeansClusterConsole\KMeansClusterConsole\obj\Debug\KMeansClusterConsole.csproj.FileListAbsolute.txt

     文件       7680  2010-01-05 10:26  KMeansClusterConsole\KMeansClusterConsole\obj\Debug\KMeansClusterConsole.exe

     文件      15872  2010-01-05 10:26  KMeansClusterConsole\KMeansClusterConsole\obj\Debug\KMeansClusterConsole.pdb

     文件        332  2010-01-05 10:26  KMeansClusterConsole\KMeansClusterConsole\Program.cs

     文件       1476  2010-01-05 10:09  KMeansClusterConsole\KMeansClusterConsole\Properties\AssemblyInfo.cs

     文件        950  2010-01-05 10:09  KMeansClusterConsole\KMeansClusterConsole.sln

    ..A..H.     13312  2010-01-06 10:51  KMeansClusterConsole\KMeansClusterConsole.suo

     目录          0  2010-01-05 10:09  KMeansClusterConsole\KMeansClusterConsole\obj\Debug\TempPE

     目录          0  2010-01-05 10:26  KMeansClusterConsole\KMeansClusterConsole\bin\Debug

     目录          0  2010-01-05 10:26  KMeansClusterConsole\KMeansClusterConsole\obj\Debug

     目录          0  2010-01-05 10:09  KMeansClusterConsole\KMeansClusterConsole\bin

     目录          0  2010-01-05 10:09  KMeansClusterConsole\KMeansClusterConsole\obj

     目录          0  2010-01-05 10:09  KMeansClusterConsole\KMeansClusterConsole\Properties

     目录          0  2010-01-06 10:51  KMeansClusterConsole\KMeansClusterConsole

     目录          0  2010-01-05 10:09  KMeansClusterConsole

----------- ---------  ---------- -----  ----

                86361                    21


评论

共有 条评论