jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def build_single_GMM(k, C, reg_covar):
	'''
	Fit a gaussian mixture model

	Parameters
	----------
	k : int
		Number of components
	C : array
		Feature data
	reg_covar : float
		Regularization of the covariance matrix
	'''
	np.random.seed()
	gmm = s.GaussianMixture(
		n_components=k,
		covariance_type='full',
		tol=0.001, # orig 0.001
		reg_covar=reg_covar,
		max_iter=10000, # orig 10000
		n_init=10,
		init_params='kmeans',
		weights_init=None,
		means_init=None,
		precisions_init=None,
		random_state=None,
		warm_start=False,
		verbose=0,
		verbose_interval=10) # create model
	return gmm.fit(C)
//  [python]
def build_single_GMM(k, C, reg_covar):
	'''
	Fit a gaussian mixture model

	Parameters
	----------
	k : int
		Number of components
	C : array
		Feature data
	reg_covar : float
		Regularization of the covariance matrix
	'''
	np.random.seed()
	gmm = smix.GaussianMixture(
		n_components=k,
		covariance_type='full',
		tol=0.001, # orig 0.001
		reg_covar=reg_covar,
		max_iter=10000, # orig 10000
		n_init=10,
		init_params='kmeans',
		weights_init=None,
		means_init=None,
		precisions_init=None,
		random_state=None,
		warm_start=False,
		verbose=0,
		verbose_interval=10) # create model
	return gmm.fit(C)
//  [python]
def generatePopulation(self, totalNumberOfIterations):
		""" Genererating a population of items (items' content and initial prominence).

		"""

		random.seed(self.seed)
		np.random.seed(self.seed)

		# Compute number of total items in the simulation
		self.totalNumberOfItems = totalNumberOfIterations*self.numberOfNewItemsPI                    
		self.percentageOfActiveItems = self.numberOfNewItemsPI/self.totalNumberOfItems

		# Apply GMM on items/articles from the BBC data
		R, S = [5,1,6,7], [5,2,28,28]
		r = int(random.random()*4)
		(X,labels,topicClasses) = pickle.load(open('BBC data/t-SNE-projection'+str(R[r])+'.pkl','rb'))
		gmm = mixture.GaussianMixture(n_components=5, random_state=S[r]).fit(X)
		
		# Normalize topic weights to sum into 1 (CBF)
		self.topicsFrequency = [np.round(i,decimals=1) for i in self.topicsFrequency/np.sum(self.topicsFrequency)]
		
		# Generate items/articles from the BBC data projection
		samples_, classes_ = gmm.sample(self.totalNumberOfItems*10)
		for c, category in enumerate(self.topics):
			selection = samples_[np.where(classes_ == c)][:int(self.topicsFrequency[c]*self.totalNumberOfItems)]
			if len(self.Items) == 0:
				self.Items = np.array(selection)
			else:
				self.Items = np.append(self.Items, selection, axis=0)
			self.ItemsClass+=[c for i in range(len(selection))]
		self.ItemsClass = np.array(self.ItemsClass)
		self.ItemsFeatures = gmm.predict_proba(self.Items)
		self.Items = self.Items/55  # Scale down to -1, 1 range
		
		# Cosine distance between item features
		self.ItemsDistances = spatial.distance.cdist(self.ItemsFeatures, self.ItemsFeatures, metric='cosine')

		# Generate a random order of item availability
		self.ItemsOrderOfAppearance = np.arange(self.totalNumberOfItems).tolist()
		random.shuffle(self.ItemsOrderOfAppearance)
	
		# Initial prominence
		self.initialProminceZ0()
		self.ItemProminence = self.ItemsInitialProminence.copy()

		# Lifespan, item age
		self.ItemLifespan = np.ones(self.totalNumberOfItems)

		# Has been recommended before
		self.hasBeenRecommended = np.zeros(self.totalNumberOfItems)
//  [python]
def generatePopulation(self, totalNumberOfIterations):
		""" Genererating a population of items (items' content and initial prominence).

		"""

		random.seed(self.seed)
		np.random.seed(self.seed)

		# Compute number of total items in the simulation
		self.totalNumberOfItems = totalNumberOfIterations*self.numberOfNewItemsPI                    
		self.percentageOfActiveItems = self.numberOfNewItemsPI/self.totalNumberOfItems

		# Apply GMM on items/articles from the BBC data
		R, S = [5,1,6,7], [5,2,28,28]
		r = int(random.random()*4)
		(X,labels,topicClasses) = pickle.load(open('BBC data/t-SNE-projection'+str(R[r])+'.pkl','rb'))
		gmm = GaussianMixture(n_components=5, random_state=S[r]).fit(X)
		
		# Normalize topic weights to sum into 1 (CBF)
		self.topicsFrequency = [np.round(i,decimals=1) for i in self.topicsFrequency/np.sum(self.topicsFrequency)]
		
		# Generate items/articles from the BBC data projection
		samples_, classes_ = gmm.sample(self.totalNumberOfItems*10)
		for c, category in enumerate(self.topics):
			selection = samples_[np.where(classes_ == c)][:int(self.topicsFrequency[c]*self.totalNumberOfItems)]
			if len(self.Items) == 0:
				self.Items = np.array(selection)
			else:
				self.Items = np.append(self.Items, selection, axis=0)
			self.ItemsClass+=[c for i in range(len(selection))]
		self.ItemsClass = np.array(self.ItemsClass)
		self.ItemsFeatures = gmm.predict_proba(self.Items)
		self.Items = self.Items/55  # Scale down to -1, 1 range
		
		# Cosine distance between item features
		self.ItemsDistances = spatial.distance.cdist(self.ItemsFeatures, self.ItemsFeatures, metric='cosine')

		# Generate a random order of item availability
		self.ItemsOrderOfAppearance = np.arange(self.totalNumberOfItems).tolist()
		random.shuffle(self.ItemsOrderOfAppearance)
	
		# Initial prominence
		self.initialProminceZ0()
		self.ItemProminence = self.ItemsInitialProminence.copy()

		# Lifespan, item age
		self.ItemLifespan = np.ones(self.totalNumberOfItems)

		# Has been recommended before
		self.hasBeenRecommended = np.zeros(self.totalNumberOfItems)
