jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def __init__(self, data_directory, model_directory, dump_data_directory, image_size, 
				 keep_PCA = 64, K_gmm = 64, only_green = False, verbose = True): 


		self.model_name = input("   Choose a name for the model : ")
		self.model_directory = model_directory
		self.dump_data_directory = dump_data_directory

		# Initialize hyper-parameters
		self.image_size = image_size
		self.keep_PCA = keep_PCA
		self.K_gmm = K_gmm
		self.only_green = only_green
		self.nb_mini_patch = int(image_size/8 - 1)**2 + int(image_size/16 - 1)**2 

		self.verbose = verbose
		# Initialize database
		self.data = il.Database_loader(directory = data_directory, 
							  		   size = image_size, 
							  		   only_green = only_green)





		# Initialize classifiers
		self.PCAs = []
		for i in range(self.nb_mini_patch):
			self.PCAs.append(PCA(n_components=keep_PCA))

		self.gmm = mixture.GaussianMixture(n_components=K_gmm, 
								   covariance_type='diag')
		self.clf_svm = CalibratedClassifierCV(LinearSVC())

		self.projector = Projection(red_dim = 128, treshold = 1.2,
									learning_rate = 0.001, 
									initialization = 'PCA')
//  [python]
def __init__(self, data_directory, model_directory, dump_data_directory, image_size, 
				 keep_PCA = 64, K_gmm = 64, only_green = False, verbose = True): 


		self.model_name = input("   Choose a name for the model : ")
		self.model_directory = model_directory
		self.dump_data_directory = dump_data_directory

		# Initialize hyper-parameters
		self.image_size = image_size
		self.keep_PCA = keep_PCA
		self.K_gmm = K_gmm
		self.only_green = only_green
		self.nb_mini_patch = int(image_size/8 - 1)**2 + int(image_size/16 - 1)**2 

		self.verbose = verbose
		# Initialize database
		self.data = il.Database_loader(directory = data_directory, 
							  		   size = image_size, 
							  		   only_green = only_green)





		# Initialize classifiers
		self.PCAs = []
		for i in range(self.nb_mini_patch):
			self.PCAs.append(PCA(n_components=keep_PCA))

		self.gmm = GaussianMixture(n_components=K_gmm, 
								   covariance_type='diag')
		self.clf_svm = CalibratedClassifierCV(LinearSVC())

		self.projector = Projection(red_dim = 128, treshold = 1.2,
									learning_rate = 0.001, 
									initialization = 'PCA')
//  [python]
def test_gaussian_mixture(self):
        model, X = fit_classification_model(sklearn.mixture.GaussianMixture(), 3)
        target_opset = 10
        model_onnx = convert_sklearn(
            model,
            "op10",
            [("input", FloatTensorType([None, X.shape[1]]))],
            target_opset=target_opset,
        )
        self.check_domain(model_onnx, target_opset=target_opset)
//  [python]
def test_gaussian_mixture(self):
        model, X = fit_classification_model(GaussianMixture(), 3)
        target_opset = 10
        model_onnx = convert_sklearn(
            model,
            "op10",
            [("input", FloatTensorType([None, X.shape[1]]))],
            target_opset=target_opset,
        )
        self.check_domain(model_onnx, target_opset=target_opset)
//  [python]
def getGMM(filename):

	data = np.load(filename)
	# 高斯混合模型 5个样本时，聚类数量为3，效果最好
	# 使用贝叶斯GMM，可避免数量选择
	gmm = mixture.GaussianMixture(7, covariance_type='diag', random_state=0).fit(data)
	return gmm
//  [python]
def getGMM(filename):

	data = np.load(filename)
	# 高斯混合模型 5个样本时，聚类数量为3，效果最好
	# 使用贝叶斯GMM，可避免数量选择
	gmm = GaussianMixture(7, covariance_type='diag', random_state=0).fit(data)
	return gmm
//  [python]
def init_sklearn(self, data, seed=None, max_iter=10, init_params='kmeans', reg=1e-6):
        r"""
        Initialize the GMM by training a GMM from the sklearn library.

        Args:
            data (np.array[N,D]): data matrix
            seed (int, None): seed for random generator
            max_iter (int): the number of EM iterations to perform
            init_params (str): {'kmeans', 'random'}, defaults to 'kmeans'. The method used to initialize the
                weights, the means and the precisions.
            reg (float): regularization term (useful to not have singular covariance matrices)
        """
        # check seed
        kwargs = {}
        if seed is not None:
            kwargs['random_state'] = seed

        # fit data to gmm
        gmm_ = mixture.GMM(n_components=self.num_components, max_iter=max_iter, init_params=init_params,
                               covariance_type='full', random_state=seed,  n_init=1)
        gmm_.fit(data)

        # create gaussians
        self._gaussians = [Gaussian(mean=mu, covariance=cov) for mu, cov in zip(gmm_.means_, gmm_.covars_)]
//  [python]
def init_sklearn(self, data, seed=None, max_iter=10, init_params='kmeans', reg=1e-6):
        r"""
        Initialize the GMM by training a GMM from the sklearn library.

        Args:
            data (np.array[N,D]): data matrix
            seed (int, None): seed for random generator
            max_iter (int): the number of EM iterations to perform
            init_params (str): {'kmeans', 'random'}, defaults to 'kmeans'. The method used to initialize the
                weights, the means and the precisions.
            reg (float): regularization term (useful to not have singular covariance matrices)
        """
        # check seed
        kwargs = {}
        if seed is not None:
            kwargs['random_state'] = seed

        # fit data to gmm
        gmm_ = GaussianMixture(n_components=self.num_components, max_iter=max_iter, init_params=init_params,
                               reg_covar=reg, **kwargs)
        gmm_.fit(data)

        # create gaussians
        self._gaussians = [Gaussian(mean=mu, covariance=cov) for mu, cov in zip(gmm_.means_, gmm_.covariances_)]
