[Coursera机器学习]K-means Clustering and Principal Component Analysis WEEK8编程作业

本文详细介绍了K-means聚类算法的实现,包括找到最近质心和计算质心均值的过程。同时,深入探讨了PCA主成分分析,从数据预处理、主成分计算到数据降维和重构的步骤。通过随机初始化和PCA,可以有效地理解和应用这些机器学习概念。
摘要由CSDN通过智能技术生成

1.1.1 Finding closest centroids

Your task is to complete the code in findClosestCentroids.m. This function takes the data matrix X and the locations of all centroids inside centroids and should output a one-dimensional array idx that holds the index (a value in {1,…,K}, where K is total number of centroids) of the closest centroid to every training example.

% Traverse all points of X
for i = 1:length(X)
    min = Inf;
    % Traverse all points of K, judge which cluster the point belong to
    for j = 1:K
        dist = norm(X(i,:) - centroids(j,:))^2;
        if dist < min
            min = dist;
            idx(i) = j;
        end
    end
end

1.1.2 Computing centroid means

You should now complete the code in computeCentroids.m. You can implement this function using a loop over the centroids. You can also use a loop over the examples; but if you can use a vectorized implementation that does not use such a loop, your code may run faster.

for i = 1:K
    count = 0;
    for j = 1:m
        if idx(j) == i
            count = count + 1;
            centroids(i,:) = centroids(i,:) + X(j,:);
        end
    end
    centroids(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值