美文网首页
Machine Learning - Week 8 K-Mean

Machine Learning - Week 8 K-Mean

作者: 有何不可_b7e1 | 来源:发表于2019-01-28 16:52 被阅读0次

    K-Means算法相关:

    findClosestCentroids:

    function idx = findClosestCentroids(X, centroids)

    K = size(centroids, 1);

    idx = zeros(size(X,1), 1);

    % ====================== YOUR CODE HERE ======================

    m = size(X, 1);

    for i = 1:m

      distance_vec = zeros(K, 1);

      for k = 1:K

        distance_vec(k) = sum((X(i, :) - centroids(k, :)) .^ 2);

      endfor

      [min_dist, min_idx] = min(distance_vec);

      idx(i) = min_idx;

    endfor

    % =============================================================

    end

    computeCentroids:

    function centroids = computeCentroids(X, idx, K)

    [m n] = size(X);

    centroids = zeros(K, n);

    % ====================== YOUR CODE HERE ======================

    total_vec = zeros(K, n);

    count_vec = zeros(K, 1);

    for i = 1:m

      k = idx(i);

      total_vec(k, :) += X(i, :);

      count_vec(k) += 1;

    endfor

    for k = 1:K

      centroids(k, :) = total_vec(k, :) / count_vec(k);

    endfor

    % =============================================================

    end

    PCA主成分析法相关:

    pca:

    function [U, S] = pca(X)

    [m, n] = size(X);

    U = zeros(n);

    S = zeros(n);

    % ====================== YOUR CODE HERE ======================

    Sigma = (1 / m) * (X' * X);

    [U, S, V] = svd(Sigma);

    % =========================================================================

    end

    projectData:

    function Z = projectData(X, U, K)

    Z = zeros(size(X, 1), K);

    % ====================== YOUR CODE HERE ======================

    U_reduce = U(:, 1:K);

    Z = X * U_reduce;

    % =============================================================

    end

    recoverData:

    function X_rec = recoverData(Z, U, K)

    X_rec = zeros(size(Z, 1), size(U, 1));

    % ====================== YOUR CODE HERE ======================

    X_rec = Z * U(:, 1:K)';

    % =============================================================

    end

    相关文章

      网友评论

          本文标题:Machine Learning - Week 8 K-Mean

          本文链接:https://www.haomeiwen.com/subject/jyoajqtx.html