<jupyter_start><jupyter_text>K-Means from Scratch<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
from matplotlib import style
style.use("ggplot")
X = np.array([
    [1, 2],
    [1.5, 1.8],
    [5, 8],
    [8, 8],
    [1, 0.6],
    [9, 11],
    [1, 5],
    [6, 4],
    [2, 5],
    [9, 7],
    [4, 1],
    [3, 5],
])
plt.scatter(X[:,0], X[:,1], s = 150)
plt.show()
colors = 10 * ["g", "r", "b", "y", "k"]
class KMeans:
    def __init__(self, k=2, tol=0.001, max_iter=300):
        # tol: how much the centroids will move
        # max_iter: how many times the centroids will move
        self.k = k
        self.tol = tol
        self.max_iter = max_iter
        
    def fit(self, data):
        self.centroids = {}
        for i in range(self.k):
            self.centroids[i] = data[i]
            
        for i in range(self.max_iter):
            self.classifications = {}
            
            for i in range(self.k):
                self.classifications[i] = []
            
            for featureset in data:
                distances = [np.linalg.norm(featureset - self.centroids[centroid]) for centroid in self.centroids]
                classification = distances.index(min(distances))
                self.classifications[classification].append(featureset)
            prev_centroids = dict(self.centroids)

            for classification in self.classifications:
                self.centroids[classification] = np.average(self.classifications[classification], axis=0)
            optimized = True
            for c in self.centroids:
                original_centroid = prev_centroids[c]
                current_centroid = self.centroids[c]
                if np.sum((current_centroid - original_centroid) / original_centroid * 100.0) > self.tol:
                    optimized = False
            if optimized:
                break
            
    
    def predict(self, data):
        distances = [np.linalg.norm(data - self.centroids[centroid]) for centroid in self.centroids]
        classification = distances.index(min(distances))
        return classification
clf = KMeans()
clf.fit(X)
for centroid in clf.centroids:
    plt.scatter(clf.centroids[centroid][0], 
                clf.centroids[centroid][1],
               marker="o",
               color="k",
               s=150,  # size
               linewidths=5)
    
for classification in clf.classifications:
    color = colors[classification]
    for featureset in clf.classifications[classification]:
        plt.scatter(featureset[0], featureset[1], marker="x", 
                    color=color, s=150, linewidths=5)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12034599473486272, 'fraction_numerical': 0.022564874012786763, 'mean_word_length': 1.9820627802690582, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '356057', 'license': 'Apache-2.0', 'n_tokens_mistral': 894, 'n_tokens_neox': 845, 'n_words': 190, 'path': 'Clustering/Flat Clustering/K-Means-From-Scratch.ipynb', 'repo_name': 'KostadinPlachkov/MachineLearning', 'size': '2659'}