def squaredClusteringErrors(inputs, k): """finds the total squared error from k-means clustering the inputs""" clusterer = KMeans(k) clusterer.train(inputs) means = clusterer.means assignments = list(map(clusterer.classify, inputs)) return sum(squaredDistance(input, means[cluster]) for input, cluster in zip(inputs, assignments))
def squaredClusteringErrors(inputs, k): """finds the total squared error from k-means clustering the inputs""" clusterer = KMeans(k) clusterer.train(inputs) means = clusterer.means assignments = list(map(clusterer.classify, inputs)) return sum( squaredDistance(input, means[cluster]) for input, cluster in zip(inputs, assignments))
def classify(self, input): """return the index of the fit closest to the input""" return min(range(self.k), key=lambda i: squaredDistance(input, self.means[i]))