コード例 #1
0
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))
コード例 #2
0
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))
コード例 #3
0
 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]))
コード例 #4
0
 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]))