//  [python]
def fit(self, X, Y=None):
    if self.method == 'random':
      N = len(X)
      idx = np.random.randint(N, size=self.M)
      self.samples = X[idx]
    elif self.method == 'normal':
      # just sample from N(0,1)
      D = X.shape[1]
      self.samples = np.random.randn(self.M, D) / np.sqrt(D)
    elif self.method == 'kmeans':
      X, Y = self._subsample_data(X, Y)

      print("Fitting kmeans...")
      t0 = datetime.now()
      kmeans = KMeans(n_clusters=len(set(Y)))
      kmeans.fit(X)
      print("Finished fitting kmeans, duration:", datetime.now() - t0)

      # calculate the most ambiguous points
      # we will do this by finding the distance between each point
      # and all cluster centers
      # and return which points have the smallest variance
      dists = kmeans.transform(X) # returns an N x K matrix
      variances = dists.var(axis=1)
      idx = np.argsort(variances) # smallest to largest
      idx = idx[:self.M]
      self.samples = X[idx]
    elif self.method == 'gmm':
      X, Y = self._subsample_data(X, Y)

      print("Fitting GMM")
      t0 = datetime.now()
      gmm = mixture.GMM(
        n_components=len(set(Y)),
        covariance_type='spherical',
        reg_covar=1e-6)
      gmm.fit(X)
      print("Finished fitting GMM, duration:", datetime.now() - t0)

      # calculate the most ambiguous points
      probs = gmm.predict_proba(X)
      ent = stats.entropy(probs.T) # N-length vector of entropies
      idx = np.argsort(-ent) # negate since we want biggest first
      idx = idx[:self.M]
      self.samples = X[idx]
    return self
//  [python]
def fit(self, X, Y=None):
    if self.method == 'random':
      N = len(X)
      idx = np.random.randint(N, size=self.M)
      self.samples = X[idx]
    elif self.method == 'normal':
      # just sample from N(0,1)
      D = X.shape[1]
      self.samples = np.random.randn(self.M, D) / np.sqrt(D)
    elif self.method == 'kmeans':
      X, Y = self._subsample_data(X, Y)

      print("Fitting kmeans...")
      t0 = datetime.now()
      kmeans = KMeans(n_clusters=len(set(Y)))
      kmeans.fit(X)
      print("Finished fitting kmeans, duration:", datetime.now() - t0)

      # calculate the most ambiguous points
      # we will do this by finding the distance between each point
      # and all cluster centers
      # and return which points have the smallest variance
      dists = kmeans.transform(X) # returns an N x K matrix
      variances = dists.var(axis=1)
      idx = np.argsort(variances) # smallest to largest
      idx = idx[:self.M]
      self.samples = X[idx]
    elif self.method == 'gmm':
      X, Y = self._subsample_data(X, Y)

      print("Fitting GMM")
      t0 = datetime.now()
      gmm = GaussianMixture(
        n_components=len(set(Y)),
        covariance_type='spherical',
        reg_covar=1e-6)
      gmm.fit(X)
      print("Finished fitting GMM, duration:", datetime.now() - t0)

      # calculate the most ambiguous points
      probs = gmm.predict_proba(X)
      ent = stats.entropy(probs.T) # N-length vector of entropies
      idx = np.argsort(-ent) # negate since we want biggest first
      idx = idx[:self.M]
      self.samples = X[idx]
    return self
//  [python]
def im_weights_gmm(z_s, y_s, z_t, self):
    # Clustering target samples in the latent space and extracting prototypes (means of clusters)
    gmm = mixture.GaussianMixture(n_components=self.n_class, n_init=20)
    y_t = gmm.fit_predict(z_t)

    mean_mat_S, num_in_class_S = extract_prototypes(z_s, y_s, self.n_class)
    mean_mat_T, num_in_class_T = extract_prototypes(z_t, y_t, self.n_class)

    # We assume that prototypes of classes have been transported in some in the feature space
    M = ot.dist(mean_mat_S, mean_mat_T)
    M /= M.max()
    a = np.ones((self.n_class,)) / self.n_class
    b = np.ones((self.n_class,)) / self.n_class

    gamma = ot.emd(a, b, M)
    proportion_T = num_in_class_T / np.sum(num_in_class_T)
    assignement_source_to_target = gamma.argmax(axis=1)

    # proportions are arranged directly per class
    proportion_T = torch.from_numpy(proportion_T[assignement_source_to_target]).float()
    if self.logger is not None:
        self.logger.info(f"GMM proportion_T: {proportion_T}, assignment_S2T: {assignement_source_to_target}")

    return proportion_T
//  [python]
def im_weights_gmm(z_s, y_s, z_t, self):
    # Clustering target samples in the latent space and extracting prototypes (means of clusters)
    gmm = GaussianMixture(n_components=self.n_class, n_init=20)
    y_t = gmm.fit_predict(z_t)

    mean_mat_S, num_in_class_S = extract_prototypes(z_s, y_s, self.n_class)
    mean_mat_T, num_in_class_T = extract_prototypes(z_t, y_t, self.n_class)

    # We assume that prototypes of classes have been transported in some in the feature space
    M = ot.dist(mean_mat_S, mean_mat_T)
    M /= M.max()
    a = np.ones((self.n_class,)) / self.n_class
    b = np.ones((self.n_class,)) / self.n_class

    gamma = ot.emd(a, b, M)
    proportion_T = num_in_class_T / np.sum(num_in_class_T)
    assignement_source_to_target = gamma.argmax(axis=1)

    # proportions are arranged directly per class
    proportion_T = torch.from_numpy(proportion_T[assignement_source_to_target]).float()
    if self.logger is not None:
        self.logger.info(f"GMM proportion_T: {proportion_T}, assignment_S2T: {assignement_source_to_target}")

    return proportion_T