//  [python]
def _cluster_mixture(data, n_clusters=2, bayesian=False, random_state=None, **kwargs):
    """Mixture model"""
    # Initialize clustering function
    if bayesian is False:
        clustering_model = s.mixture.GaussianMixture(
            n_components=n_clusters, random_state=random_state, **kwargs
        )
    else:
        clustering_model = s.mixture.BayesianGaussianMixture(
            n_components=n_clusters, random_state=random_state, **kwargs
        )

    # Fit
    clustering = clustering_model.fit_predict(data)

    # Get representatives
    clusters = clustering_model.means_

    # Get probability
    prediction = clustering_model.predict_proba(data)
    prediction = pd.DataFrame(prediction).add_prefix("Probability_")
    prediction["Cluster"] = clustering

    # Else, copy function
    clustering_function = functools.partial(
        _cluster_mixture, n_clusters=n_clusters, random_state=random_state, **kwargs
    )

    # Info dump
    info = {
        "n_clusters": n_clusters,
        "clustering_function": clustering_function,
        "sklearn_model": clustering_model,
        "random_state": random_state,
    }

    return prediction, clusters, info
//  [python]
def _cluster_mixture(data, n_clusters=2, bayesian=False, random_state=None, **kwargs):
    """Mixture model"""
    # Initialize clustering function
    if bayesian is False:
        clustering_model = sklearn.mixture.GaussianMixture(
            n_components=n_clusters, random_state=random_state, **kwargs
        )
    else:
        clustering_model = sklearn.mixture.BayesianGaussianMixture(
            n_components=n_clusters, random_state=random_state, **kwargs
        )

    # Fit
    clustering = clustering_model.fit_predict(data)

    # Get representatives
    clusters = clustering_model.means_

    # Get probability
    prediction = clustering_model.predict_proba(data)
    prediction = pd.DataFrame(prediction).add_prefix("Probability_")
    prediction["Cluster"] = clustering

    # Else, copy function
    clustering_function = functools.partial(
        _cluster_mixture, n_clusters=n_clusters, random_state=random_state, **kwargs
    )

    # Info dump
    info = {
        "n_clusters": n_clusters,
        "clustering_function": clustering_function,
        "sklearn_model": clustering_model,
        "random_state": random_state,
    }

    return prediction, clusters, info
//  [python]
def fit_multivariate_gaussian(X_s):
    gmm = mixture.GaussianMixture(n_components=1).fit(X_s)
    labels = gmm.predict(X_s)
    N = 50
    X = np.linspace(-2, 10, N)
    Y = np.linspace(-4, 4, N)
    X, Y = np.meshgrid(X, Y)
    # Pack X and Y into a single 3-dimensional array
    pos = np.empty(X.shape + (2,))
    pos[:, :, 0] = X
    pos[:, :, 1] = Y
    norm = distribution.MultivariateNormal(torch.Tensor(gmm.means_[0]), torch.Tensor(gmm.covariances_[0]))
    Z = torch.exp(norm.log_prob(torch.Tensor(pos))).numpy()
    plt.figure(figsize=(10, 8));
    ax = plt.gca()
    cset = ax.contourf(X, Y, Z, cmap='magma')
    plt.scatter(X_s[:, 0], X_s[:, 1], c='b', s=60, edgecolor='w', zorder=2.5); plt.grid(True);
    return labels
//  [python]
def fit_multivariate_gaussian(X_s):
    gmm = GaussianMixture(n_components=1).fit(X_s)
    labels = gmm.predict(X_s)
    N = 50
    X = np.linspace(-2, 10, N)
    Y = np.linspace(-4, 4, N)
    X, Y = np.meshgrid(X, Y)
    # Pack X and Y into a single 3-dimensional array
    pos = np.empty(X.shape + (2,))
    pos[:, :, 0] = X
    pos[:, :, 1] = Y
    norm = distribution.MultivariateNormal(torch.Tensor(gmm.means_[0]), torch.Tensor(gmm.covariances_[0]))
    Z = torch.exp(norm.log_prob(torch.Tensor(pos))).numpy()
    plt.figure(figsize=(10, 8));
    ax = plt.gca()
    cset = ax.contourf(X, Y, Z, cmap='magma')
    plt.scatter(X_s[:, 0], X_s[:, 1], c='b', s=60, edgecolor='w', zorder=2.5); plt.grid(True);
    return labels
//  [python]
def setUp(self):
        self.size = 100
        self.X = np.random.rand(self.size, 1)
        self.y = self.X + np.random.randn(self.size, 1)
        concatenator = Concatenator()
        gaussian_clustering = mixture.GaussianMixture(n_components=3)
        dbscan = DBSCAN(eps=0.5)
        mixer = CustomCombination()
        linear_model = LinearRegression()
        self.steps = steps = [('Concatenate_Xy', concatenator),
                              ('Gaussian_Mixture', gaussian_clustering),
                              ('Dbscan', dbscan),
                              ('Combine_Clustering', mixer),
                              ('Regressor', linear_model),
                              ]

        self.connections = {
            'Concatenate_Xy': dict(df1='X',
                                   df2='y'),

            'Gaussian_Mixture': dict(X=('Concatenate_Xy', 'Xy')),

            'Dbscan': dict(X=('Concatenate_Xy', 'Xy')),

            'Combine_Clustering': dict(
                dominant=('Dbscan', 'predict'),
                other=('Gaussian_Mixture', 'predict')),


            'Regressor': dict(X='X', y='y')
        }
//  [python]
def setUp(self):
        self.size = 100
        self.X = np.random.rand(self.size, 1)
        self.y = self.X + np.random.randn(self.size, 1)
        concatenator = Concatenator()
        gaussian_clustering = GaussianMixture(n_components=3)
        dbscan = DBSCAN(eps=0.5)
        mixer = CustomCombination()
        linear_model = LinearRegression()
        self.steps = steps = [('Concatenate_Xy', concatenator),
                              ('Gaussian_Mixture', gaussian_clustering),
                              ('Dbscan', dbscan),
                              ('Combine_Clustering', mixer),
                              ('Regressor', linear_model),
                              ]

        self.connections = {
            'Concatenate_Xy': dict(df1='X',
                                   df2='y'),

            'Gaussian_Mixture': dict(X=('Concatenate_Xy', 'Xy')),

            'Dbscan': dict(X=('Concatenate_Xy', 'Xy')),

            'Combine_Clustering': dict(
                dominant=('Dbscan', 'predict'),
                other=('Gaussian_Mixture', 'predict')),


            'Regressor': dict(X='X', y='y')
        }
