(self, datas, k, iter_count=10000, diff=0.00001)
return centers
k center count
k center count
k center count
def cluster(self, datas, k, iter_count=10000, diff=0.00001):
        """
            k center count 
        """
        if k > len(datas):
            return datas
        centers = self.rand_seed(datas, k)
        center_range = range(k)
        data_range = range(len(datas))
        sample_rate = 0.3
        sample_data_count = int(len(datas) * sample_rate)
        sample_data_range = range(sample_data_count)
        for _ in range(iter_count):
            sample_data = random.sample(datas, sample_data_count)
            distance_vector = [-1] * sample_data_count
            center_counts = [0] * k
            for i in sample_data_range:
                mini_distance, bestlabel = min(
                    [
                        (
                            self.distance(
                                datas[i],
                                centers[j]
                            ), j
                        )
                        for j in center_range
                    ]
                )
                distance_vector[i] = bestlabel
            for i in sample_data_range:
                data_label = distance_vector[i]
                center_counts[data_label] += 1
                eta = 1.0 / center_counts[data_label]
                centers[data_label] = self.add(
                    centers[data_label],
                    sample_data[i],
                    eta,
                    len(sample_data[i])
                )
        return centers
def cluster ( self , datas , k , iter_count = 10000 , diff = 0.00001 ) : if k > len ( datas ) : return datas centers = self . rand_seed ( datas , k ) center_range = range ( k ) data_range = range ( len ( datas ) ) sample_rate = 0.3 sample_data_count = int ( len ( datas ) * sample_rate ) sample_data_range = range ( sample_data_count ) for _ in range ( iter_count ) : sample_data = random . sample ( datas , sample_data_count ) distance_vector = [ - 1 ] * sample_data_count center_counts = [ 0 ] * k for i in sample_data_range : mini_distance , bestlabel = min ( [ ( self . distance ( datas [ i ] , centers [ j ] ) , j ) for j in center_range ] ) distance_vector [ i ] = bestlabel for i in sample_data_range : data_label = distance_vector [ i ] center_counts [ data_label ] += 1 eta = 1.0 / center_counts [ data_label ] centers [ data_label ] = self . add ( centers [ data_label ] , sample_data [ i ] , eta , len ( sample_data [ i ] ) ) return centers