//  [python]
def project_gmm(gmm1, Tmat):
	'''
	Project gaussian mixture model into lower-dimensional space
	m = number of cells, n = original dimensionality

	Parameters
	----------
	gmm1: obj
	    sklearn GaussianMixture object
	Tmat : array
	    transformation matrix. n x newdim
	'''

	# get number of new dimensions
	ndims = Tmat.shape[1]

	# get parameters from the gmm: 
	means_ = gmm1.means_
	covariances_ = gmm1.covariances_
	weights_ = gmm1.weights_
	lower_bound_ = gmm1.lower_bound_ 

	# Transform gmm parameters and make a projected GMM
	r = len(weights_)
	projmeans_ = means_ @ Tmat
	projcovariances_ = np.ndarray([r,ndims,ndims])
	projprecisions_ = np.ndarray([r,ndims,ndims])
	projprecisions_chol_ = np.ndarray([r,ndims,ndims])
	allposdef=True
	for j in range(r):
		currcov = covariances_[j,:,:]
		newcov =  Tmat.T @currcov @ Tmat
		projcovariances_[j,:,:] = newcov

		if not check_posdef(currcov): 
			allposdef = False
		else:
			newprecision = np.linalg.inv(newcov)
			projprecisions_[j,:,:] = newprecision
			projprecisions_chol_[j,:,:] = np.matrix(linalg.cholesky(newprecision)).H

	if not allposdef: 
		return None # return a null object
	else: 
		newgmm = s.GaussianMixture(
			n_components = r,
			covariance_type='full',
			tol=0.001,
			reg_covar=False,
			max_iter=1,
			n_init=1,
			init_params='kmeans',
			weights_init=weights_,
			means_init=projmeans_,
			precisions_init=projprecisions_,
			random_state=None,
			warm_start=False,
			verbose=0,
			verbose_interval=10)
		best_params = (weights_, projmeans_, projcovariances_, projprecisions_chol_)
		newgmm._set_parameters(best_params)
		newgmm.n_iter_ = 1
		newgmm.lower_bound_ = gmm1.lower_bound_
		newgmm.covariance_type = 'full'
	    
	return newgmm
//  [python]
def project_gmm(gmm1, Tmat):
	'''
	Project gaussian mixture model into lower-dimensional space
	m = number of cells, n = original dimensionality

	Parameters
	----------
	gmm1: obj
	    sklearn GaussianMixture object
	Tmat : array
	    transformation matrix. n x newdim
	'''

	# get number of new dimensions
	ndims = Tmat.shape[1]

	# get parameters from the gmm: 
	means_ = gmm1.means_
	covariances_ = gmm1.covariances_
	weights_ = gmm1.weights_
	lower_bound_ = gmm1.lower_bound_ 

	# Transform gmm parameters and make a projected GMM
	r = len(weights_)
	projmeans_ = means_ @ Tmat
	projcovariances_ = np.ndarray([r,ndims,ndims])
	projprecisions_ = np.ndarray([r,ndims,ndims])
	projprecisions_chol_ = np.ndarray([r,ndims,ndims])
	allposdef=True
	for j in range(r):
		currcov = covariances_[j,:,:]
		newcov =  Tmat.T @currcov @ Tmat
		projcovariances_[j,:,:] = newcov

		if not check_posdef(currcov): 
			allposdef = False
		else:
			newprecision = np.linalg.inv(newcov)
			projprecisions_[j,:,:] = newprecision
			projprecisions_chol_[j,:,:] = np.matrix(linalg.cholesky(newprecision)).H

	if not allposdef: 
		return None # return a null object
	else: 
		newgmm = smix.GaussianMixture(
			n_components = r,
			covariance_type='full',
			tol=0.001,
			reg_covar=False,
			max_iter=1,
			n_init=1,
			init_params='kmeans',
			weights_init=weights_,
			means_init=projmeans_,
			precisions_init=projprecisions_,
			random_state=None,
			warm_start=False,
			verbose=0,
			verbose_interval=10)
		best_params = (weights_, projmeans_, projcovariances_, projprecisions_chol_)
		newgmm._set_parameters(best_params)
		newgmm.n_iter_ = 1
		newgmm.lower_bound_ = gmm1.lower_bound_
		newgmm.covariance_type = 'full'
	    
	return newgmm
//  [python]
def trainGMM(self, nClasses=2, covType='spherical', maxIts=20):
        model = mixture.GaussianMixture(n_components=nClasses, covariance_type=covType, max_iter=maxIts)
        model.means_init = np.array([self.train[self.trainTgt == i].mean(axis=0) for i in range(nClasses)])
        model.fit(self.train, self.trainTgt)

        trainOut = model.predict(self.train)
        trainError = np.mean(self.trainTgt.ravel() == trainOut.ravel()) * 100
        print("Training Error: ", trainError)

        return model
//  [python]
def trainGMM(self, nClasses=2, covType='spherical', maxIts=20):
        model = GaussianMixture(n_components=nClasses, covariance_type=covType, max_iter=maxIts)
        model.means_init = np.array([self.train[self.trainTgt == i].mean(axis=0) for i in range(nClasses)])
        model.fit(self.train, self.trainTgt)

        trainOut = model.predict(self.train)
        trainError = np.mean(self.trainTgt.ravel() == trainOut.ravel()) * 100
        print("Training Error: ", trainError)

        return model
//  [python]
def CEM(
    fitness_function,
    prior_particles,
    alpha=0.1,
    qtile=0.25,
    verbose=True,
    gmm_components=None,
    max_iter=30,
):
    """
    Run CEM with quantile bounding which maximizes fitness function.
    :param fitness_function: a function from [particle x param_value] -> [fitness]
    :param prior_particles: a matrix of [particle x param_value]
    :param alpha: the low pass filter alpha on the update of the quantile and posterior distribution
    :param qtile: the quantile of fit samples to include
    :param verbose: whether to print out debugging information
    :param gmm_components: number of kernels in the GMM
    :param max_iter: maximum number of iterations for fitting the GMM at each CEM step
    """
    gmm_verbosity = 2 if verbose else 0
    model = mixture.GaussianMixture(
        n_components=gmm_components,
        n_init=5,
        warm_start=True,
        verbose=gmm_verbosity,
        max_iter=max_iter,
    )
    cur_bound = float("-inf")
    mean = None
    var = None
    while True:
        # evaluate fitness
        fitnesses = fitness_function(prior_particles)
        elite_idxs = fitnesses >= cur_bound
        elite_samples = prior_particles[elite_idxs, :]
        elite_fitnesses = fitnesses[elite_idxs]
        if elite_fitnesses != np.array([]):
            quantile_fitnesses = np.quantile(elite_fitnesses, [1 - qtile])[0]
            if cur_bound != float("-inf"):
                cur_bound = alpha * quantile_fitnesses + \
                    (1 - alpha) * cur_bound
            else:
                cur_bound = quantile_fitnesses
            if gmm_components is None:
                cur_mean = np.mean(elite_samples, axis=0)
                cur_var = np.var(elite_samples, axis=0)
                if mean is not None:
                    mean = alpha * cur_mean + (1 - alpha) * mean
                    var = alpha * cur_var + (1 - alpha) * var
                else:
                    mean = cur_mean
                    var = cur_var
                if verbose:
                    print(f"Posterior N({mean},{var}), Bound: {cur_bound}")
            else:
                model.fit(elite_samples)
        if gmm_components is None:
            prior_particles = np.random.multivariate_normal(
                mean, np.diag(var), size=prior_particles.shape[0]
            )
        else:
            # ensure the sum of weights never exceeds 1
            model.weights_ = model.weights_ / np.sum(model.weights_)
            prior_particles = model.sample(prior_particles.shape[0])[0]
            prior_particles[:,3:7] /= np.linalg.norm(prior_particles[:,3:7],axis=1,keepdims=True)
        yield prior_particles