//  [python]
def fit_gaussian_mixture(X_s):
    gmm = mixture.GaussianMixture(n_components=4).fit(X_s)
    labels = gmm.predict(X_s)
    N = 50
    X = np.linspace(-2, 10, N)
    Y = np.linspace(-4, 4, N)
    X, Y = np.meshgrid(X, Y)
    # Pack X and Y into a single 3-dimensional array
    pos = np.empty(X.shape + (2,))
    pos[:, :, 0] = X
    pos[:, :, 1] = Y
    Z = np.zeros((pos.shape[0], pos.shape[1]))
    for i in range(4):
        norm = distribution.MultivariateNormal(torch.Tensor(gmm.means_[i]), torch.Tensor(gmm.covariances_[i]))
        Z += torch.exp(norm.log_prob(torch.Tensor(pos))).numpy()
    plt.figure(figsize=(10, 8));
    ax = plt.gca()
    cset = ax.contourf(X, Y, Z, cmap='magma')
    plt.scatter(X_s[:, 0], X_s[:, 1], c='b', s=60, edgecolor='w', zorder=2.5); plt.grid(True);
    return labels
//  [python]
def fit_gaussian_mixture(X_s):
    gmm = GaussianMixture(n_components=4).fit(X_s)
    labels = gmm.predict(X_s)
    N = 50
    X = np.linspace(-2, 10, N)
    Y = np.linspace(-4, 4, N)
    X, Y = np.meshgrid(X, Y)
    # Pack X and Y into a single 3-dimensional array
    pos = np.empty(X.shape + (2,))
    pos[:, :, 0] = X
    pos[:, :, 1] = Y
    Z = np.zeros((pos.shape[0], pos.shape[1]))
    for i in range(4):
        norm = distribution.MultivariateNormal(torch.Tensor(gmm.means_[i]), torch.Tensor(gmm.covariances_[i]))
        Z += torch.exp(norm.log_prob(torch.Tensor(pos))).numpy()
    plt.figure(figsize=(10, 8));
    ax = plt.gca()
    cset = ax.contourf(X, Y, Z, cmap='magma')
    plt.scatter(X_s[:, 0], X_s[:, 1], c='b', s=60, edgecolor='w', zorder=2.5); plt.grid(True);
    return labels
//  [python]
def _initialize_variables(self, X):
        """
        Initializes the variables of the model. Right now fits a PCA model
        in sklearn, uses the loadings and sets sigma^2 to be unexplained
        variance for each group.

        Parameters
        ----------
        X : np.array-like,(n_samples,p)
            The data

        Returns
        -------
        W_ : torch.tensor-like,(n_components,p)
            The loadings of our latent factor model

        sigmal_ : list
            A list of the isotropic noises for each group
        """
        model_pca = PCA(self.n_components)
        S_ = model_pca.fit_transform(X)
        model_gmm = mixture.GaussianMixture(self.n_clusters, covariance_type="tied")
        model_gmm.fit(S_)
        W_init = model_pca.components_
        W_ = torch.tensor(W_init.astype(np.float32), requires_grad=True)
        X_recon = np.dot(S_, W_init)
        diff = np.mean((X - X_recon) ** 2)
        sinv = softplus_inverse_np(diff * np.ones(1).astype(np.float32))
        sigmal_ = torch.tensor(sinv, requires_grad=True)

        pi_logits = torch.tensor(
            np.log(model_gmm.weights_ + 1e-3).astype(np.float32),
            requires_grad=True,
        )
        mean_list = [
            torch.tensor(
                model_gmm.means_[k].astype(np.float32), requires_grad=True
            )
            for k in range(self.n_clusters)
        ]
        return W_, sigmal_, pi_logits, mean_list
//  [python]
def _initialize_variables(self, X):
        """
        Initializes the variables of the model. Right now fits a PCA model
        in sklearn, uses the loadings and sets sigma^2 to be unexplained
        variance for each group.

        Parameters
        ----------
        X : np.array-like,(n_samples,p)
            The data

        Returns
        -------
        W_ : torch.tensor-like,(n_components,p)
            The loadings of our latent factor model

        sigmal_ : list
            A list of the isotropic noises for each group
        """
        model_pca = PCA(self.n_components)
        S_ = model_pca.fit_transform(X)
        model_gmm = GaussianMixture(self.n_clusters, covariance_type="tied")
        model_gmm.fit(S_)
        W_init = model_pca.components_
        W_ = torch.tensor(W_init.astype(np.float32), requires_grad=True)
        X_recon = np.dot(S_, W_init)
        diff = np.mean((X - X_recon) ** 2)
        sinv = softplus_inverse_np(diff * np.ones(1).astype(np.float32))
        sigmal_ = torch.tensor(sinv, requires_grad=True)

        pi_logits = torch.tensor(
            np.log(model_gmm.weights_ + 1e-3).astype(np.float32),
            requires_grad=True,
        )
        mean_list = [
            torch.tensor(
                model_gmm.means_[k].astype(np.float32), requires_grad=True
            )
            for k in range(self.n_clusters)
        ]
        return W_, sigmal_, pi_logits, mean_list
//  [python]
def setUp(self):
        self.size = 100
        self.X = np.random.rand(self.size, 1)
        self.y = 2 * self.X

        sc = MinMaxScaler()
        gm = GMM(n_components=3)
        km = KMeans(n_clusters=4)

        steps = [('scaler', sc), ('gaussian', gm), ('kmeans', km)]
        connections_1 = {'scaler': dict(X='X'), 'gaussian': 'scaler'}
        connections_2 = {'scaler': dict(X='X'), 'kmeans': 'scaler'}

        self.sc = sc
        self.gm = gm
        self.km = km

        self.steps = steps
        self.connections_1 = connections_1
        self.connections_2 = connections_2
        self.pgraph = PipeGraph(steps=steps, fit_connections=connections_1)
        self.param_grid = dict(fit_connections=[connections_1, connections_2])
