# Path: skmultilearn/cluster/base.py
# class LabelSpaceClustererBase(BaseEstimator):
#     """An abstract base class for Label Space clustering
# 
#     Inherit it in your classifier according to`developer guide <../developer.ipynb>`_.
# 
#     """
# 
#     def __init__(self):
#         super(LabelSpaceClustererBase, self).__init__()
# 
#     def fit_predict(self, X, y):
#         """ Abstract method for clustering label space
# 
#         Implement it in your classifier according to`developer guide <../developer.ipynb>`_.
# 
#         Raises
#         ------
#         NotImplementedError
#             this is an abstract method
#         """
#         raise NotImplementedError("LabelSpaceClustererBase::fit_predict()")
# 
# Path: skmultilearn/cluster/helpers.py
# def _euclidean_distance(array1, array2):
#     """Returns the euclidean distance of two arrays
# 
#     Parameters
#     ----------
#     array1 : array of numbers
#         
#     array2 : array of numbers
# 
#     Returns
#     -------
#     distance : float
#         float with the euclidean distance, False if not possible
#     """
#     #Ensure that both arrays hava the same length
#     if len(array1) != len(array2):
#         return False
#     else:
#         distance = 0.0
#         for i in range(0, len(array1)):
#             distance = distance + pow(array1[i] - array2[i], 2)
#         distance = math.sqrt(distance)
#         return distance
# 
# def _recalculateCenters(y, balancedCluster, k):
# 
#     Centers = []
#     kAux = 0
#     while kAux < k:
#         vectorAux = np.zeros(len(y))
#         for i in range(0, len(balancedCluster)):
# 
#             if int(kAux) == int(balancedCluster[i]):
#                 #We have to fill the vector
#                 for j in range(0, len(y)):
#                     vectorAux[j] += y[j,i]
# 
#         vectorAux /= k
#         Centers.append(vectorAux)
#         kAux += 1
#     return Centers
# 
# def _countNumberOfAparitions(array, number):
#     """Number of aparitions of a number in an array
# 
#     Parameters
#     ----------
#     array : array of numbers
#         
#     number : number to search for
# 
#     Returns
#     -------
#     aparaitions : int
#         Number of aparitions of the number in the given array
#     """
#     aparitions = 0
#     for i in range(0, len(array)):
#         if array[i] == number:
#             aparitions += 1
#     return aparitions


import numpy as np
import random
from .base import LabelSpaceClustererBase
from .helpers import _euclidean_distance, _recalculateCenters, _countNumberOfAparitions
        Centers =[]
        y = y.todense()
        for i in range(0, self.k):
            auxVector = y[:, random.randint(0, number_of_labels-1)]
            Centers.append(np.asarray(auxVector))
        #Now we have the clusters created and we need to make each label its corresponding cluster
        
        while self.it > 0:
            balancedCluster = []
            for j in range(0, number_of_labels):
                auxVector = y[:,j]
                v = np.asarray(auxVector)
                #Now we calculate the distance and store it in an array
                distances = []
                for i in range(0, self.k):
                    #Store the distances
                    distances.append(_euclidean_distance(v, Centers[i]))
                finished = False
                while not finished:
                    minIndex = np.argmin(distances)
                    balancedCluster.append(minIndex)
                    #Now we have the cluster we want to add this label to
                    numberOfAparitions = _countNumberOfAparitions(balancedCluster, minIndex)
                    if float(numberOfAparitions) > (float(float(number_of_labels)/float(self.k))+1):
                        distances[minIndex] = float("inf")
                        balancedCluster.pop()
                    else:
                        finished = True

                

# Path: skmultilearn/cluster/base.py
# class LabelSpaceClustererBase(BaseEstimator):
#     """An abstract base class for Label Space clustering
# 
#     Inherit it in your classifier according to`developer guide <../developer.ipynb>`_.
# 
#     """
# 
#     def __init__(self):
#         super(LabelSpaceClustererBase, self).__init__()
# 
#     def fit_predict(self, X, y):
#         """ Abstract method for clustering label space
# 
#         Implement it in your classifier according to`developer guide <../developer.ipynb>`_.
# 
#         Raises
#         ------
#         NotImplementedError
#             this is an abstract method
#         """
#         raise NotImplementedError("LabelSpaceClustererBase::fit_predict()")
# 
# Path: skmultilearn/cluster/helpers.py
# def _euclidean_distance(array1, array2):
#     """Returns the euclidean distance of two arrays
# 
#     Parameters
#     ----------
#     array1 : array of numbers
#         
#     array2 : array of numbers
# 
#     Returns
#     -------
#     distance : float
#         float with the euclidean distance, False if not possible
#     """
#     #Ensure that both arrays hava the same length
#     if len(array1) != len(array2):
#         return False
#     else:
#         distance = 0.0
#         for i in range(0, len(array1)):
#             distance = distance + pow(array1[i] - array2[i], 2)
#         distance = math.sqrt(distance)
#         return distance
# 
# def _recalculateCenters(y, balancedCluster, k):
# 
#     Centers = []
#     kAux = 0
#     while kAux < k:
#         vectorAux = np.zeros(len(y))
#         for i in range(0, len(balancedCluster)):
# 
#             if int(kAux) == int(balancedCluster[i]):
#                 #We have to fill the vector
#                 for j in range(0, len(y)):
#                     vectorAux[j] += y[j,i]
# 
#         vectorAux /= k
#         Centers.append(vectorAux)
#         kAux += 1
#     return Centers
# 
# def _countNumberOfAparitions(array, number):
#     """Number of aparitions of a number in an array
# 
#     Parameters
#     ----------
#     array : array of numbers
#         
#     number : number to search for
# 
#     Returns
#     -------
#     aparaitions : int
#         Number of aparitions of the number in the given array
#     """
#     aparitions = 0
#     for i in range(0, len(array)):
#         if array[i] == number:
#             aparitions += 1
#     return aparitions


# Path: skmultilearn/cluster/balancedkmeans.py
import numpy as np
import random
from .base import LabelSpaceClustererBase
from .helpers import _euclidean_distance, _recalculateCenters, _countNumberOfAparitions

        Centers =[]
        y = y.todense()
        for i in range(0, self.k):
            auxVector = y[:, random.randint(0, number_of_labels-1)]
            Centers.append(np.asarray(auxVector))
        #Now we have the clusters created and we need to make each label its corresponding cluster
        
        while self.it > 0:
            balancedCluster = []
            for j in range(0, number_of_labels):
                auxVector = y[:,j]
                v = np.asarray(auxVector)
                #Now we calculate the distance and store it in an array
                distances = []
                for i in range(0, self.k):
                    #Store the distances
                    distances.append(_euclidean_distance(v, Centers[i]))
                finished = False
                while not finished:
                    minIndex = np.argmin(distances)
                    balancedCluster.append(minIndex)
                    #Now we have the cluster we want to add this label to
                    numberOfAparitions = _countNumberOfAparitions(balancedCluster, minIndex)
                    if float(numberOfAparitions) > (float(float(number_of_labels)/float(self.k))+1):
                        distances[minIndex] = float("inf")
                        balancedCluster.pop()
                    else:
                        finished = True

                

            Centers = _recalculateCenters(np.asarray(y), balancedCluster, self.k)