• 大小: 9.71MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-10-07
  • 语言: C/C++
  • 标签: RBF  

资源简介

GitHub搬运,作者主页:https://github.com/osama-afifi

资源截图

代码片段和文件信息

#include “KmeansPP.h“

using namespace std;

KmeansPP::KmeansPP(const std::vector &input_data) 
: input_data(input_data) random_index_gen(0 input_data.size()-1) random_real_gen(0 1) random_engine(rd())
{}

KmeansPP::~KmeansPP(void)
{}

vector > KmeansPP::RunKMeansPP(int K vector ¢roids)
{
assert(K<=input_data.size());
vector > clusters_vec(K); 
// First: initalize the initial centroids according to K-Means Plus Plus Algorithm
init();
int first_centroid = random_index_gen(random_engine);
initial_centroids_.push_back(input_data[first_centroid]);
for(int i=1 ; i {
updateNearestClusters(initial_centroids_);
initial_centroids_.push_back(input_data[getNextInitialCentroidIndex()]);
}
// Second: Continue as in the regular K-means clustering algoithm
cur_centroids_ = initial_centroids_;
do
{
prev_centroids_ = cur_centroids_;
updateNearestClusters(cur_centroids_);
updateCentroids(cur_centroids_);
}
while(!equalCentroids(cur_centroids_prev_centroids_));

// Push resuts into the clusters vector
for(int i = 0 ; i clusters_vec[nearest_cluster_idx[i]].push_back(i);
centroids = cur_centroids_;
return clusters_vec;
}

int KmeansPP::getNextInitialCentroidIndex()
{
/* Adding the rest of the points according to the probability D(x)/SIGMA(D(x))
where D(x) is the distance between a datapoint x and it‘s nearest cluster */

// Total Error i.e SIGMA(D(x)
double total_distance=0;
for (int i = 0; i < input_data.size() ; ++i)
total_distance += nearest_cluster_dist[i];

// The probability D(x)/SIGMA(D(x))
vector cumm_prob(input_data.size()0);
for (int i = 0; i < input_data.size() ; ++i)
cumm_prob[i] = (nearest_cluster_dist[i] / total_distance);

// Cummulating
for (int i = 1; i < input_data.size() ; ++i)
cumm_prob[i] += cumm_prob[i-1];

// Choosing the next point with a probabilty D(x)/SIGMA(D(x))
int rand_num = random_real_gen(random_engine);
for (int i = 0; i < cumm_prob.size() ; ++i)
if (rand_num < cumm_prob[i])
return i;

return cumm_prob.size()- 1;
}

void KmeansPP::updateNearestClusters(const vector ¢roids_)
{
for (int i = 0; i < input_data.size() ; ++i)
{
int idx = getClosestCentroidIndex(i centroids_);
nearest_cluster_idx[i] = idx;
nearest_cluster_dist[i] = distance(centroids_[idx]input_data[i]);
}
}

void KmeansPP::updateCentroids(const vector ¢roids_)
{
vectorfreq(centroids_.size()0);
vectornew_centroids(centroids_.size()vector(centroids_[0].size()0));
for (int i = 0; i < input_data.size() ; ++i) // training set count
{
++freq[nearest_cluster_idx[i]];
for(int j=0 ; j< input_data[i].size() ; ++j) // feature count
new_centroids[nearest_cluster_idx[i]][j] += input_data[i][j];
}

for (int i = 0; i < centroids_.size() ; ++i)
if(freq[i])
for (int j = 0; j < centroids_[i].size() ; ++j)
new_centroids[i][j] *= (1.0/(double)freq[i])

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2015-05-09 16:21  RBF-Radial-Basis-Function-Network-master\
     目录           0  2015-05-09 16:21  RBF-Radial-Basis-Function-Network-master\Debug\
     文件       19456  2015-05-09 16:21  RBF-Radial-Basis-Function-Network-master\Debug\RadialBasisFunctionNetwok.pdb
     文件      280576  2015-05-09 16:21  RBF-Radial-Basis-Function-Network-master\Debug\RadialBasisFunctionNetwork.exe
     文件     2183464  2015-05-09 16:21  RBF-Radial-Basis-Function-Network-master\Debug\RadialBasisFunctionNetwork.ilk
     文件     2706432  2015-05-09 16:21  RBF-Radial-Basis-Function-Network-master\Debug\RadialBasisFunctionNetwork.pdb
     文件         371  2015-05-09 16:21  RBF-Radial-Basis-Function-Network-master\README.md
     文件     8716288  2015-05-09 16:21  RBF-Radial-Basis-Function-Network-master\RadialBasisFunctionNetwok.sdf
     文件    16711680  2015-05-09 16:21  RBF-Radial-Basis-Function-Network-master\RadialBasisFunctionNetwork.sdf
     文件         925  2015-05-09 16:21  RBF-Radial-Basis-Function-Network-master\RadialBasisFunctionNetwork.sln
     文件       27648  2015-05-09 16:21  RBF-Radial-Basis-Function-Network-master\RadialBasisFunctionNetwork.suo
     文件       43008  2015-05-09 16:21  RBF-Radial-Basis-Function-Network-master\RadialBasisFunctionNetwork.v11.suo
     目录           0  2015-05-09 16:21  RBF-Radial-Basis-Function-Network-master\RadialBasisFunctionNetwork\
     目录           0  2015-05-09 16:21  RBF-Radial-Basis-Function-Network-master\RadialBasisFunctionNetwork\Debug\
     文件       46016  2015-05-09 16:21  RBF-Radial-Basis-Function-Network-master\RadialBasisFunctionNetwork\Debug\CL.read.1.tlog
     文件        3612  2015-05-09 16:21  RBF-Radial-Basis-Function-Network-master\RadialBasisFunctionNetwork\Debug\CL.write.1.tlog
     文件      871662  2015-05-09 16:21  RBF-Radial-Basis-Function-Network-master\RadialBasisFunctionNetwork\Debug\KmeansPP.obj
     文件      791299  2015-05-09 16:21  RBF-Radial-Basis-Function-Network-master\RadialBasisFunctionNetwork\Debug\RBFNetwork.obj
     文件        2082  2015-05-09 16:21  RBF-Radial-Basis-Function-Network-master\RadialBasisFunctionNetwork\Debug\RadialBasisFunctionNetwok.Build.CppClean.log
     文件          88  2015-05-09 16:21  RBF-Radial-Basis-Function-Network-master\RadialBasisFunctionNetwork\Debug\RadialBasisFunctionNetwok.lastbuildstate
     文件        3854  2015-05-09 16:21  RBF-Radial-Basis-Function-Network-master\RadialBasisFunctionNetwork\Debug\RadialBasisFunctionNetwok.log
     文件           0  2015-05-09 16:21  RBF-Radial-Basis-Function-Network-master\RadialBasisFunctionNetwork\Debug\RadialBasisFunctionNetwok.unsuccessfulbuild
     文件        1035  2015-05-09 16:21  RBF-Radial-Basis-Function-Network-master\RadialBasisFunctionNetwork\Debug\RadialBasisFunctionNetwork.Build.CppClean.log
     文件          88  2015-05-09 16:21  RBF-Radial-Basis-Function-Network-master\RadialBasisFunctionNetwork\Debug\RadialBasisFunctionNetwork.lastbuildstate
     文件        3826  2015-05-09 16:21  RBF-Radial-Basis-Function-Network-master\RadialBasisFunctionNetwork\Debug\RadialBasisFunctionNetwork.log
     文件        2538  2015-05-09 16:21  RBF-Radial-Basis-Function-Network-master\RadialBasisFunctionNetwork\Debug\cl.command.1.tlog
     文件           2  2015-05-09 16:21  RBF-Radial-Basis-Function-Network-master\RadialBasisFunctionNetwork\Debug\link-cvtres.read.1.tlog
     文件           2  2015-05-09 16:21  RBF-Radial-Basis-Function-Network-master\RadialBasisFunctionNetwork\Debug\link-cvtres.write.1.tlog
     文件           2  2015-05-09 16:21  RBF-Radial-Basis-Function-Network-master\RadialBasisFunctionNetwork\Debug\link-rc.read.1.tlog
     文件           2  2015-05-09 16:21  RBF-Radial-Basis-Function-Network-master\RadialBasisFunctionNetwork\Debug\link-rc.write.1.tlog
     文件           2  2015-05-09 16:21  RBF-Radial-Basis-Function-Network-master\RadialBasisFunctionNetwork\Debug\link.1080-cvtres.read.1.tlog
............此处省略91个文件信息

评论

共有 条评论