//  [python]
def setUp(self):
        self.size = 100
        self.X = np.random.rand(self.size, 1)
        self.y = 2 * self.X

        sc = MinMaxScaler()
        gm = GaussianMixture(n_components=3)
        km = KMeans(n_clusters=4)

        steps = [('scaler', sc), ('gaussian', gm), ('kmeans', km)]
        connections_1 = {'scaler': dict(X='X'), 'gaussian': 'scaler'}
        connections_2 = {'scaler': dict(X='X'), 'kmeans': 'scaler'}

        self.sc = sc
        self.gm = gm
        self.km = km

        self.steps = steps
        self.connections_1 = connections_1
        self.connections_2 = connections_2
        self.pgraph = PipeGraph(steps=steps, fit_connections=connections_1)
        self.param_grid = dict(fit_connections=[connections_1, connections_2])
//  [python]
def cluster_GMM(num_clusters, word_vectors):
	# Initalize a GMM object and use it for clustering.
	clf =  sklearn.mixture.GMM(n_components=num_clusters,
                    covariance_type="full", init_params='kmeans', max_iter=50)
	# Get cluster assignments.
	clf.fit(word_vectors)
	idx = clf.predict(word_vectors)
	# print("Clustering Done...", time.time()-start, "seconds")
	# Get probabilities of cluster assignments.
	idx_proba = clf.predict_proba(word_vectors)
	# Dump cluster assignments and probability of cluster assignments. 
	joblib.dump(idx, 'gmm_latestclusmodel_len2alldata.pkl')
	# print("Cluster Assignments Saved...")

	joblib.dump(idx_proba, 'gmm_prob_latestclusmodel_len2alldata.pkl')
	# print("Probabilities of Cluster Assignments Saved...")
	return (idx, idx_proba)
//  [python]
def cluster_GMM(num_clusters, word_vectors):
	# Initalize a GMM object and use it for clustering.
	clf =  GaussianMixture(n_components=num_clusters,
                    covariance_type="full", init_params='kmeans', max_iter=50)
	# Get cluster assignments.
	clf.fit(word_vectors)
	idx = clf.predict(word_vectors)
	# print("Clustering Done...", time.time()-start, "seconds")
	# Get probabilities of cluster assignments.
	idx_proba = clf.predict_proba(word_vectors)
	# Dump cluster assignments and probability of cluster assignments. 
	joblib.dump(idx, 'gmm_latestclusmodel_len2alldata.pkl')
	# print("Cluster Assignments Saved...")

	joblib.dump(idx_proba, 'gmm_prob_latestclusmodel_len2alldata.pkl')
	# print("Probabilities of Cluster Assignments Saved...")
	return (idx, idx_proba)
//  [python]
def setUp(self):
        self.size = 1000
        self.X = pd.DataFrame(dict(X=np.random.rand(self.size, )))
        self.y = pd.DataFrame(dict(y=(np.random.rand(self.size, ))))
        concatenator = Concatenator()
        gaussian_clustering = mixture.GaussianMixture(n_components=3)
        dbscan = DBSCAN(eps=0.5)
        mixer = CustomCombination()
        linear_model = LinearRegression()
        steps = [('Concatenate_Xy', concatenator),
                 ('Gaussian_Mixture', gaussian_clustering),
                 ('Dbscan', dbscan),
                 ('Combine_Clustering', mixer),
                 ('Regressor', linear_model),
                 ]

        connections = {
            'Concatenate_Xy': dict(df1='X',
                                   df2='y'),

            'Gaussian_Mixture': dict(X=('Concatenate_Xy', 'predict')),

            'Dbscan': dict(X=('Concatenate_Xy', 'predict')),

            'Combine_Clustering': dict(
                dominant=('Dbscan', 'predict'),
                other=('Gaussian_Mixture', 'predict')),

            'Regressor': dict(X='X', y='y')
        }

        self.steps_external = [('_External', concatenator),
                               ('Gaussian_Mixture', gaussian_clustering),
                               ('Dbscan', dbscan),
                               ('Combine_Clustering', mixer),
                               ('Regressor', linear_model),
                               ]

        self.connections_external = {
            '_External': dict(df1='X',
                              df2='y'),

            'Gaussian_Mixture': dict(X=('Concatenate_Xy', 'predict')),

            'Dbscan': dict(X=('Concatenate_Xy', 'predict')),

            'Combine_Clustering': dict(
                dominant=('Dbscan', 'predict'),
                other=('Gaussian_Mixture', 'predict')),

            'Regressor': dict(X='X', y='y')
        }

        self.steps = steps
        self.connections = connections
        self.pgraph = PipeGraph(steps=steps, fit_connections=connections)
        self.pgraph.fit(self.X, self.y)
//  [python]
def setUp(self):
        self.size = 1000
        self.X = pd.DataFrame(dict(X=np.random.rand(self.size, )))
        self.y = pd.DataFrame(dict(y=(np.random.rand(self.size, ))))
        concatenator = Concatenator()
        gaussian_clustering = GaussianMixture(n_components=3)
        dbscan = DBSCAN(eps=0.5)
        mixer = CustomCombination()
        linear_model = LinearRegression()
        steps = [('Concatenate_Xy', concatenator),
                 ('Gaussian_Mixture', gaussian_clustering),
                 ('Dbscan', dbscan),
                 ('Combine_Clustering', mixer),
                 ('Regressor', linear_model),
                 ]

        connections = {
            'Concatenate_Xy': dict(df1='X',
                                   df2='y'),

            'Gaussian_Mixture': dict(X=('Concatenate_Xy', 'predict')),

            'Dbscan': dict(X=('Concatenate_Xy', 'predict')),

            'Combine_Clustering': dict(
                dominant=('Dbscan', 'predict'),
                other=('Gaussian_Mixture', 'predict')),

            'Regressor': dict(X='X', y='y')
        }

        self.steps_external = [('_External', concatenator),
                               ('Gaussian_Mixture', gaussian_clustering),
                               ('Dbscan', dbscan),
                               ('Combine_Clustering', mixer),
                               ('Regressor', linear_model),
                               ]

        self.connections_external = {
            '_External': dict(df1='X',
                              df2='y'),

            'Gaussian_Mixture': dict(X=('Concatenate_Xy', 'predict')),

            'Dbscan': dict(X=('Concatenate_Xy', 'predict')),

            'Combine_Clustering': dict(
                dominant=('Dbscan', 'predict'),
                other=('Gaussian_Mixture', 'predict')),

            'Regressor': dict(X='X', y='y')
        }

        self.steps = steps
        self.connections = connections
        self.pgraph = PipeGraph(steps=steps, fit_connections=connections)
        self.pgraph.fit(self.X, self.y)
