<jupyter_start><jupyter_text>MeanShift - Clustering Algo for unSupervised Learning<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],])

plt.scatter(X[:,0], X[:,1], s=150)
plt.show()

colors = 10*["g","r","c","b","k"]<jupyter_output><empty_output><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],])

##plt.scatter(X[:,0], X[:,1], s=150)
##plt.show()

colors = 10*["g","r","c","b","k"]

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

    def fit(self, data):
        centroids = {} #dictionary mein har iter ke liye ek pt.

        for i in range(len(data)):
            centroids[i] = data[i]#har point ko centroid banaya
 #           
 #check distance between all pts for every iteration. if distance<bandwidth, append in bandwidth, 
 #average of all distances in bandwidth -> append in new centroids
        while True:        
            new_centroids = []#har iter pe empty
            for i in centroids:
                in_bandwidth = []
                centroid = centroids[i]#dictionary ki values centroid mein store
                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))#tuple:?
#turn new_centroids into a sorted list, pass cent dict into pre_centroids and empty centroids. 
#transfer unique as array to empty centroids
            uniques = sorted(list(set(new_centroids)))
            print(uniques)
            prev_centroids = dict(centroids)
            centroids = {}
            for i in range(len(uniques)):
                centroids[i] = np.array(uniques[i])
            optimized = True
            
            for i in centroids:
                #if centroids(in prev and centroids) are same(All) then loop will complete. if first one is not matched,
                #go to next centroid pair comparison in this for. procedure to optimize comparison
                if (np.array_equal(centroids[i], prev_centroids[i])) != True: 
                #[(np.array_equal(centroids[i], prev_centroids[i])) != True]=true if pair doesnt match
                    optimized = False                 
                if optimized != True: 
                    break
            if optimized: #to break while
                break
        
        self.centroids = centroids# finally centroids ko self mein dala

clf = Mean_Shift()
clf.fit(X)

centroids = clf.centroids

plt.scatter(X[:,0], X[:,1], s=150)

for c in centroids:
    plt.scatter(centroids[c][0], centroids[c][1], color='k', marker='*', s=150)

plt.show()<jupyter_output>[(1.1666666666666667, 1.4666666666666666), (6.5, 8.0), (7.333333333333333, 9.0), (8.5, 9.5), (9.0, 2.3333333333333335)]
[(1.1666666666666667, 1.4666666666666666), (7.333333333333333, 9.0), (9.0, 2.3333333333333335)]
[(1.1666666666666667, 1.4666666666666666), (7.333333333333333, 9.0), (9.0, 2.3333333333333335)]
{'fraction_non_alphanumeric': 0.12681354110693177, 'fraction_numerical': 0.07898979043524987, 'mean_word_length': 2.1739130434782608, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 12, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '148008', 'license': 'MIT', 'n_tokens_mistral': 1399, 'n_tokens_neox': 1211, 'n_words': 313, 'path': 'MeanShiftProgram.ipynb', 'repo_name': 'sambbhavgarg/Machine-Learning', 'size': '3722'}