//  [python]
def CEM(
    fitness_function,
    prior_particles,
    alpha=0.1,
    qtile=0.25,
    verbose=True,
    gmm_components=None,
    max_iter=30,
):
    """
    Run CEM with quantile bounding which maximizes fitness function.
    :param fitness_function: a function from [particle x param_value] -> [fitness]
    :param prior_particles: a matrix of [particle x param_value]
    :param alpha: the low pass filter alpha on the update of the quantile and posterior distribution
    :param qtile: the quantile of fit samples to include
    :param verbose: whether to print out debugging information
    :param gmm_components: number of kernels in the GMM
    :param max_iter: maximum number of iterations for fitting the GMM at each CEM step
    """
    gmm_verbosity = 2 if verbose else 0
    model = GaussianMixture(
        n_components=gmm_components,
        n_init=5,
        warm_start=True,
        verbose=gmm_verbosity,
        max_iter=max_iter,
    )
    cur_bound = float("-inf")
    mean = None
    var = None
    while True:
        # evaluate fitness
        fitnesses = fitness_function(prior_particles)
        elite_idxs = fitnesses >= cur_bound
        elite_samples = prior_particles[elite_idxs, :]
        elite_fitnesses = fitnesses[elite_idxs]
        if elite_fitnesses != np.array([]):
            quantile_fitnesses = np.quantile(elite_fitnesses, [1 - qtile])[0]
            if cur_bound != float("-inf"):
                cur_bound = alpha * quantile_fitnesses + \
                    (1 - alpha) * cur_bound
            else:
                cur_bound = quantile_fitnesses
            if gmm_components is None:
                cur_mean = np.mean(elite_samples, axis=0)
                cur_var = np.var(elite_samples, axis=0)
                if mean is not None:
                    mean = alpha * cur_mean + (1 - alpha) * mean
                    var = alpha * cur_var + (1 - alpha) * var
                else:
                    mean = cur_mean
                    var = cur_var
                if verbose:
                    print(f"Posterior N({mean},{var}), Bound: {cur_bound}")
            else:
                model.fit(elite_samples)
        if gmm_components is None:
            prior_particles = np.random.multivariate_normal(
                mean, np.diag(var), size=prior_particles.shape[0]
            )
        else:
            # ensure the sum of weights never exceeds 1
            model.weights_ = model.weights_ / np.sum(model.weights_)
            prior_particles = model.sample(prior_particles.shape[0])[0]
            prior_particles[:,3:7] /= np.linalg.norm(prior_particles[:,3:7],axis=1,keepdims=True)
        yield prior_particles
//  [python]
def fit(self, X, y):
        X = self._check_X(X)
        y = np.asarray(y)

        n_samples, n_features = X.shape

        if n_samples != y.shape[0]:
            raise ValueError("X and y have incompatible shapes")

        self.classes_ = np.unique(y)
        self.classes_.sort()
        unique_y = self.classes_

        n_classes = unique_y.shape[0]

        if self.n_components.size not in (1, len(unique_y)):
            raise ValueError("n_components must be compatible with "
                             "the number of classes")

        self.gmms_ = [None for i in range(n_classes)]
        self.class_prior_ = np.zeros(n_classes)

        n_comp = np.zeros(len(self.classes_), dtype=int) + self.n_components

        for i, y_i in enumerate(unique_y):
            if n_comp[i] > X[y == y_i].shape[0]:
                warnstr = ("Expected n_samples >= n_components but got "
                           "n_samples={0}, n_components={1}, "
                           "n_components set to {0}.")
                warnings.warn(warnstr.format(X[y == y_i].shape[0], n_comp[i]))
                n_comp[i] = X[y == y_i].shape[0]
            self.gmms_[i] = mixture.GMM(n_comp[i], **self.kwargs).fit(X[y == y_i])
            self.class_prior_[i] = float(np.sum(y == y_i)) / n_samples

        return self
//  [python]
def fit(self, X, y):
        X = self._check_X(X)
        y = np.asarray(y)

        n_samples, n_features = X.shape

        if n_samples != y.shape[0]:
            raise ValueError("X and y have incompatible shapes")

        self.classes_ = np.unique(y)
        self.classes_.sort()
        unique_y = self.classes_

        n_classes = unique_y.shape[0]

        if self.n_components.size not in (1, len(unique_y)):
            raise ValueError("n_components must be compatible with "
                             "the number of classes")

        self.gmms_ = [None for i in range(n_classes)]
        self.class_prior_ = np.zeros(n_classes)

        n_comp = np.zeros(len(self.classes_), dtype=int) + self.n_components

        for i, y_i in enumerate(unique_y):
            if n_comp[i] > X[y == y_i].shape[0]:
                warnstr = ("Expected n_samples >= n_components but got "
                           "n_samples={0}, n_components={1}, "
                           "n_components set to {0}.")
                warnings.warn(warnstr.format(X[y == y_i].shape[0], n_comp[i]))
                n_comp[i] = X[y == y_i].shape[0]
            self.gmms_[i] = GaussianMixture(n_comp[i], **self.kwargs).fit(X[y == y_i])
            self.class_prior_[i] = float(np.sum(y == y_i)) / n_samples

        return self
//  [python]
def gmmtry(data):
	if(data.shape[0]>200000):
		choice=np.random.choice(np.arange(data.shape[0]),200000)
		data_=data[choice]
	else:
		data_=data
	gmmrange=range(1,max(2,min(10,int(data.shape[0]/2))))
	gmms=[sklearn.mixture.GMM(n_components=i) for i in gmmrange]
	[gmm.fit(data_) for gmm in gmms]
	bics=[gmm.bic(data_) for gmm in gmms]
	return gmms[np.argmin(bics)]