//  [python]
def testEmMatchesSkLearn(self):
        """
        Assert that log-probabilities (E-step) and parameter updates (M-step) approximately match those of sklearn.
        """
        d = 20
        n_components = np.random.randint(1, 100)

        # (n, k, d)
        x = torch.randn(40, 1, d)
        # (n, d)
        x_np = np.squeeze(x.data.numpy())

        var_init = torch.ones(1, n_components, d) - .4

        model = GaussianMixture(n_components, d, var_init=var_init)
        model_sk = sklearn.mixture.GMM(n_components,
            covariance_type="diag",
            init_params="random",
            means_init=np.squeeze(model.mu.data.numpy()),
            precisions_init=np.squeeze(1. / np.sqrt(var_init.data.numpy())))

        model_sk._initialize(x_np, np.random.RandomState())
        log_prob_sk = model_sk._estimate_log_prob(x_np)
        log_prob = model._estimate_log_prob(x)

        # Test whether log-probabilities are approximately equal
        np.testing.assert_almost_equal(np.squeeze(log_prob.data.numpy()),
            log_prob_sk,
            decimal=2,
            verbose=True)

        _, log_resp_sk = model_sk._e_step(x_np)
        _, log_resp = model._e_step(x)

        # Test whether E-steps are approximately equal
        np.testing.assert_almost_equal(np.squeeze(log_resp.data.numpy()),
            log_resp_sk,
            decimal=0,
            verbose=True)

        model_sk._m_step(x_np, log_prob_sk)
        pi_sk = model_sk.weights_
        mu_sk = model_sk.means_
        var_sk = model_sk.means_

        pi, mu, var = model._m_step(x, log_prob)

        # Test whether pi ..
        np.testing.assert_almost_equal(np.squeeze(pi.data.numpy()),
            pi_sk,
            decimal=1,
            verbose=True)

        # .. mu ..
        np.testing.assert_almost_equal(np.squeeze(mu.data.numpy()),
            mu_sk,
            decimal=1,
            verbose=True)

        # .. and var are approximately equal
        np.testing.assert_almost_equal(np.squeeze(var.data.numpy()),
            var_sk,
            decimal=1,
            verbose=True)
//  [python]
def testEmMatchesSkLearn(self):
        """
        Assert that log-probabilities (E-step) and parameter updates (M-step) approximately match those of sklearn.
        """
        d = 20
        n_components = np.random.randint(1, 100)

        # (n, k, d)
        x = torch.randn(40, 1, d)
        # (n, d)
        x_np = np.squeeze(x.data.numpy())

        var_init = torch.ones(1, n_components, d) - .4

        model = GaussianMixture(n_components, d, var_init=var_init)
        model_sk = sklearn.mixture.GaussianMixture(n_components,
            covariance_type="diag",
            init_params="random",
            means_init=np.squeeze(model.mu.data.numpy()),
            precisions_init=np.squeeze(1. / np.sqrt(var_init.data.numpy())))

        model_sk._initialize_parameters(x_np, np.random.RandomState())
        log_prob_sk = model_sk._estimate_log_prob(x_np)
        log_prob = model._estimate_log_prob(x)

        # Test whether log-probabilities are approximately equal
        np.testing.assert_almost_equal(np.squeeze(log_prob.data.numpy()),
            log_prob_sk,
            decimal=2,
            verbose=True)

        _, log_resp_sk = model_sk._e_step(x_np)
        _, log_resp = model._e_step(x)

        # Test whether E-steps are approximately equal
        np.testing.assert_almost_equal(np.squeeze(log_resp.data.numpy()),
            log_resp_sk,
            decimal=0,
            verbose=True)

        model_sk._m_step(x_np, log_prob_sk)
        pi_sk = model_sk.weights_
        mu_sk = model_sk.means_
        var_sk = model_sk.means_

        pi, mu, var = model._m_step(x, log_prob)

        # Test whether pi ..
        np.testing.assert_almost_equal(np.squeeze(pi.data.numpy()),
            pi_sk,
            decimal=1,
            verbose=True)

        # .. mu ..
        np.testing.assert_almost_equal(np.squeeze(mu.data.numpy()),
            mu_sk,
            decimal=1,
            verbose=True)

        # .. and var are approximately equal
        np.testing.assert_almost_equal(np.squeeze(var.data.numpy()),
            var_sk,
            decimal=1,
            verbose=True)
//  [python]
def cal_gmm_components(evt_xy, n_components=1):
    score_list = []
    for i in range(n_components, n_components+5):
        gmm = mixture.GaussianMixture(n_components=i, warm_start=True).fit(evt_xy)
        score_list.append(gmm.bic(evt_xy))

    print(score_list)
//  [python]
def cal_gmm_components(evt_xy, n_components=1):
    score_list = []
    for i in range(n_components, n_components+5):
        gmm = GaussianMixture(n_components=i, warm_start=True).fit(evt_xy)
        score_list.append(gmm.bic(evt_xy))

    print(score_list)
