k均值算法c++語言實現(xiàn)代碼
//k-mean.h
#ifndef KMEAN_HEAD
#define KMEAN_HEAD
 #include <vector>
 #include <map>
 //空間點的定義
 class Node
 {
     public:
        double pos_x;
        double pos_y;
        double pos_z;
      Node()
      {
          pos_x = 0.0;
          pos_y = 0.0;
          pos_z = 0.0;
      }
      Node(double x,double y,double z)
      {
          pos_x = x;
          pos_y = y;
          pos_z = z;
      }
       friend bool operator < (const Node& first,const Node& second)
       {
          //對x軸的比較
          if(first.pos_x < second.pos_x)
          {
             return true;
          }
        else if (first.pos_x > second.pos_x)
          {
              return false;
        }
         //對y軸的比較
      else
      {
        if(first.pos_y < second.pos_y)
             {
                 return true;
             }
             else if (first.pos_y > second.pos_y)
             {
                return false;
             }
             //對z軸的比較
         else
         {
             if(first.pos_z < second.pos_z)
                 {
                     return true;
                 }
                 else if (first.pos_z >=  second.pos_z)
                 {
                    return false;
                 }
             }
      }
       }
       friend bool operator == (const Node& first,const Node& second)
       {
             if(first.pos_x == second.pos_x && first.pos_y == second.pos_y && first.pos_z == second.pos_z)
             {
                 return true;
             }
             else
             {
                 return false;
             }
       }
 };
 class KMean
 {
 private:
     int cluster_num;//生成的簇的數(shù)量。
     std:: vector<Node> mean_nodes;//均值點
     std:: vector<Node> data;//所有的數(shù)據(jù)點
     std:: map <int , std:: vector<Node> > cluster;//簇,key為簇的下標,value為該簇中所有點
     void Init();//初始化函數(shù)(首先隨即生成代表點)
     void ClusterProcess();//聚類過程,將空間中的點分到不同的簇中
     Node GetMean(int cluster_index);//生成均值
     void NewCluster();//確定新的簇過程,該函數(shù)會調(diào)用ClusterProcess函數(shù)。
     double Kdistance(Node active,Node other);//判斷兩個點之間的距離
     public:
     KMean(int c_num,std:: vector<Node> node_vector);
     void Star();//啟動k均值算法
 };
#endif // KMEAN_HEAD
//k-mean.h
#ifndef KMEAN_HEAD
#define KMEAN_HEAD
 #include <vector>
 #include <map>
 //空間點的定義
 class Node
 {
     public:
        double pos_x;
        double pos_y;
        double pos_z;
      Node()
      {
          pos_x = 0.0;
          pos_y = 0.0;
          pos_z = 0.0;
      }
      Node(double x,double y,double z)
      {
          pos_x = x;
          pos_y = y;
          pos_z = z;
      }
       friend bool operator < (const Node& first,const Node& second)
       {
          //對x軸的比較
          if(first.pos_x < second.pos_x)
          {
             return true;
          }
        else if (first.pos_x > second.pos_x)
          {
              return false;
        }
         //對y軸的比較
      else
      {
        if(first.pos_y < second.pos_y)
             {
                 return true;
             }
             else if (first.pos_y > second.pos_y)
             {
                return false;
             }
             //對z軸的比較
         else
         {
             if(first.pos_z < second.pos_z)
                 {
                     return true;
                 }
                 else if (first.pos_z >=  second.pos_z)
                 {
                    return false;
                 }
             }
      }
       }
       friend bool operator == (const Node& first,const Node& second)
       {
             if(first.pos_x == second.pos_x && first.pos_y == second.pos_y && first.pos_z == second.pos_z)
             {
                 return true;
             }
             else
             {
                 return false;
             }
       }
 };
 class KMean
 {
 private:
     int cluster_num;//生成的簇的數(shù)量。
     std:: vector<Node> mean_nodes;//均值點
     std:: vector<Node> data;//所有的數(shù)據(jù)點
     std:: map <int , std:: vector<Node> > cluster;//簇,key為簇的下標,value為該簇中所有點
     void Init();//初始化函數(shù)(首先隨即生成代表點)
     void ClusterProcess();//聚類過程,將空間中的點分到不同的簇中
     Node GetMean(int cluster_index);//生成均值
     void NewCluster();//確定新的簇過程,該函數(shù)會調(diào)用ClusterProcess函數(shù)。
     double Kdistance(Node active,Node other);//判斷兩個點之間的距離
     public:
     KMean(int c_num,std:: vector<Node> node_vector);
     void Star();//啟動k均值算法
 };
