21xrx.com
2025-06-30 23:33:43 Monday
文章检索 我的文章 写文章
用C++编写k-means算法来分析鸢尾花数据
2023-10-22 13:48:33 深夜i     55     0
C++ means算法 鸢尾花数据分析

鸢尾花数据集是一个经典的机器学习数据集,用于分类问题和聚类问题的研究。鸢尾花数据集包含了150个样本,每个样本有四个特征:花萼长度、花萼宽度、花瓣长度和花瓣宽度。这些样本被分为三个不同的类别:Setosa、Versicolor和Virginica。

为了更好地理解和分析鸢尾花数据集,我们可以使用C++编写k-means算法来对数据进行聚类。

首先,我们需要导入鸢尾花数据集,并将其存储在一个二维数组中。然后,我们将初始化k个初始聚类中心。在这个例子中,我们选择k值为3,代表我们将数据集分为三个不同的类别。

接下来,我们将迭代地执行以下步骤,直到聚类中心不再发生变化:

1. 对于每个样本,计算它与每个聚类中心的距离,并将其分配给最近的聚类中心。

2. 对于每个聚类中心,更新其位置为该簇中所有样本的平均值。

重复以上步骤,直到聚类中心不再发生变化。最终,我们将得到三个不同的聚类簇,每个簇都代表了一种鸢尾花的类别。

下面是用C++编写k-means算法的示例代码:

#include <iostream>
#include <vector>
#include <cmath>
// Function to calculate distance between two points
double calculateDistance(std::vector<double> point1, std::vector<double> point2) {
  double distance = 0.0;
  for (int i = 0; i < point1.size(); ++i) {
    distance += pow(point1[i] - point2[i], 2);
  }
  return sqrt(distance);
}
// Function to assign samples to nearest cluster
std::vector<int> assignSamplesToCluster(std::vector<std::vector<double>> samples, std::vector<std::vector<double>> clusterCenters) {
  std::vector<int> assignedClusters;
  for (int i = 0; i < samples.size(); ++i) {
    double minDistance = calculateDistance(samples[i], clusterCenters[0]);
    int assignedCluster = 0;
    for (int j = 1; j < clusterCenters.size(); ++j) {
      double distance = calculateDistance(samples[i], clusterCenters[j]);
      if (distance < minDistance)
        minDistance = distance;
        assignedCluster = j;
      
    }
    assignedClusters.push_back(assignedCluster);
  }
  return assignedClusters;
}
// Function to update cluster centers
std::vector<std::vector<double>> updateClusterCenters(std::vector<std::vector<double>> samples, std::vector<int> assignedClusters, int k) {
  std::vector<std::vector<double>> clusterCenters(k, std::vector<double>(samples[0].size(), 0.0));
  std::vector<int> clusterSizes(k, 0);
  
  for (int i = 0; i < samples.size(); ++i) {
    int cluster = assignedClusters[i];
    for (int j = 0; j < samples[i].size(); ++j) {
      clusterCenters[cluster][j] += samples[i][j];
    }
    clusterSizes[cluster] += 1;
  }
  
  for (int i = 0; i < clusterCenters.size(); ++i) {
    for (int j = 0; j < clusterCenters[i].size(); ++j) {
      clusterCenters[i][j] /= clusterSizes[i];
    }
  }
  
  return clusterCenters;
}
int main() {
  std::vector<std::vector<double>> samples = {5.1,
                         1.4,
                         1.4,
                        // ... additional samples
                         6.0};
  std::vector<std::vector<double>> clusterCenters = { 0.2,
                            3.2,
                            6.3};
  
  int k = 3; // number of clusters
  
  // Iterate until cluster centers do not change
  while (true) {
    std::vector<int> assignedClusters = assignSamplesToCluster(samples, clusterCenters);
    std::vector<std::vector<double>> newClusterCenters = updateClusterCenters(samples, assignedClusters, k);
    
    if (newClusterCenters == clusterCenters)
      break;
    
    
    clusterCenters = newClusterCenters;
  }
  
  // Print the final cluster centers
  for (int i = 0; i < clusterCenters.size(); ++i) {
    std::cout << "Cluster " << i+1 << " center: ";
    for (int j = 0; j < clusterCenters[i].size(); ++j) {
      std::cout << clusterCenters[i][j] << " ";
    }
    std::cout << std::endl;
  }
  
  return 0;
}

上述代码展示了如何使用C++编写k-means算法来分析鸢尾花数据集。我们首先定义了计算距离的函数以及分配样本到最近聚类中心的函数。然后,我们定义了更新聚类中心的函数。最后,在主函数中实现了迭代计算直到聚类中心不再变化的逻辑。最终,我们打印出了得到的聚类中心。

通过这个例子,我们可以看到k-means算法的基本思想和应用。通过适当选择k值和初始聚类中心,我们可以对鸢尾花数据集进行聚类分析并得到有意义的结果。

  
  

评论区