//  [python]
def _cluster_mixture(data, n_clusters=2, bayesian=False, random_state=None, **kwargs):
    """Mixture model
    """
    # Initialize clustering function
    if bayesian is False:
        clustering_model = sklearn.mixture.GMM(n_components=n_clusters,
                                                           random_state=random_state,
                                                           **kwargs)
    else:
        clustering_model = sklearn.mixture.BayesianGaussianMixture(n_components=n_clusters,
                                                                   random_state=random_state,
                                                                   **kwargs)

    # Fit
    clustering = clustering_model.fit_predict(data)

    # Get representatives
    clusters = clustering_model.means_

    # Get probability
    prediction = clustering_model.predict_proba(data)
    prediction = pd.DataFrame(prediction).add_prefix("Probability_")
    prediction["Cluster"] = clustering

    # Else, copy function
    clustering_function = functools.partial(_cluster_mixture,
                                            n_clusters=n_clusters,
                                            random_state=random_state,
                                            **kwargs)

    # Info dump
    info = {"n_clusters": n_clusters,
            "clustering_function": clustering_function,
            "sklearn_model": clustering_model,
            "random_state": random_state}

    return prediction, clusters, info
//  [python]
def _cluster_mixture(data, n_clusters=2, bayesian=False, random_state=None, **kwargs):
    """Mixture model
    """
    # Initialize clustering function
    if bayesian is False:
        clustering_model = sklearn.mixture.GaussianMixture(n_components=n_clusters,
                                                           random_state=random_state,
                                                           **kwargs)
    else:
        clustering_model = sklearn.mixture.BayesianGaussianMixture(n_components=n_clusters,
                                                                   random_state=random_state,
                                                                   **kwargs)

    # Fit
    clustering = clustering_model.fit_predict(data)

    # Get representatives
    clusters = clustering_model.means_

    # Get probability
    prediction = clustering_model.predict_proba(data)
    prediction = pd.DataFrame(prediction).add_prefix("Probability_")
    prediction["Cluster"] = clustering

    # Else, copy function
    clustering_function = functools.partial(_cluster_mixture,
                                            n_clusters=n_clusters,
                                            random_state=random_state,
                                            **kwargs)

    # Info dump
    info = {"n_clusters": n_clusters,
            "clustering_function": clustering_function,
            "sklearn_model": clustering_model,
            "random_state": random_state}

    return prediction, clusters, info
//  [python]
def best_gmm(X,
             max_range=np.arange(2, 11),
             covariance_types=None,
             max_iter=1000,
             n_init=5,
             seed=SEED):
    """
    Return the best Gaussian Mixture Model given the data, a range of K values, and two K selection criteria.

    :param X: usage matrix (made of usage vectors)
    :param max_range: range within the number of clusters should lie
    :param covariance_types: a list containing any subset of this list:
    :param max_iter: maximum number of EM iterations
    :param n_init: number of EM runs
    :param seed: random seed
    :return: best GMM according to Akaike Information Criterion, Bayesian Information Criterion,
             and the respective AIC and BIC scores
    """
    if covariance_types is None:
        covariance_types = ['full', 'spherical', 'tied', 'diag']
    if not isinstance(covariance_types, (list,)):
        covariance_types = [covariance_types]

    aics = defaultdict(list)
    bics = defaultdict(list)
    best_gmm_aic = GMM()
    best_gmm_bic = GMM()

    for i, cov in enumerate(covariance_types):
        for k in max_range:
            m = mixture.GaussianMixture(
                n_components=k,
                covariance_type=cov,
                max_iter=max_iter,
                n_init=n_init,
                random_state=seed).fit(X)

            if m.aic(X) < best_gmm_aic.aic(X):
                best_gmm_aic = GMM(m)
            if m.bic(X) < best_gmm_bic.bic(X):
                best_gmm_bic = GMM(m)

            bics[cov].append(m.bic(X))
            aics[cov].append(m.aic(X))

    return best_gmm_aic, best_gmm_bic, bics, aics
//  [python]
def best_gmm(X,
             max_range=np.arange(2, 11),
             covariance_types=None,
             max_iter=1000,
             n_init=5,
             seed=SEED):
    """
    Return the best Gaussian Mixture Model given the data, a range of K values, and two K selection criteria.

    :param X: usage matrix (made of usage vectors)
    :param max_range: range within the number of clusters should lie
    :param covariance_types: a list containing any subset of this list:
    :param max_iter: maximum number of EM iterations
    :param n_init: number of EM runs
    :param seed: random seed
    :return: best GMM according to Akaike Information Criterion, Bayesian Information Criterion,
             and the respective AIC and BIC scores
    """
    if covariance_types is None:
        covariance_types = ['full', 'spherical', 'tied', 'diag']
    if not isinstance(covariance_types, (list,)):
        covariance_types = [covariance_types]

    aics = defaultdict(list)
    bics = defaultdict(list)
    best_gmm_aic = GMM()
    best_gmm_bic = GMM()

    for i, cov in enumerate(covariance_types):
        for k in max_range:
            m = GaussianMixture(
                n_components=k,
                covariance_type=cov,
                max_iter=max_iter,
                n_init=n_init,
                random_state=seed).fit(X)

            if m.aic(X) < best_gmm_aic.aic(X):
                best_gmm_aic = GMM(m)
            if m.bic(X) < best_gmm_bic.bic(X):
                best_gmm_bic = GMM(m)

            bics[cov].append(m.bic(X))
            aics[cov].append(m.aic(X))

    return best_gmm_aic, best_gmm_bic, bics, aics
//  [python]
def get_best_gmm(X):
    lowest_bic = np.infty
    bic = []
    n_components_range = range(
        1, 7)  ## stop based on fit/small BIC change/ earlystopping
    cv_types = ['full']
    best_gmm = mixture.GaussianMixture()
    for cv_type in cv_types:
        for n_components in n_components_range:
            # Fit a Gaussian mixture with EM
            gmm = mixture.GaussianMixture(n_components=n_components,
                                  covariance_type=cv_type)
            gmm.fit(X)
            bic.append(gmm.bic(X))
            if bic[-1] < lowest_bic:
                lowest_bic = bic[-1]
                best_gmm = gmm

    bic = np.array(bic)
    return best_gmm