//  [python]
def gmmtry(data):
	if(data.shape[0]>200000):
		choice=np.random.choice(np.arange(data.shape[0]),200000)
		data_=data[choice]
	else:
		data_=data
	gmmrange=range(1,max(2,min(10,int(data.shape[0]/2))))
	gmms=[sklearn.mixture.GaussianMixture(n_components=i) for i in gmmrange]
	[gmm.fit(data_) for gmm in gmms]
	bics=[gmm.bic(data_) for gmm in gmms]
	return gmms[np.argmin(bics)]
//  [python]
data = np.array([-67, -48, 6, 8, 14, 16, 23, 24, 28, 29, 41, 49, 56, 60, 75]).reshape(-1, 1)

# 设置n_components=2，表示两个分量高斯混合模型
gmm_model = mixture.GaussianMixture(n_components=2)
//  [python]
data = np.array([-67, -48, 6, 8, 14, 16, 23, 24, 28, 29, 41, 49, 56, 60, 75]).reshape(-1, 1)

# 设置n_components=2，表示两个分量高斯混合模型
gmm_model = GaussianMixture(n_components=2)
//  [python]
def gmm_policy(scores, given_gt_thr=0.9):
        """The policy of choosing pseudo label.

        The previous GMM-B policy is used as default.
        1. Use the predicted bbox to fit a GMM with 2 center.
        2. Find the predicted bbox belonging to the positive
            cluster with highest GMM probability.
        3. Take the class score of the finded bbox as gt_thr.

        Args:
            scores (nd.array): The scores.

        Returns:
            float: Found gt_thr.

        """
        if len(scores) < 2:
            return given_gt_thr
        if isinstance(scores, torch.Tensor):
            scores = scores.cpu().numpy()
        if len(scores.shape) == 1:
            scores = scores[:, np.newaxis]
        means_init = [[np.min(scores)], [np.max(scores)]]
        weights_init = [1 / 2, 1 / 2]
        precisions_init = [[[1.0]], [[1.0]]]
        gmm = s.GMM(
            2,
            weights_init=weights_init,
            means_init=means_init,
            precisions_init=precisions_init)
        gmm.fit(scores)
        means = gmm.means_
        weights = gmm.weights_
        vars = np.sqrt(gmm.covars_[..., 0])
        params = np.concatenate([means, vars, weights.reshape(-1, 1)], 1)
        return params, gmm.predict_proba(scores)
//  [python]
def gmm_policy(scores, given_gt_thr=0.9):
        """The policy of choosing pseudo label.

        The previous GMM-B policy is used as default.
        1. Use the predicted bbox to fit a GMM with 2 center.
        2. Find the predicted bbox belonging to the positive
            cluster with highest GMM probability.
        3. Take the class score of the finded bbox as gt_thr.

        Args:
            scores (nd.array): The scores.

        Returns:
            float: Found gt_thr.

        """
        if len(scores) < 2:
            return given_gt_thr
        if isinstance(scores, torch.Tensor):
            scores = scores.cpu().numpy()
        if len(scores.shape) == 1:
            scores = scores[:, np.newaxis]
        means_init = [[np.min(scores)], [np.max(scores)]]
        weights_init = [1 / 2, 1 / 2]
        precisions_init = [[[1.0]], [[1.0]]]
        gmm = skm.GaussianMixture(
            2,
            weights_init=weights_init,
            means_init=means_init,
            precisions_init=precisions_init)
        gmm.fit(scores)
        means = gmm.means_
        weights = gmm.weights_
        vars = np.sqrt(gmm.covariances_[..., 0])
        params = np.concatenate([means, vars, weights.reshape(-1, 1)], 1)
        return params, gmm.predict_proba(scores)
//  [python]
def do_gmixture(self, n_components=2, seed=42):
        """
        Estimate parameters of a Gaussian mixture distribution of (parent, child) data.

        Args:
            n_components (int): Number of components in GMM.
            seed (int): Random seed.

        Returns:
            gmm_output (dict): Predicted labels of (parent, child) pairs and predicted posterior probabilities of each component.

        """

        np.random.seed(seed)
        gmm = mixture.GaussianMixture(n_components=n_components, random_state=seed, covariance_type='full')

        gmm.fit(self.X)
        gmm_labels = gmm.predict(self.X)
        gmm_prob = gmm.predict_proba(self.X)[:,0]
        gmm_output = {'gmm_labels': gmm_labels, 'gmm_prob': gmm_prob}

        return gmm_output
//  [python]
def do_gmixture(self, n_components=2, seed=42):
        """
        Estimate parameters of a Gaussian mixture distribution of (parent, child) data.

        Args:
            n_components (int): Number of components in GMM.
            seed (int): Random seed.

        Returns:
            gmm_output (dict): Predicted labels of (parent, child) pairs and predicted posterior probabilities of each component.

        """

        np.random.seed(seed)
        gmm = GaussianMixture(n_components=n_components, random_state=seed, covariance_type='full')

        gmm.fit(self.X)
        gmm_labels = gmm.predict(self.X)
        gmm_prob = gmm.predict_proba(self.X)[:,0]
        gmm_output = {'gmm_labels': gmm_labels, 'gmm_prob': gmm_prob}

        return gmm_output
//  [python]
def fit_mixture(X=None, n_clusters=2):
    """**Gaussian Mixture Model**

    Performs a polynomial regression of given order.

    Parameters
    ----------
    X : Union[list, np.array, pd.Series]
        The values to classify.
    n_clusters : int
        Number of components to look for.

    Returns
    -------
    pd.DataFrame
        DataFrame containing the probability of belonging to each cluster.
    dict
        Dictionary containing additional information such as the parameters (:func:`.n_clusters`).

    See Also
    ----------
    signal_detrend, fit_error

    Examples
    ---------
    .. ipython:: python

      import pandas as pd
      import neurokit2 as nk

      x = nk.signal_simulate()
      probs, info = nk.fit_mixture(x, n_clusters=2)  # Rmb to merge with main to return ``info``
      @savefig p_fit_mixture.png scale=100%
      fig = nk.signal_plot([x, probs["Cluster_0"], probs["Cluster_1"]], standardize=True)
      @suppress
      plt.close()

    """
    if X.ndim == 1:
        X = X.reshape(-1, 1)

    # fit a Gaussian Mixture Model with two components
    clf = sklearn.mixture.GMM(n_components=n_clusters, random_state=333)
    clf = clf.fit(X)

    # Get predicted probabilities
    predicted = clf.predict_proba(X)
    probabilities = pd.DataFrame(predicted).add_prefix("Cluster_")

    return probabilities, {"n_clusters": n_clusters}