#endif // KMEAN_HEAD
#include "k-mean.h"
#include <vector>
#include <map>
#include <ctime>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <iostream>
 using namespace std;
 #define MAXDISTANCE 1000000
 KMean::KMean(int c_num,vector<Node> node_vector)
 {
       cluster_num = c_num;
       data = node_vector;
       srand((int)time(0));
       Init();
 }
 void KMean::Init()//初始化函數(shù)(首先隨即生成代表點)
 {
      for(int i =0 ;i<cluster_num;)
      {
            int pos = rand() % data.size();
bool insert_flag = true;
            //首先判斷選中的點是否是中心點
            for(unsigned int j = 0;j<mean_nodes.size();j++)
            {
                if(mean_nodes[j] ==  data[i])
                {
                    insert_flag = false;
                    break;
                }
            }
            if(insert_flag )
            {
                  mean_nodes.push_back(data[pos]);
                  i++;
            }
      }
      ClusterProcess();//進行聚類過程
 }
  void KMean::ClusterProcess()//聚類過程,將空間中的點分到不同的簇中
  {
             //遍歷空間上所有的點
             for( unsigned int i = 0 ; i < data.size();i++)
             {
                  //忽略中心點
                 bool continue_flag = false;
                 for(unsigned int j = 0;j<mean_nodes.size();j++)
                     {
                         if(mean_nodes[j] ==  data[i])
                         {
                                 continue_flag = true;
                                 break;
                         }
                     }
                 if(continue_flag)
                     {
                         continue;
                     }
                  //下面是聚類過程
                  //首先找到離當前點最近的中心點,并記錄下該中心點所在的簇
                  int min_kdistance = MAXDISTANCE;
                  int index = 0 ;
                  for(unsigned int j = 0;j < mean_nodes.size();j++)
                  {
                      double dis = Kdistance(data[i],mean_nodes[j]);
                      if(dis < min_kdistance)
                      {
                          min_kdistance = dis;
                          index = j;
                      }
                  }
                   //先將當前點從原先的簇中刪除
                   map<int,vector<Node> >::iterator iter;
         //搜索所有的簇
                   for(iter = cluster.begin();iter != cluster.end();++iter)
                   {
                vector<Node>::iterator node_iter;
                      bool jump_flag = false;
                      //對每個簇中的vector進行搜索
                      for(node_iter = iter->second.begin();node_iter != iter->second.end();node_iter++)
                   {
                   if(*node_iter == data[i])
                  {
                             //如果當前點就在更新的簇中,則忽略后面的操作
                       if(index == iter->first)
                            {
                           continue_flag = true;
                             }
                             else
                             {
                           iter->second.erase(node_iter);
                             }
                       jump_flag = true;
                       break;
                    }
                }
                    if(jump_flag)
                    {
                     break;
                    }
                     }
                   if(continue_flag)
               {
                     continue;
                   }
                   //將當前點插入到中心點所對應的簇中
                   //查看中心點是否已經(jīng)存在map中
                  bool insert_flag = true;
                  for(iter = cluster.begin(); iter != cluster.end();++iter)
                  {
                     if(iter->first == index)
                  {
                          iter->second.push_back(data[i]);
                          insert_flag = false;
                       break;
                   }
                  }
                  //不存在則創(chuàng)建新的元素對象
                  if(insert_flag)
                  {
                      vector<Node> cluster_node_vector;
                      cluster_node_vector.push_back(data[i]);
                      cluster.insert(make_pair(index,cluster_node_vector));
                  }
             }
  }
  double KMean::Kdistance(Node active,Node other)
  {
         return sqrt(pow((active.pos_x-other.pos_x),2) + pow((active.pos_y - other.pos_y),2) + pow((active.pos_z - other.pos_z),2));
  }
  Node KMean::GetMean(int cluster_index)
  {
      //對傳入的參數(shù)進行判斷,查看是否越界
      if( cluster_num <0 || unsigned (cluster_index) >= mean_nodes.size() )
      {
          Node new_node;
          new_node.pos_x = -1.0;
          new_node.pos_y = -1.0;
          new_node.pos_z = -1.0;
          return new_node;
      }
      //求出簇中所有點的均值
      Node sum_node;
      Node aver_node;
        for(int j = 0;j < cluster[cluster_index].size();j++)
         {
           sum_node.pos_x += cluster[cluster_index].at(j).pos_x;
            sum_node.pos_y += cluster[cluster_index].at(j).pos_y;
           sum_node.pos_z += cluster[cluster_index].at(j).pos_z;
        }
         aver_node.pos_x = sum_node.pos_x*1.0/ cluster[cluster_index].size();
         aver_node.pos_y = sum_node.pos_y*1.0 / cluster[cluster_index].size();
         aver_node.pos_z = sum_node.pos_z*1.0 / cluster[cluster_index].size();
       //找到與均值最近的點
      double min_dis = MAXDISTANCE;
      Node new_mean_doc;
      for(unsigned int i  = 0;i< cluster[cluster_index].size();i++)
      {
            double dis = Kdistance(aver_node,cluster[cluster_index].at(i));
            if(min_dis > dis)
            {
                  min_dis = dis;
                  new_mean_doc = cluster[cluster_index].at(i);
            }
      }
      return new_mean_doc;
  }
  void KMean::NewCluster()//確定新的中心點
  {
       for (unsigned int i = 0;i < mean_nodes.size();i++)
       {
            Node new_node =GetMean(i);
            mean_nodes[i] = new_node;
       }
       ClusterProcess();
  }
 void KMean::Star()
 {
     for (int i = 0;i<100;i++)
     {
         NewCluster();
         cout << "no:"<< i<<endl;
         for(int j = 0;j < mean_nodes.size();j++)
     {
         cout << cluster[j].size()<<endl;
     }
     }
 }