//  [python]
def get_best_gmm(X):
    lowest_bic = np.infty
    bic = []
    n_components_range = range(
        1, 7)  ## stop based on fit/small BIC change/ earlystopping
    cv_types = ['full']
    best_gmm = GaussianMixture()
    for cv_type in cv_types:
        for n_components in n_components_range:
            # Fit a Gaussian mixture with EM
            gmm = GaussianMixture(n_components=n_components,
                                  covariance_type=cv_type)
            gmm.fit(X)
            bic.append(gmm.bic(X))
            if bic[-1] < lowest_bic:
                lowest_bic = bic[-1]
                best_gmm = gmm

    bic = np.array(bic)
    return best_gmm
//  [python]
def _eval_samples(self, eval_loader):
        """
        Sample selection
        """
        self.model.eval()
        loss_func = nn.CrossEntropyLoss(reduction='none')
        losses = np.zeros(len(eval_loader.dataset))
        with torch.no_grad():
            for i, data in enumerate(eval_loader):
                input_ids, att_mask, labels, index = [Variable(elem.cuda()) for elem in data] 
                outputs = self.model(input_ids, att_mask) 
                pred = torch.softmax(outputs, dim=-1)
                loss = loss_func(pred, labels).cpu().detach().numpy()
                index = index.long().cpu().detach().numpy()
                losses[index] = loss
                
        if self.model_args.class_reg:
            labels = np.array(eval_loader.dataset.labels, dtype=int)
            for now_class in range(self.model_args.num_classes):
                indices = np.where(labels == now_class)[0]
                losses[indices] = (losses[indices] - losses[indices].mean()) / losses[indices].var()
        else:
            losses = (losses - losses.min()) / (losses.max() - losses.min())
        
        gmm = mixture.GaussianMixture(
            n_components=2, 
            max_iter=self.model_args.gmm_max_iter, 
            tol=self.model_args.gmm_tol, 
            reg_covar=self.model_args.gmm_reg_covar
        )
        losses = losses.reshape(-1, 1)
        gmm.fit(losses)
        prob = gmm.predict_proba(losses) 
        prob = prob[:,gmm.means_.argmin()]
        return prob
//  [python]
def _eval_samples(self, eval_loader):
        """
        Sample selection
        """
        self.model.eval()
        loss_func = nn.CrossEntropyLoss(reduction='none')
        losses = np.zeros(len(eval_loader.dataset))
        with torch.no_grad():
            for i, data in enumerate(eval_loader):
                input_ids, att_mask, labels, index = [Variable(elem.cuda()) for elem in data] 
                outputs = self.model(input_ids, att_mask) 
                pred = torch.softmax(outputs, dim=-1)
                loss = loss_func(pred, labels).cpu().detach().numpy()
                index = index.long().cpu().detach().numpy()
                losses[index] = loss
                
        if self.model_args.class_reg:
            labels = np.array(eval_loader.dataset.labels, dtype=int)
            for now_class in range(self.model_args.num_classes):
                indices = np.where(labels == now_class)[0]
                losses[indices] = (losses[indices] - losses[indices].mean()) / losses[indices].var()
        else:
            losses = (losses - losses.min()) / (losses.max() - losses.min())
        
        gmm = GaussianMixture(
            n_components=2, 
            max_iter=self.model_args.gmm_max_iter, 
            tol=self.model_args.gmm_tol, 
            reg_covar=self.model_args.gmm_reg_covar
        )
        losses = losses.reshape(-1, 1)
        gmm.fit(losses)
        prob = gmm.predict_proba(losses) 
        prob = prob[:,gmm.means_.argmin()]
        return prob
//  [python]
def __init__(self, n_components):
        self.model = mixture.GaussianMixture(n_components=n_components, max_iter=1000)
//  [python]
def __init__(self, n_components):
        self.model = GaussianMixture(n_components=n_components, max_iter=1000)
//  [python]
def get_best_gmm2(X):  #early stopping gmm
    lowest_bic = np.infty
    bic = []
    cv_types = ['full']  #changed to only looking for full covariance
    best_gmm = mixture.GMM()
    for cv_type in cv_types:
        p = 1  #Decide a value
        n_comps = 1
        j = 0
        while j < p and n_comps < 5:  # if hasn't improved in p times, then stop. Do it for each cv type and take the minimum of all of them
            gmm = mixture.GMM(n_components=n_comps,
                                  covariance_type=cv_type)
            gmm.fit(X)
            bic.append(gmm.bic(X))
            if bic[-1] < lowest_bic:
                lowest_bic = bic[-1]
                best_gmm = gmm
                j = 0  #reset counter
            else:  #increment counter
                j += 1
            n_comps += 1

    bic = np.array(bic)
    return best_gmm
//  [python]
def get_best_gmm2(X):  #early stopping gmm
    lowest_bic = np.infty
    bic = []
    cv_types = ['full']  #changed to only looking for full covariance
    best_gmm = GaussianMixture()
    for cv_type in cv_types:
        p = 1  #Decide a value
        n_comps = 1
        j = 0
        while j < p and n_comps < 5:  # if hasn't improved in p times, then stop. Do it for each cv type and take the minimum of all of them
            gmm = GaussianMixture(n_components=n_comps,
                                  covariance_type=cv_type)
            gmm.fit(X)
            bic.append(gmm.bic(X))
            if bic[-1] < lowest_bic:
                lowest_bic = bic[-1]
                best_gmm = gmm
                j = 0  #reset counter
            else:  #increment counter
                j += 1
            n_comps += 1

    bic = np.array(bic)
    return best_gmm
//  [python]
def test_fv_wrong_descriptor_types():
    """
    Test that DescriptorException is raised when the incorrect type for the
    descriptors is passed into the fisher_vector function.
    """
    try:
        from sklearn.mixture import GMM
    except ImportError:
        print(
            'scikit-learn is not installed. Please ensure it is installed in '
            'order to use the Fisher vector functionality.'
        )

    with pytest.raises(DescriptorException):
        fisher_vector([[1, 2, 3, 4]], GMM())
