溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C++中聚類算法的數(shù)據(jù)壓縮與解壓

發(fā)布時(shí)間:2024-11-11 09:45:54 來(lái)源:億速云 閱讀:78 作者:小樊 欄目:編程語(yǔ)言

在C++中,聚類算法可以用于數(shù)據(jù)壓縮,因?yàn)樗鼈兛梢詫⑾嗨频臄?shù)據(jù)點(diǎn)分組在一起,從而減少數(shù)據(jù)集中的冗余。以下是一個(gè)簡(jiǎn)單的例子,展示了如何使用K-means聚類算法進(jìn)行數(shù)據(jù)壓縮和解壓:

#include <iostream>
#include <vector>
#include <cmath>
#include <random>
#include <algorithm>

// K-means聚類算法
std::vector<std::vector<double>> kMeans(const std::vector<std::vector<double>>& data, int k, int maxIterations = 100) {
    int n = data.size();
    std::vector<int> labels(n, -1);
    std::vector<std::vector<double>> centroids(k, std::vector<double>(data[0].size(), 0));
    std::vector<std::vector<double>> clusterCentroids(k, std::vector<double>(data[0].size(), 0));

    // 隨機(jī)初始化質(zhì)心
    std::random_device rd;
    std::mt19937 gen(rd());
    std::shuffle(data.begin(), data.end(), gen);
    for (int i = 0; i < k; ++i) {
        centroids[i] = data[i];
    }

    for (int i = 0; i < maxIterations; ++i) {
        std::vector<std::vector<double>> clusters(k);

        // 將數(shù)據(jù)點(diǎn)分配到最近的質(zhì)心
        for (int j = 0; j < n; ++j) {
            double minDist = std::numeric_limits<double>::max();
            int minIndex = -1;
            for (int l = 0; l < k; ++l) {
                double dist = 0;
                for (int m = 0; m < data[j].size(); ++m) {
                    dist += pow(data[j][m] - centroids[l][m], 2);
                }
                if (dist < minDist) {
                    minDist = dist;
                    minIndex = l;
                }
            }
            labels[j] = minIndex;
            clusters[minIndex].push_back(data[j]);
        }

        // 更新質(zhì)心
        for (int j = 0; j < k; ++j) {
            double sum = 0;
            for (const auto& point : clusters[j]) {
                for (int m = 0; m < point.size(); ++m) {
                    sum += point[m];
                }
            }
            for (int m = 0; m < point.size(); ++m) {
                centroids[j][m] = sum / clusters[j].size();
            }
        }
    }

    // 計(jì)算最終的質(zhì)心
    std::vector<std::vector<double>> finalCentroids;
    for (int i = 0; i < k; ++i) {
        if (!clusters[i].empty()) {
            double sum = 0;
            for (const auto& point : clusters[i]) {
                for (int m = 0; m < point.size(); ++m) {
                    sum += point[m];
                }
            }
            for (int m = 0; m < point.size(); ++m) {
                finalCentroids[i][m] = sum / clusters[i].size();
            }
        }
    }

    return finalCentroids;
}

// 數(shù)據(jù)壓縮
std::vector<std::vector<double>> compressData(const std::vector<std::vector<double>>& data, int k) {
    std::vector<std::vector<double>> centroids = kMeans(data, k);
    std::vector<std::vector<double>> compressedData;

    for (const auto& point : data) {
        double minDist = std::numeric_limits<double>::max();
        int minIndex = -1;
        for (int i = 0; i < centroids.size(); ++i) {
            double dist = 0;
            for (int m = 0; m < point.size(); ++m) {
                dist += pow(point[m] - centroids[i][m], 2);
            }
            if (dist < minDist) {
                minDist = dist;
                minIndex = i;
            }
        }
        compressedData.push_back(centroids[minIndex]);
    }

    return compressedData;
}

// 數(shù)據(jù)解壓
std::vector<std::vector<double>> decompressData(const std::vector<std::vector<double>>& compressedData, const std::vector<std::vector<double>>& originalData, int k) {
    std::vector<std::vector<double>> centroids = kMeans(originalData, k);
    std::vector<std::vector<double>> decompressedData;

    for (const auto& point : compressedData) {
        double minDist = std::numeric_limits<double>::max();
        int minIndex = -1;
        for (int i = 0; i < centroids.size(); ++i) {
            double dist = 0;
            for (int m = 0; m < point.size(); ++m) {
                dist += pow(point[m] - centroids[i][m], 2);
            }
            if (dist < minDist) {
                minDist = dist;
                minIndex = i;
            }
        }
        decompressedData.push_back(originalData[minIndex]);
    }

    return decompressedData;
}

int main() {
    std::vector<std::vector<double>> data = {{1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}};
    int k = 2;

    // 數(shù)據(jù)壓縮
    std::vector<std::vector<double>> compressedData = compressData(data, k);
    std::cout << "Compressed data:" << std::endl;
    for (const auto& point : compressedData) {
        std::cout << "[" << point[0] << ", " << point[1] << "]" << std::endl;
    }

    // 數(shù)據(jù)解壓
    std::vector<std::vector<double>> decompressedData = decompressData(compressedData, data, k);
    std::cout << "Decompressed data:" << std::endl;
    for (const auto& point : decompressedData) {
        std::cout << "[" << point[0] << ", " << point[1] << "]" << std::endl;
    }

    return 0;
}

這個(gè)例子中,我們首先使用K-means聚類算法對(duì)數(shù)據(jù)進(jìn)行壓縮,將相似的數(shù)據(jù)點(diǎn)分組在一起。然后,我們可以使用相同的算法對(duì)壓縮后的數(shù)據(jù)進(jìn)行解壓,恢復(fù)原始數(shù)據(jù)。請(qǐng)注意,這個(gè)例子僅用于演示目的,實(shí)際應(yīng)用中可能需要根據(jù)具體需求進(jìn)行調(diào)整。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

c++
AI