#include <iostream>
#include <vector>
#include "k-mean.h"
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
 {
     srand((int) time(0));
vector<Node> data;
     for(int i =0;i<100;i++)
     {
          Node node;
          node.pos_x = (random() % 17 )*1.2;
          node.pos_y = (random() % 19 )*1.2;
          node.pos_z = (random() % 21) *1.2;
          data.push_back(node);
     }
     KMean kmean(3,data);
     kmean.Star();
     return 0;
 }
欄 目:C語言
下一篇:利用C++實現(xiàn)矩陣的相加/相稱/轉(zhuǎn)置/求鞍點
本文標題:k均值算法c++語言實現(xiàn)代碼
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/3929.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
 - 01-10深入理解C++中常見的關(guān)鍵字含義
 - 01-10使用C++實現(xiàn)全排列算法的方法詳解
 - 01-10深入第K大數(shù)問題以及算法概要的詳解
 - 01-10c++中inline的用法分析
 - 01-10深入N皇后問題的兩個最高效算法的詳解
 - 01-10用C++實現(xiàn)DBSCAN聚類算法
 - 01-10深入全排列算法及其實現(xiàn)方法
 - 01-10全排列算法的非遞歸實現(xiàn)與遞歸實現(xiàn)的方法(C++)
 - 01-10C++大數(shù)模板(推薦)
 


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
 - 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
 - 04-02c語言的正則匹配函數(shù) c語言正則表達
 - 04-02c語言用函數(shù)寫分段 用c語言表示分段
 - 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
 - 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
 - 04-02c語言沒有round函數(shù) round c語言
 - 04-02c語言分段函數(shù)怎么求 用c語言求分段
 - 04-02C語言中怎么打出三角函數(shù) c語言中怎
 - 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
 
隨機閱讀
- 01-10delphi制作wav文件的方法
 - 01-10C#中split用法實例總結(jié)
 - 01-11ajax實現(xiàn)頁面的局部加載
 - 08-05DEDE織夢data目錄下的sessions文件夾有什
 - 04-02jquery與jsp,用jquery
 - 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
 - 01-10使用C語言求解撲克牌的順子及n個骰子
 - 01-10SublimeText編譯C開發(fā)環(huán)境設置
 - 08-05織夢dedecms什么時候用欄目交叉功能?
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 


