Skip to content

Commit

Permalink
Gustafson-Kessel clustering algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
ksiminski committed Sep 18, 2023
1 parent 39c018d commit 13cee7f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
40 changes: 19 additions & 21 deletions source/partitions/gk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "../metrics/metric_mahalanobis.h"
#include "../partitions/gk.h"
#include "../auxiliary/matrix.h"
#include "../auxiliary/vector-operators.h"

std::string ksi::gk::getAbbreviation () const
{
Expand Down Expand Up @@ -71,8 +72,6 @@ double ksi::gk::calculateDistance(const std::vector<double>& x, const std::vecto
return metrics_for_clusters[cluster].calculateDistance(x, y);
}



std::vector<std::vector<double>> ksi::gk::modifyPartitionMatrix(const std::vector<std::vector<double>>& mV, const std::vector<std::vector<double>>& mX)
{
try
Expand Down Expand Up @@ -106,16 +105,6 @@ std::vector<std::vector<double>> ksi::gk::modifyPartitionMatrix(const std::vecto
}
}

// for (std::size_t c = 0; c < nClusters; c++)
// {
// for (std::size_t x = 0; x < nX; x++)
// {
// if (Dm[c][x] == 0)
// Dmzeros[x]++;
// Dmsums[x] += Dm[c][x];
// }
// }

for (std::size_t c = 0; c < nClusters; c++)
{
for (std::size_t x = 0; x < nX; x++)
Expand Down Expand Up @@ -169,10 +158,15 @@ ksi::partition ksi::gk::doPartition(const ksi::dataset& ds)
{
mV = calculateClusterCentres(mU, mX);
std::vector<Matrix<double>> covariance_matrices = calculateCovarianceMatrices(mX, mU, mV);
updateMetrics(covariance_matrices, nAttr);
if (ksi::is_valid(covariance_matrices))
updateMetrics(covariance_matrices, nAttr);

mU = modifyPartitionMatrix (mV, mX);
normaliseByColumns(mU);
auto mU_new = modifyPartitionMatrix (mV, mX);
if (ksi::is_valid(mU_new))
{
mU = mU_new;
normaliseByColumns(mU);
}
}
}
else if (_epsilon > 0)
Expand All @@ -182,12 +176,17 @@ ksi::partition ksi::gk::doPartition(const ksi::dataset& ds)
{
mV = calculateClusterCentres(mU, mX);
std::vector<Matrix<double>> covariance_matrices = calculateCovarianceMatrices(mX, mU, mV);
updateMetrics(covariance_matrices, nAttr);
if (ksi::is_valid(covariance_matrices))
updateMetrics(covariance_matrices, nAttr);

auto mUnew = modifyPartitionMatrix (mV, mX);
normaliseByColumns(mUnew);
frob = Frobenius_norm_of_difference (mU, mUnew);
mU = mUnew;
auto mU_new = modifyPartitionMatrix (mV, mX);
if (ksi::is_valid(mU_new))
{
mU = mU_new;
normaliseByColumns(mU_new);
}
frob = Frobenius_norm_of_difference (mU, mU_new);
mU = mU_new;
} while (frob > _epsilon);
}

Expand Down Expand Up @@ -216,7 +215,6 @@ std::vector<ksi::Matrix<double>> ksi::gk::get_A_matrices_from_metrics(const std:
return A_matrices;
}


void ksi::gk::updateMetrics(const std::vector<Matrix<double>>& covariance_matrices, const int nAttr)
{
metrics_for_clusters.clear();
Expand Down
2 changes: 1 addition & 1 deletion source/partitions/gk.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace ksi
double _volume_rho = 1.0; // cluster volume parameter
public:
gk ();
gk (const int nClusters, const int nIterations);
gk (const double volume_rho);
gk (const gk & wzor) = default;
gk (gk && wzor) = default;
Expand Down Expand Up @@ -59,5 +60,4 @@ namespace ksi
};
}


#endif

0 comments on commit 13cee7f

Please sign in to comment.