免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4549 | 回复: 0
打印 上一主题 下一主题

k-中心点法的聚类数据挖掘算法的C#实现 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-03-19 14:17 |只看该作者 |倒序浏览
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Collections;
  5. using System.Data.Odbc;

  6. class ClusterGenerator : IDataWorker
  7. {



  8.     #region IDataWorker Members
  9.     public void work()
  10.     {
  11.         throw new Exception("The method or operation is not implemented.");
  12.     }
  13.     #endregion

  14.     private int k = 10;
  15.     public int K
  16.     {
  17.         get { return k; }
  18.         set { k = value; }
  19.     }
  20.     private ArrayList centerList = new ArrayList();

  21.     private ArrayList pointClusterList = new ArrayList();

  22.     private ArrayList cluster = new ArrayList();
  23.     private bool isChanged = false;
  24.     private int minCenter = 0;
  25.     private double minPower = 0;
  26.     private double tempPower = 1;
  27.     private double sumPower = 0;
  28.     private double randPower = 0;
  29.     private int oRandom = 0;

  30.     private void genCluster(ArrayList alldata)
  31.     {
  32.         centerList.Clear();
  33.         //初始化clusterList
  34.         Random random = new Random();
  35.         for (int i = 0; i < k; i++)
  36.         {
  37.             centerList.Add(random.Next(alldata.Count));
  38.             pointClusterList.Add(new ArrayList());
  39.         }
  40.         do
  41.         {
  42.             isChanged = false;
  43.             /////////////////////////////指派每个剩余的对象给离它最近的中心点所代表的簇
  44.             for (int i = 0; i < alldata.Count; i++)
  45.             {
  46.                 for (int j = 0; j < centerList.Count; j++)
  47.                 {
  48.                     tempPower = PowerUtil.countPower(alldata[i], alldata[(int)centerList[j]]);
  49.                     if (tempPower < minPower)
  50.                     {
  51.                         minCenter = j;
  52.                         minPower = tempPower;
  53.                     }
  54.                 }
  55.                 cluster = (ArrayList)(pointClusterList[minCenter]);
  56.                 cluster.Add(i);

  57.             }
  58.             ///////////////////////////////////////////////////
  59.             ////////////////////针对每个簇提出一个随机的非中心点对象
  60.             //for (int i = 0; i < pointClusterList; i++) {
  61.             //    cluster = (ArrayList)pointClusterList[i];
  62.             //    oRandom = random.Next(alldata.Count);
  63.             //    for (int j = 0; j < cluster; j++) {

  64.             //        sumPower += PowerUtil.countPower(alldata((int)(cluster[j])), alldata[(int)centerList[i]]);
  65.             //        randPower += PowerUtil.countPower(alldata((int)(cluster[j])), alldata(oRandom));
  66.             //    }
  67.             //    if (randPower < sumPower)
  68.             //    {
  69.             //        centerList[i] = oRandom;
  70.             //        isChanged = true;
  71.             //    }

  72.             //}
  73.             /////////////////////////
  74.             for (int i = 0; i < pointClusterList; i++)
  75.             {

  76.                 oRandom = random.Next(alldata.Count);
  77.                 if (PowerUtil.countEM(alldata, centerList, pointClusterList, oRandom, i))
  78.                 {
  79.                     centerList[i] = oRandom;
  80.                     isChanged = true;
  81.                     break;//?
  82.                 }

  83.             }

  84.             ///////////////////////////




  85.             /////////////////////////////////

  86.         } while (!isChanged);

  87.     }
  88. }
  89. using System;
  90. using System.Collections.Generic;
  91. using System.Text;
  92. using System.Collections;

  93.     class PowerUtil
  94.     {
  95.         public static double countPower(ArrayList a,ArrayList b) {
  96.             


  97.             double powerA=1.0;
  98.             double powerB=1.0;
  99.             double power=0;
  100.             int lengthA = a.Count;
  101.             int lengthB = b.Count;


  102.              for(int i=0;i<a.Count;i++){
  103.                for(int j=0;j<b.Count;j++){
  104.                    if (((string)a[i]).Equals((string)b[i]) && !((string)a[i]).Equals(""))
  105.                    {
  106.                        powerA = powerA - 1 / lengthA;
  107.                    }
  108.                    else {
  109.                    if(((string)a[i]).Equals("")){
  110.                        lengthA--;
  111.                   
  112.                    }
  113.                    }
  114.                
  115.                }
  116.             
  117.             }
  118.              for(int i=0;i<b.Count;i++){
  119.                for(int j=0;j<a.Count;j++){
  120.                    if (((string)b[i]).Equals((string)a[i]) && !((string)a[i]).Equals(""))
  121.                    {
  122.                        powerB = powerB - 1 / lengthB;
  123.                    }
  124.                    else {
  125.                        if (((string)b[i]).Equals(""))
  126.                        {
  127.                            lengthB--;
  128.                        }
  129.                   
  130.                   
  131.                    }
  132.                
  133.                }
  134.             
  135.             }
  136.             return (powerA + powerB) / 2;
  137.         
  138.         }
  139.         public static bool countEM(ArrayList alldata, ArrayList centerList, ArrayList pointClusterList,int oRandom,int position)
  140.         {
  141.               
  142.       int minCenter = 0;
  143.       double minPower = 0;
  144.       double tempPower = 1;
  145.       double sumPower = 0;
  146.       double randPower = 0;
  147.         ArrayList cluster = new ArrayList();
  148.    
  149.             ArrayList newCenterList = centerList.Clone();
  150.             newCenterList[position] = oRandom;
  151.             ArrayList newPointClusterList = new ArrayList();
  152.             for (int i = 0; i < pointClusterList.Count; i++)
  153.             {
  154.                 newPointClusterList.Add(new ArrayList());
  155.             }
  156.             for (int i = 0; i < alldata.Count; i++)
  157.             {
  158.                 for (int j = 0; j < centerList.Count; j++)
  159.                 {
  160.                     tempPower = PowerUtil.countPower(alldata[i], alldata[(int)newCenterList[j]]);
  161.                     if (tempPower < minPower)
  162.                     {
  163.                         minCenter = j;
  164.                         minPower = tempPower;
  165.                     }
  166.                 }
  167.                 cluster = (ArrayList)(newPointClusterList[minCenter]);
  168.                 cluster.Add(i);

  169.             }


  170.             //////////////////////////////
  171.             //开始算EM
  172.             for (int i = 0; i < pointClusterList; i++)
  173.             {
  174.                 cluster = (ArrayList)pointClusterList[i];
  175.               
  176.                 for (int j = 0; j < cluster; j++)
  177.                 {

  178.                     sumPower += PowerUtil.countPower(alldata((int)(cluster[j])), alldata[(int)centerList[i]]);
  179.                   
  180.                 }
  181.             

  182.             }
  183.             for (int i = 0; i < pointClusterList; i++)
  184.             {
  185.                 cluster = (ArrayList)newPointClusterList[i];

  186.                 for (int j = 0; j < cluster; j++)
  187.                 {

  188.                     randPower += PowerUtil.countPower(alldata((int)(cluster[j])), alldata[(int)newCenterList[i]]);

  189.                 }


  190.             }
  191.             double s=randPower-sumPower;
  192.             if (s < 0)
  193.             {
  194.                 return true;
  195.             }
  196.             else
  197.             {
  198.                 return false;
  199.             }

  200.         
  201.         }
  202.     }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP