<jupyter_start><jupyter_text>Chapter 1: Introduction to Clustering - Tested and Verified Exercise 1.04 - 1.05  General Feedback1. Decide on the indentation and keep it that way2. Make comments3. Define Goal for the exercise and each module.<jupyter_code># Tested And Verified
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
import numpy as np
import math
%matplotlib inline
#Fix the seed to have the reproducible results.
np.random.seed(20)
# Tested And Verified
X, y = make_blobs(n_samples=1500, centers=3, n_features=2, random_state=800)
centroids = [[-6,2],[3,-4],[-5,10]]
X
y
centroids
# Tested And Verified
# We dont need this
plt.scatter(X[:, 0], X[:, 1], s=50, cmap='tab20b')
plt.show()
# Tested And Verified
plt.scatter(X[:, 0], X[:, 1], c=y,s=50, cmap='tab20b')
plt.show()
# Tested And Verified
def dist(a, b):
    return math.sqrt(math.pow(a[0]-b[0],2) + math.pow(a[1]-b[1],2))
from scipy.spatial.distance import cdist
X[105:110]
# Tested And Verified
# Please specify the need for this snippet in simple terms, what we meant by cluster membership.
for x in X[105:110]:
    calcs = []
    for c in centroids:
        calcs.append(dist(x, c))
    print(calcs, "Cluster Membership: ", np.argmin(calcs, axis=0))<jupyter_output>[4.027750355981394, 10.70202290628413, 5.542160268055164] Cluster Membership:  0
[9.73035280174993, 7.208665829113462, 17.44505393393603] Cluster Membership:  1
[4.066767506545852, 11.113179986633003, 5.1589701124301515] Cluster Membership:  0
[5.284418164665783, 8.931464028407861, 13.314157359115697] Cluster Membership:  0
[6.293105164930943, 13.467921029846712, 2.664298385076878] Cluster Membership:  2<jupyter_text>Fix Indentation; An advice use Spyder's static code analysis before committing the code.<jupyter_code># Tested And Verified
def k_means(X, K):
    # Keep track of history so you can see K-means in action
    centroids_history = []
    labels_history = []
    rand_index = np.random.choice(X.shape[0], K)  
    centroids = X[rand_index]
    centroids_history.append(centroids)
    while True:
    # Euclidean distances are calculated for each point relative to centroids, and then np.argmin returns
    # the index location of the minimal distance which cluster a point is assigned to.
        labels = np.argmin(cdist(X, centroids), axis=1)
        labels_history.append(labels)
        # Take mean of points within clusters to find new centroids:
        new_centroids = np.array([X[labels == i].mean(axis=0) for i in range(K)])
        centroids_history.append(new_centroids)
        # If old centroids and new centroids no longer change, K-means is complete and end. Otherwise continue
        if np.all(centroids == new_centroids):
            break
        centroids = new_centroids
    return centroids, labels, centroids_history, labels_history
centers, labels, centers_hist, labels_hist = k_means(X, 3)<jupyter_output><empty_output><jupyter_text>Output is not matching; history = zip(centers_hist, labels_hist) is not present in the documentFix a seed for randomness to avoid this issue<jupyter_code>history = zip(centers_hist, labels_hist)
# Tested And Verified
# Add comment
for x, y in history:
    plt.figure(figsize=(4,3))
    plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='tab20b')
    plt.scatter(x[:, 0], x[:, 1], c='red')
    plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10950838975566676, 'fraction_numerical': 0.09449514277303503, 'mean_word_length': 4.5252032520325205, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '206002', 'license': 'MIT', 'n_tokens_mistral': 1386, 'n_tokens_neox': 1196, 'n_words': 349, 'path': 'CH1/Exercise 1.04-1.05/Exercise1.04 - Exercise1.05.ipynb', 'repo_name': 'iamrochit/Applied-Unsupervised-Learning-with-Python', 'size': '3397'}