Uploaded by Wahaj Choudhry

Implement The K-Means Clustering Algorithm Using P... Chegg.com

advertisement
6/12/22, 4:35 PM

Implement The K-Means Clustering Algorithm Using P... | Chegg.com

Home
Study tools

My courses

My books
My folder
Career
Life

Search
Find solutions for your homework
home / study / engineering / computer science / computer science questions and answers / implement the k-means clustering algorithm using py…
Question: Implement The K-Means Clustering Algorithm Using python P…
Implement The K-Means Clustering Algorithm Using python Programming Language:
• Given The Following Dataset:
(3,4) , (3,6) , (3,8) , (4,5) , (4,7) , (5,1) , (5,5) , (7,3) , (7,5) , (8,5).
• Select the Number of Clusters “K”.
• Select the Centroid Points Randomly.
• Let the User Selects Euclidian or Manhattan Distance (Run the code twice in the video).
• The Program should show all Iterations and the Final Clusters / Centroids.
• Stopping Condition: No Change in Clusters / Centroids.
• Don’t use predefined AI Functions or Libraries.
Answers from our experts for your tough
homework questions
Enter question
Continue to post
11 questions remaining
My Textbook Solutions
Expert Answer
Sashank Chittoory answered this
262 answers
Post a question
Was this answer helpful?
1
0
code:
import math
from random import shuffle, uniform
The...
The...
Criminology..
2nd Edition
3rd Edition
7th Edition
View all solutions
###_Auxiliary Function_###
def FindColMinMax(items):
n = len(items[0])
minima = [float('inf') for i in range(n)]
maxima = [float('-inf') for i in range(n)]
for item in items:
for f in range(len(item)):
if(item[f] < minima[f]):
minima[f] = item[f]
if(item[f] > maxima[f]):
maxima[f] = item[f]
return minima,maxima
def EuclideanDistance(x,y):
S = 0 #The sum of the squared differences of the elements
for i in range(len(x)):
S += math.pow(x[i]-y[i],2)
return math.sqrt(S) #The square root of the sum
def ManhattanDistance(x,y):
S=0
for i in range(len(x)):
S += abs(x[i]-y[i])
return S
def InitializeMeans(items,k,cMin,cMax):
#Initialize means to random numbers between
#the min and max of each column/feature
f = len(items[0]) #number of features
means = [[0 for i in range(f)] for j in range(k)]
for mean in means:
for i in range(len(mean)):
#Set value to a random float
#(adding +-1 to avoid a wide placement of a mean)
mean[i] = uniform(cMin[i]+1,cMax[i]-1)
return means
https://www.chegg.com/homework-help/questions-and-answers/implement-k-means-clustering-algorithm-using-python-programming-language-given-f…
1/4
6/12/22, 4:35 PM

Implement The K-Means Clustering Algorithm Using P... | Chegg.com
def UpdateMean(n,mean,item):
Home Study tools

for i in range(len(mean)):

My courses

My books
My folder
Career
Life

m = mean[i]
m = (m*(n-1)+item[i])/float(n)
mean[i] = round(m,3)
return mean
def FindClusters(means,items):
clusters = [[] for i in range(len(means))] #Init clusters
for item in items:
#Classify item into a cluster
index = Classify(means,item,selection)
#Add item to cluster
clusters[index].append(item)
return clusters
###_Core Functions_###
def Classify(means,item,selection):
#Classify item to the mean with minimum distance
minimum = float('inf')
index = -1
for i in range(len(means)):
#Find distance from item to mean
if selection == 1:
dis = EuclideanDistance(item,means[i])
elif selection == 2:
dis = ManhattanDistance(item,means[i])
if(dis < minimum):
minimum = dis
index = i
return index
def CalculateMeans(k,items,maxIterations=100000):
#Find the minima and maxima for columns
cMin, cMax = FindColMinMax(items)
#Initialize means at random points
means = InitializeMeans(items,k,cMin,cMax)
#Initialize clusters, the array to hold
#the number of items in a class
clusterSizes = [0 for i in range(len(means))]
#An array to hold the cluster an item is in
belongsTo = [0 for i in range(len(items))]
#Calculate means
for e in range(maxIterations):
#If no change of cluster occurs, halt
noChange = True
for i in range(len(items)):
item = items[i]
#Classify item into a cluster and update the
#corresponding means.
index = Classify(means,item,selection)
clusterSizes[index] += 1
means[index] = UpdateMean(clusterSizes[index],means[index],item)
#Item changed cluster
if(index != belongsTo[i]):
noChange = False
belongsTo[i] = index
#Nothing changed, return
if(noChange):
break
https://www.chegg.com/homework-help/questions-and-answers/implement-k-means-clustering-algorithm-using-python-programming-language-given-f…
2/4
6/12/22, 4:35 PM
Implement The K-Means Clustering Algorithm Using P... | Chegg.com
return means


Home
Study tools

My courses

My books
My folder
Career
Life

if __name__ == "__main__":
# Input data
items = [[3,4],[3,6],[3,8],[4,5],[4,7],[5,1],[5,5],[7,3],[7,5],[8,5]]
k=3
print("Enetr 1 for euclidean distance and 2 for manhattam distance")
selection = int(input())
means = CalculateMeans(k,items,selection)
clusters = FindClusters(means,items)
print("fianl centroids of 3 clusters is : ",means)
print("Cluster 1 has ", clusters[0])
print("Cluster 2 has ", clusters[1])
print("Cluster 3 has ", clusters[2])
View comments (1)

output:
Questions viewed by other students
Q: Problem 5 Consider the single layer perceptron network shown in the figure below with inputs, xi € {0,1} and
activation function y = f(£xx •Wi + b) where f(2) = { wifiz-szige l1 otherwise X1 Wi W2 X2 (a) Provide values for wi, w2,
and b to use the perceptron for logical AND. (b) Provide values for wi, w2, and b to use the perceptron for logical OR.
(c) Can this perceptron be used...
A: See answer
100% (1 rating)
COMPANY
LEGAL & POLICIES
CHEGG PRODUCTS AND SERVICES
CHEGG NETWORK
CUSTOMER SERVICE
https://www.chegg.com/homework-help/questions-and-answers/implement-k-means-clustering-algorithm-using-python-programming-language-given-f…
3/4
6/12/22, 4:35 PM


Implement The K-Means Clustering Algorithm Using P... | Chegg.com
Home
Study tools

My courses

My books
My folder
Career
Life

© 2003-2022 Chegg Inc. All rights reserved.
https://www.chegg.com/homework-help/questions-and-answers/implement-k-means-clustering-algorithm-using-python-programming-language-given-f…
4/4
Download