//  [python]
def fit_mixture(X=None, n_clusters=2):
    """**Gaussian Mixture Model**

    Performs a polynomial regression of given order.

    Parameters
    ----------
    X : Union[list, np.array, pd.Series]
        The values to classify.
    n_clusters : int
        Number of components to look for.

    Returns
    -------
    pd.DataFrame
        DataFrame containing the probability of belonging to each cluster.
    dict
        Dictionary containing additional information such as the parameters (:func:`.n_clusters`).

    See Also
    ----------
    signal_detrend, fit_error

    Examples
    ---------
    .. ipython:: python

      import pandas as pd
      import neurokit2 as nk

      x = nk.signal_simulate()
      probs, info = nk.fit_mixture(x, n_clusters=2)  # Rmb to merge with main to return ``info``
      @savefig p_fit_mixture.png scale=100%
      fig = nk.signal_plot([x, probs["Cluster_0"], probs["Cluster_1"]], standardize=True)
      @suppress
      plt.close()

    """
    if X.ndim == 1:
        X = X.reshape(-1, 1)

    # fit a Gaussian Mixture Model with two components
    clf = sklearn.mixture.GaussianMixture(n_components=n_clusters, random_state=333)
    clf = clf.fit(X)

    # Get predicted probabilities
    predicted = clf.predict_proba(X)
    probabilities = pd.DataFrame(predicted).add_prefix("Cluster_")

    return probabilities, {"n_clusters": n_clusters}
//  [python]
def test_labels_match_clustering(case, labels):
    """
    train a GMM, assert predictions match labels
    """
    # should get 100% accuracy since the two clusters are super different
    latent = case.latent_left_
    gmm = mixture.GaussianMixture(n_components=2)
    predicted_labels = gmm.fit_predict(latent)
    assert np.array_equal(predicted_labels, labels) or np.array_equal(
        1 - predicted_labels, labels
    )
//  [python]
def test_labels_match_clustering(case, labels):
    """
    train a GMM, assert predictions match labels
    """
    # should get 100% accuracy since the two clusters are super different
    latent = case.latent_left_
    gmm = GaussianMixture(n_components=2)
    predicted_labels = gmm.fit_predict(latent)
    assert np.array_equal(predicted_labels, labels) or np.array_equal(
        1 - predicted_labels, labels
    )
//  [python]
def build_GMM(self, num_gmm_sets=3, fig=False, name="Local Density"):
        target = self.local_ds
        gmm1 = mixture.GaussianMixture(
            n_components=num_gmm_sets,
            max_iter=20,
            tol=1e-2,
            reg_covar=5e-7,
            random_state=1,
        )

        gmm1.fit(target)
        pred = gmm1.predict(target)
        info_idx = pred[target.argmin()]
        high_idx = pred[target.argmax()]
        high_target = (pred == high_idx).nonzero()[0]
        info_target = (pred == info_idx).nonzero()[0]

        if num_gmm_sets == 3:
            uncertain_idx = 3 - info_idx - high_idx
            uncertain_target = (pred == uncertain_idx).nonzero()[0]

        if fig and dist.get_rank() == 0:
            plt.hist(
                target[high_target],
                bins=200,
                range=(0.0, 1.0),
                edgecolor="black",
                alpha=0.5,
                label=f"High {name}",
            )
            plt.hist(
                target[info_target],
                bins=200,
                range=(0.0, 1.0),
                edgecolor="black",
                alpha=0.5,
                label=f"Informative {name}",
            )
            if num_gmm_sets == 3:
                plt.hist(
                    target[uncertain_target],
                    bins=200,
                    range=(0.0, 1.0),
                    edgecolor="black",
                    alpha=0.5,
                    label=f"Uncertain {name}",
                )
            plt.legend()
            plt.grid()
            plt.savefig(f"{self.log_path}/{name}_{self.loop}")
            plt.clf()
        if num_gmm_sets == 3:
            return high_target, uncertain_target, info_target
        else:
            return high_target, info_target
//  [python]
def build_GMM(self, num_gmm_sets=3, fig=False, name="Local Density"):
        target = self.local_ds
        gmm1 = GaussianMixture(
            n_components=num_gmm_sets,
            max_iter=20,
            tol=1e-2,
            reg_covar=5e-7,
            random_state=1,
        )

        gmm1.fit(target)
        pred = gmm1.predict(target)
        info_idx = pred[target.argmin()]
        high_idx = pred[target.argmax()]
        high_target = (pred == high_idx).nonzero()[0]
        info_target = (pred == info_idx).nonzero()[0]

        if num_gmm_sets == 3:
            uncertain_idx = 3 - info_idx - high_idx
            uncertain_target = (pred == uncertain_idx).nonzero()[0]

        if fig and dist.get_rank() == 0:
            plt.hist(
                target[high_target],
                bins=200,
                range=(0.0, 1.0),
                edgecolor="black",
                alpha=0.5,
                label=f"High {name}",
            )
            plt.hist(
                target[info_target],
                bins=200,
                range=(0.0, 1.0),
                edgecolor="black",
                alpha=0.5,
                label=f"Informative {name}",
            )
            if num_gmm_sets == 3:
                plt.hist(
                    target[uncertain_target],
                    bins=200,
                    range=(0.0, 1.0),
                    edgecolor="black",
                    alpha=0.5,
                    label=f"Uncertain {name}",
                )
            plt.legend()
            plt.grid()
            plt.savefig(f"{self.log_path}/{name}_{self.loop}")
            plt.clf()
        if num_gmm_sets == 3:
            return high_target, uncertain_target, info_target
        else:
            return high_target, info_target
