09.10.2023 Views

Advanced Data Analytics Using Python_ With Machine Learning, Deep Learning and NLP Examples ( 2023)

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 4

Unsupervised Learning: Clustering

if source[-1] == target[-1]:

cost = 0

else:

cost = 1

res = min([ED(source[:-1], target)+1,

ED(source, target[:-1])+1,

ED(source[:-1], target[:-1]) + cost])

return res

def find_centre(x, X, mu):

min = 100

cent = 0

for c in mu:

dist = ED(x, X[c])

if dist< min:

min = dist

cent = c

return cent

def cluster_arrange(X, cent):

clusters = {}

for x in X:

bestcent = find_centre(x, X, cent)

try:

clusters[bestcent].append(x)

exceptKeyError:

clusters[bestcent] = [x]

return clusters

def rearrange_centers(cent, clusters):

newcent = []

keys = sorted(clusters.keys())

80

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!