//  [python]
def test_fv_wrong_descriptor_types():
    """
    Test that DescriptorException is raised when the incorrect type for the
    descriptors is passed into the fisher_vector function.
    """
    try:
        from sklearn.mixture import GaussianMixture
    except ImportError:
        print(
            'scikit-learn is not installed. Please ensure it is installed in '
            'order to use the Fisher vector functionality.'
        )

    with pytest.raises(DescriptorException):
        fisher_vector([[1, 2, 3, 4]], GaussianMixture())
//  [python]
def __init__(self,
                 covariance_type:str="full",
                 random_state:int=0):
        self.covariance_type = covariance_type
        self.random_state = random_state
        self.__MODEL__ = mixture.GaussianMixture(covariance_type=covariance_type,
                                         random_state=random_state)
        self.processor = RegularDataProcessor
//  [python]
def __init__(self,
                 covariance_type:str="full",
                 random_state:int=0):
        self.covariance_type = covariance_type
        self.random_state = random_state
        self.__MODEL__ = GaussianMixture(covariance_type=covariance_type,
                                         random_state=random_state)
        self.processor = RegularDataProcessor
//  [python]
def eval_train(eval_loader, model, device, whichnet, queue):
    CE = nn.CrossEntropyLoss(reduction='none')
    model.eval()
    num_iter = (len(eval_loader.dataset) // eval_loader.batch_size) + 1
    losses = torch.zeros(len(eval_loader.dataset))
    with torch.no_grad():
        for batch_idx, (inputs, targets, index) in enumerate(eval_loader):
            inputs, targets = inputs.to(device), targets.to(device, non_blocking=True)
            outputs = model(inputs)
            loss = CE(outputs, targets)
            for b in range(inputs.size(0)):
                losses[index[b]] = loss[b]
            sys.stdout.write('\n')
            sys.stdout.write('|%s Evaluating loss Iter[%3d/%3d]\t' % (whichnet, batch_idx, num_iter))
            sys.stdout.flush()

    losses = (losses - losses.min()) / (losses.max() - losses.min())

    # fit a two-component GMM to the loss
    input_loss = losses.reshape(-1, 1)
    gmm = mixture.GaussianMixture(n_components=2, max_iter=10, tol=1e-2, reg_covar=1e-3)
    gmm.fit(input_loss)
    prob = gmm.predict_proba(input_loss)
    prob = prob[:, gmm.means_.argmin()]
    queue.put(prob)
//  [python]
def eval_train(eval_loader, model, device, whichnet, queue):
    CE = nn.CrossEntropyLoss(reduction='none')
    model.eval()
    num_iter = (len(eval_loader.dataset) // eval_loader.batch_size) + 1
    losses = torch.zeros(len(eval_loader.dataset))
    with torch.no_grad():
        for batch_idx, (inputs, targets, index) in enumerate(eval_loader):
            inputs, targets = inputs.to(device), targets.to(device, non_blocking=True)
            outputs = model(inputs)
            loss = CE(outputs, targets)
            for b in range(inputs.size(0)):
                losses[index[b]] = loss[b]
            sys.stdout.write('\n')
            sys.stdout.write('|%s Evaluating loss Iter[%3d/%3d]\t' % (whichnet, batch_idx, num_iter))
            sys.stdout.flush()

    losses = (losses - losses.min()) / (losses.max() - losses.min())

    # fit a two-component GMM to the loss
    input_loss = losses.reshape(-1, 1)
    gmm = GaussianMixture(n_components=2, max_iter=10, tol=1e-2, reg_covar=1e-3)
    gmm.fit(input_loss)
    prob = gmm.predict_proba(input_loss)
    prob = prob[:, gmm.means_.argmin()]
    queue.put(prob)
//  [python]
def train_using_python(self, input_data, iters=10):
        seed = 6
        np.random.seed(seed) 
        if self.components_seeded and self.clf is None:
            self.clf = mixture.GaussianMixture(n_components=self.M, covariance_type=self.cvtype, warm_start=True,\
                               max_iter=iters, random_state=seed, init_params='random',reg_covar=0.01,\
                               weights_init=self.components.weights, means_init=self.components.means)
#                               precisions_init = self.components.covars)

        elif not self.components_seeded and self.clf is None:
            self.clf = mixture.GaussianMixture(n_components=self.M, covariance_type=self.cvtype, warm_start=True,\
                               max_iter=iters, random_state=seed, init_params='random',reg_covar=0.01)
            
#        self.clf = mixture.GaussianMixture(n_components=self.M, covariance_type=self.cvtype,\
#                           max_iter=iters, random_state=seed, init_params='random')            

        self.clf.fit(input_data)
        return self.clf.means_, self.clf.covariances_
//  [python]
def train_using_python(self, input_data, iters=10):
        seed = 6
        np.random.seed(seed) 
        if self.components_seeded and self.clf is None:
            self.clf = GaussianMixture(n_components=self.M, covariance_type=self.cvtype, warm_start=True,\
                               max_iter=iters, random_state=seed, init_params='random',reg_covar=0.01,\
                               weights_init=self.components.weights, means_init=self.components.means)
#                               precisions_init = self.components.covars)

        elif not self.components_seeded and self.clf is None:
            self.clf = GaussianMixture(n_components=self.M, covariance_type=self.cvtype, warm_start=True,\
                               max_iter=iters, random_state=seed, init_params='random',reg_covar=0.01)
            
#        self.clf = GaussianMixture(n_components=self.M, covariance_type=self.cvtype,\
#                           max_iter=iters, random_state=seed, init_params='random')            

        self.clf.fit(input_data)
        return self.clf.means_, self.clf.covariances_
//  [python]
def estimate_driver_gmm(input_folder, max_len):
    data_paths = [f for f in os.listdir(input_folder) if f.endswith("pkl")]
    current_set = []
    for p in data_paths:
        pkl_path = os.path.join(input_folder, p)
        with open(pkl_path, "rb") as f:
            current_data = pkl.load(f)["current"]
            current_mask = np.zeros(max_len)
            duration = len(current_data)
            if duration > max_len:
                continue
            current_mask[0:duration] = current_data
            current_set.append(current_mask.tolist())
    X = np.array(current_set)
    gmm = mixture.GaussianMixture(n_components=15, random_state=0, max_iter=200).fit(X)
    return gmm