//  [python]
def sample(self, dist='dirichlet', batch_size=256, dirichlet_alpha=0.1, ori_data=None):
        if dist == 'dirichlet':
            z_true = np.random.dirichlet(
                np.ones(self.z_dim)*dirichlet_alpha, size=batch_size)
            z_true = torch.from_numpy(z_true).float()
            return z_true
        elif dist == 'gaussian':
            z_true = np.random.randn(batch_size, self.z_dim)
            z_true = torch.softmax(torch.from_numpy(z_true), dim=1).float()
            return z_true
        elif dist == 'gmm_std':
            odes = np.eye(self.z_dim)*20
            ides = np.random.randint(low=0, high=self.z_dim, size=batch_size)
            mus = odes[ides]
            sigmas = np.ones((batch_size, self.z_dim))*0.2*20
            z_true = np.random.normal(mus, sigmas)
            z_true = F.softmax(torch.from_numpy(z_true).float(), dim=1)
            return z_true
        elif dist=='gmm_ctm' and ori_data!=None:
            with torch.no_grad():
                hid_vecs = self.encode(ori_data).cpu().numpy()
                gmm = mixture.GaussianMixture(n_components=self.z_dim,covariance_type='full',max_iter=200)
                gmm.fit(hid_vecs)
                #hid_vecs = torch.from_numpy(hid_vecs).to(self.device)
                gmm_spls, _spl_lbls = gmm.sample(n_samples=len(ori_data))
                theta_prior = torch.from_numpy(gmm_spls).float()
                theta_prior = F.softmax(theta_prior,dim=1)
                return theta_prior
        else:
            return self.sample(dist='dirichlet',batch_size=batch_size)
//  [python]
def sample(self, dist='dirichlet', batch_size=256, dirichlet_alpha=0.1, ori_data=None):
        if dist == 'dirichlet':
            z_true = np.random.dirichlet(
                np.ones(self.z_dim)*dirichlet_alpha, size=batch_size)
            z_true = torch.from_numpy(z_true).float()
            return z_true
        elif dist == 'gaussian':
            z_true = np.random.randn(batch_size, self.z_dim)
            z_true = torch.softmax(torch.from_numpy(z_true), dim=1).float()
            return z_true
        elif dist == 'gmm_std':
            odes = np.eye(self.z_dim)*20
            ides = np.random.randint(low=0, high=self.z_dim, size=batch_size)
            mus = odes[ides]
            sigmas = np.ones((batch_size, self.z_dim))*0.2*20
            z_true = np.random.normal(mus, sigmas)
            z_true = F.softmax(torch.from_numpy(z_true).float(), dim=1)
            return z_true
        elif dist=='gmm_ctm' and ori_data!=None:
            with torch.no_grad():
                hid_vecs = self.encode(ori_data).cpu().numpy()
                gmm = GaussianMixture(n_components=self.z_dim,covariance_type='full',max_iter=200)
                gmm.fit(hid_vecs)
                #hid_vecs = torch.from_numpy(hid_vecs).to(self.device)
                gmm_spls, _spl_lbls = gmm.sample(n_samples=len(ori_data))
                theta_prior = torch.from_numpy(gmm_spls).float()
                theta_prior = F.softmax(theta_prior,dim=1)
                return theta_prior
        else:
            return self.sample(dist='dirichlet',batch_size=batch_size)
//  [python]
def getPeaksGMM(self, nodeID, itemname = 'rawScore'):


		X = []
		for i in range(len(self.nodes[nodeID][itemname])):
			X = X + [i] * int(self.nodes[nodeID][itemname][i])

		if len(X) < 10 :
			return [],[],[]


		#print("GMM size of X", len(X))

		X = np.asarray(X).reshape(-1,1)

		m = [mixture.GMM(i+1).fit(X) for i in range(3)]
		bic = [m[i].bic(X) for i in range(3)]

		nPeak = bic.index(min(bic))

		peaks = [int(m[nPeak].means_[i]) for i in range(nPeak+1)]
		covars = m[nPeak].covariances_
		weight = m[nPeak].weights_

		#print(peaks)
		#print(covars)
		#print(weight)

		return peaks, covars, weight
//  [python]
def getPeaksGMM(self, nodeID, itemname = 'rawScore'):


		X = []
		for i in range(len(self.nodes[nodeID][itemname])):
			X = X + [i] * int(self.nodes[nodeID][itemname][i])

		if len(X) < 10 :
			return [],[],[]


		#print("GMM size of X", len(X))

		X = np.asarray(X).reshape(-1,1)

		m = [GaussianMixture(i+1).fit(X) for i in range(3)]
		bic = [m[i].bic(X) for i in range(3)]

		nPeak = bic.index(min(bic))

		peaks = [int(m[nPeak].means_[i]) for i in range(nPeak+1)]
		covars = m[nPeak].covariances_
		weight = m[nPeak].weights_

		#print(peaks)
		#print(covars)
		#print(weight)

		return peaks, covars, weight
//  [python]
def __init__(self, batch_size, **gmm_args):
        check_modules('sklearn', 'clustering.GMMSampler')
        from sklearn.mixture import GMM

        if 'n_clusters' in gmm_args:
            raise ValueError(
                'You have specified n_clusters={} when creating GMMSampler.'
                ' This is not supported since n_clusters is overridden using '
                'batch_size.'.format(gmm_args['n_clusters']))
        gmm_args['n_components'] = batch_size
        super().__init__(GMM(**gmm_args), batch_size)
//  [python]
def __init__(self, batch_size, **gmm_args):
        check_modules('sklearn', 'clustering.GMMSampler')
        from sklearn.mixture import GaussianMixture

        if 'n_clusters' in gmm_args:
            raise ValueError(
                'You have specified n_clusters={} when creating GMMSampler.'
                ' This is not supported since n_clusters is overridden using '
                'batch_size.'.format(gmm_args['n_clusters']))
        gmm_args['n_components'] = batch_size
        super().__init__(GaussianMixture(**gmm_args), batch_size)
//  [python]
def eval_train(model, eval_loader, CE, all_loss, epoch, net, device, r, stats_log):
    model.eval()
    losses = torch.zeros(50000)
    losses_clean = torch.zeros(50000)
    with torch.no_grad():
        for batch_idx, (inputs, _, targets, index, targets_clean) in enumerate(eval_loader):
            inputs, targets, targets_clean = inputs.to(device), targets.to(device), targets_clean.to(device)
            outputs = model(inputs)
            loss = CE(outputs, targets)
            clean_loss = CE(outputs, targets_clean)
            for b in range(inputs.size(0)):
                losses[index[b]] = loss[b]
                losses_clean[index[b]] = clean_loss[b]
    losses = (losses - losses.min()) / (losses.max() - losses.min())
    all_loss.append(losses)

    history = torch.stack(all_loss)

    if r >= 0.9:  # average loss over last 5 epochs to improve convergence stability
        input_loss = history[-5:].mean(0)
        input_loss = input_loss.reshape(-1, 1)
    else:
        input_loss = losses.reshape(-1, 1)

    # exp = '_std_tpc_oracle'
    # save_losses(input_loss, exp)

    gmm = mixture.GMM(n_components=2, max_iter=200, tol=1e-2, reg_covar=5e-4)
    gmm.fit(input_loss)

    clean_idx, noisy_idx = gmm.means_.argmin(), gmm.means_.argmax()
    stats_log.write('Epoch {} (net {}): GMM results: {} with weight {}\t'
                    '{} with weight {}\n'.format(epoch, net, gmm.means_[clean_idx], gmm.weights_[clean_idx],
                                                 gmm.means_[noisy_idx], gmm.weights_[noisy_idx]))
    stats_log.flush()

    prob = gmm.predict_proba(input_loss)
    prob = prob[:, clean_idx]
    return prob, all_loss, losses_clean
