<jupyter_start><jupyter_text>Make all datapoints centroidsTake mean of all featuresets within centroid's radius, setting this mean as new centroid.Repeat step 2 until convergence.<jupyter_code>import matplotlib.pyplot as plt
from matplotlib import style
style.use('ggplot')
import numpy as np

X = np.array([[1, 2],[1.5, 1.8],[5, 8 ],[8, 8],[1, 0.6],
              [9,11],[8,2],[10,2],[9,3]])
colors = 10*["g","r","c","b","k"]

class Mean_Shift:
    def __init__(self, radius=10):
        self.radius = radius

    def fit(self, data):
        centroids = {}

        for i in range(len(data)):
            centroids[i] = data[i]
        while True:        
            new_centroids = []
            for i in centroids:
                in_bandwidth = []
                centroid = centroids[i]
                for featureset in data:
                    if np.linalg.norm(featureset-
                        centroid) < self.radius:
                        in_bandwidth.append(featureset)

                new_centroid = np.average(in_bandwidth,axis=0)
                new_centroids.append(tuple(new_centroid))
                
            uniques = sorted(list(set(new_centroids)))
            prev_centroids = dict(centroids)
            centroids = {}
            for i in range(len(uniques)):
                centroids[i] = np.array(uniques[i])
            optimized = True
            for i in centroids:
                if (np.array_equal(centroids[i],
                    prev_centroids[i])) != True:
                    optimized = False                 
                    break
            if optimized: 
                break    
        self.centroids = centroids
    
    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 = sklearn.Mean_Shift()
clf.fit(X)
centroids = clf.centroids
print(centroids)
clf.predict((10,2))
clf.predict((10,2))
import pandas as pd
df = pd.read_csv('Breast-Cancer.csv')
df.replace('?',-99999,inplace=True)
df.drop(['id','label'],axis=1,inplace=True)
data = np.array(df.astype(float).values.tolist())
clf = Mean_Shift()
clf.fit(data)
centroids = clf.centroids
print(centroids)

l = [3,4,6] #list of distances with cluster 0,1,2 respectively
l.index(min(l)) #Gives me name of cluster with min distance from pt<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11910471622701839, 'fraction_numerical': 0.018784972022382093, 'mean_word_length': 2.033939393939394, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, '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': '343975', 'license': 'MIT', 'n_tokens_mistral': 827, 'n_tokens_neox': 786, 'n_words': 170, 'path': '2 Unsupervised Learning/Clustering Algos/Meanshift Clustering, Unsupervised.ipynb', 'repo_name': 'HJJ256/Machine-Learning-2018-present-', 'size': '2502'}