<jupyter_start><jupyter_text>1.找出距离每个样本最近的聚类中心<jupyter_code>def findClosestCentroids(X, centroids):
    c = np.empty((0,X.shape[0]))
    for i in range(len(centroids)):
        c = np.vstack((c,np.sum((X - centroids[i])**2,axis = 1)))#这里利用了numpy的广播原理，和ndarray的垂直堆叠方法
    idx = np.argmin(c, axis = 0)#找出每列的最小值的索引
    return idx
data = loadmat(".\\data\\ex7data2.mat")
X = data["X"]
initial_centroids = np.array([[3, 3], [6, 2], [8, 5]])
idx = findClosestCentroids(X,initial_centroids)
idx[0:3]<jupyter_output><empty_output><jupyter_text>2.更新聚类中心<jupyter_code>def computeCentroids(X,centroids,idx):
    new_centroids = np.empty((0,centroids.shape[1]))
    for i in range(len(centroids)):
        c = X[idx == i]#用掩码数组选择对应聚簇的所有样本
        new_centroids = np.vstack((new_centroids,c.mean(axis=0)))#计算同一聚簇的平均聚类中心
    return new_centroids
computeCentroids(X,initial_centroids,idx)<jupyter_output><empty_output><jupyter_text>3.运行k-means聚类算法<jupyter_code>def k_means(X,K,epoch):
    randidx = np.random.randint(1,X.shape[0])
    centroids = X[randidx - K:randidx]#随机初始化，取随机索引的前K个样本作为初始化聚类中心
    for i in range(epoch):
        idx = findClosestCentroids(X, centroids)
        centroids = computeCentroids(X,centroids,idx )
    return centroids,idx
centorids,idx = k_means(X,K=3,epoch=500)<jupyter_output><empty_output><jupyter_text>4.效果展示<jupyter_code>def show_k_means(input,K=3,epoch=500):
    centorids,idx = k_means(input,K,epoch)
    clusters = []
    for i in range(K):
        clusters.append("cluster{}".format(i))
    fig, axarr = plt.subplots(1,2,figsize=(21,8))
    for i in range(K):
        clusters[i] = X[np.where(idx == i)[0],:]
        axarr[1].scatter(clusters[i][:,0], clusters[i][:,1], s=30, cmap='rainbow', label='Cluster{}'.format(i))
    axarr[1].legend()
    axarr[0].scatter(X[:,0],X[:,1])
    plt.show()
show_k_means(X,K=6)<jupyter_output><empty_output><jupyter_text>5.用k-means对图像进行压缩<jupyter_code>from IPython.display import Image
Image(filename='data/bird_small.png')
data = loadmat(".\\data\\bird_small.mat")
data.keys()
A = data["A"]
A.shape
"预处理"
pit = A/255
pit = pit.reshape(pit.shape[0]*pit.shape[1],pit.shape[2])
centroids, idx = k_means(pit, 16, 500)
idx = findClosestCentroids(pit, centroids)
X_recovered = centroids[idx,:]
X_recovered.shape
X_recovered = np.reshape(X_recovered, (A.shape[0], A.shape[1], A.shape[2]))
plt.imshow(X_recovered)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17566445182724252, 'fraction_numerical': 0.02616279069767442, 'mean_word_length': 5.636363636363637, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '85111', 'license': 'MIT', 'n_tokens_mistral': 1129, 'n_tokens_neox': 1140, 'n_words': 128, 'path': 'code-homework/ML/ex7_k-means/k-means.ipynb', 'repo_name': 'phww/Andrew.Ng-ML-Study', 'size': '2408'}