//  [python]
def eval_train(model, eval_loader, CE, all_loss, epoch, net, device, r, stats_log):
    model.eval()
    losses = torch.zeros(50000)
    losses_clean = torch.zeros(50000)
    with torch.no_grad():
        for batch_idx, (inputs, _, targets, index, targets_clean) in enumerate(eval_loader):
            inputs, targets, targets_clean = inputs.to(device), targets.to(device), targets_clean.to(device)
            outputs = model(inputs)
            loss = CE(outputs, targets)
            clean_loss = CE(outputs, targets_clean)
            for b in range(inputs.size(0)):
                losses[index[b]] = loss[b]
                losses_clean[index[b]] = clean_loss[b]
    losses = (losses - losses.min()) / (losses.max() - losses.min())
    all_loss.append(losses)

    history = torch.stack(all_loss)

    if r >= 0.9:  # average loss over last 5 epochs to improve convergence stability
        input_loss = history[-5:].mean(0)
        input_loss = input_loss.reshape(-1, 1)
    else:
        input_loss = losses.reshape(-1, 1)

    # exp = '_std_tpc_oracle'
    # save_losses(input_loss, exp)

    gmm = GaussianMixture(n_components=2, max_iter=200, tol=1e-2, reg_covar=5e-4)
    gmm.fit(input_loss)

    clean_idx, noisy_idx = gmm.means_.argmin(), gmm.means_.argmax()
    stats_log.write('Epoch {} (net {}): GMM results: {} with weight {}\t'
                    '{} with weight {}\n'.format(epoch, net, gmm.means_[clean_idx], gmm.weights_[clean_idx],
                                                 gmm.means_[noisy_idx], gmm.weights_[noisy_idx]))
    stats_log.flush()

    prob = gmm.predict_proba(input_loss)
    prob = prob[:, clean_idx]
    return prob, all_loss, losses_clean
//  [python]
def getGMM(filename):

    nw = 320  #对于16KHz的文件，20ms的采样点个数
    inc = 160
    wave_data=getWaveData(filename)
    winfunc = signal.hann(nw)
    X = enframe(wave_data, nw, inc, winfunc)
    frameNum = X.shape[0]  # 返回矩阵列数，获取帧数

    data=[]
    for oneframe in  X:
        tmpList=list()
        for a in librosa.feature.mfcc(y=oneframe, sr=16000, n_mfcc=24):
            tmpList.append(a[0])
        data.append(tmpList)
    data=np.array(data)


    gmm = mixture.GMM(7, covariance_type='full', random_state=0).fit(data)
    return gmm
//  [python]
def getGMM(filename):

    nw = 320  #对于16KHz的文件，20ms的采样点个数
    inc = 160
    wave_data=getWaveData(filename)
    winfunc = signal.hann(nw)
    X = enframe(wave_data, nw, inc, winfunc)
    frameNum = X.shape[0]  # 返回矩阵列数，获取帧数

    data=[]
    for oneframe in  X:
        tmpList=list()
        for a in librosa.feature.mfcc(y=oneframe, sr=16000, n_mfcc=24):
            tmpList.append(a[0])
        data.append(tmpList)
    data=np.array(data)


    gmm = GaussianMixture(7, covariance_type='full', random_state=0).fit(data)
    return gmm
//  [python]
def process_gmm(): 
	K = int(sys.argv[1])

	# load all cols except screen_names (0)
	#X = np.loadtxt(sys.argv[2], delimiter=',', skiprows=1,
	X = np.loadtxt(sys.argv[2], delimiter=',', skiprows=0,
			#usecols=range(1,23)) # range(start,stop) - stop not inclusive, 1-16 or 1-23, 0 is screen_name
			usecols=range(1,16)) # range(start,stop) - stop not inclusive, 1-16 or 1-23, 0 is screen_name
	#X = np.genfromtxt(sys.argv[2], delimiter=',', skip_header=1,
	#		#usecols=range(1,23)) # range(start,stop) - stop not inclusive, 1-16 or 1-23, 0 is screen_name
	#		usecols=range(1,16)) # range(start,stop) - stop not inclusive, 1-16 or 1-23, 0 is screen_name

	## Tranpose (to normalise per col), Normalise, Tranpose (back to correct matrix arrangement)
	#X_tran = X.transpose()
	#X_norm = preprocessing.normalize(X_tran, norm='l1') # L1 for least absolute deviations
	#X = X_norm.transpose()

	#print("K: {}, data shape: [{}][{}]".format(K, len(X), len(X[0])))

	# Fit a Gaussian mixture with EM using K components
	gmm = mixture.GMM(n_components=K, covariance_type='full',
			tol=1e-4, n_iter=500, n_init=3, init_params='kmeans',
			random_state=0, verbose=1).fit(X)

	## generate random samples from the fitted Gaussian distribution
	#sample = gmm.sample(1000)

	# load screen_names
	with open(sys.argv[2]) as csvfile:
		read_csv = csv.reader(csvfile, delimiter=',')
		screen_names = []
		for row in read_csv:
			screen_names.append(row[0])

	# label clusters
	labels = gmm.predict(X)
	clusters = {}
	i = 0
	for label in labels:
		if label in clusters:
			clusters[label].append(screen_names[i])
		else:
			clusters[label] = [screen_names[i]]
		i += 1

	# outputs
	for cluster in clusters:
		f = open(sys.argv[3]+"/gmm."+sys.argv[2].split("/")[-1:].pop()+".K"+sys.argv[1]+".cluster"+str(cluster)+".out", "w+")
		for c in clusters[cluster]:
			f.write("{}\n".format(c))
		f.close()