jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def clustering(vectors, mode, n_clusters, cluster_method, out_path, ind=False):
    index = []
    if mode == 'feature':
        vectors = vectors.T
        for i in range(len(vectors)):
            index.append('F%d' % (i + 1))
    else:
        for i in range(len(vectors)):
            index.append('S%d' % (i + 1))

    labels = np.zeros(len(vectors))
    if cluster_method == 'AP':
        ap = AffinityPropagation().fit(vectors)
        cluster_centers_indices = ap.cluster_centers_indices_
        labels = ap.labels_
        # plot_ap(vectors, cluster_centers_indices, labels, out_path, ind)
    elif cluster_method == 'DBSCAN':
        data = StandardScaler().fit_transform(vectors)
        db = DBSCAN().fit(data)
        labels = db.labels_
    elif cluster_method == 'GMM':
        gm = GaussianMixture(n_components=n_clusters).fit(vectors)
        labels = gm.predict(vectors)
    elif cluster_method == 'AGNES':
        # plot_hc(vectors, index, out_path, ind)
        connectivity = kneighbors_graph(vectors, n_neighbors=10, include_self=False)
        ward = AgglomerativeClustering(n_clusters=n_clusters, connectivity=connectivity,
                                       linkage='ward').fit(vectors)
        labels = ward.labels_
    elif cluster_method == 'Kmeans':
        labels = KMeans(n_clusters=n_clusters).fit_predict(vectors)

    res = []
    for i in range(len(vectors)):
        res.append([index[i], labels[i]])
    return res
//  [python]
def _gmm(self, data):
        n_clusters = self.params['n_clusters']
        cov = self.params['cov']
        estimator = mixture.GaussianMixture(n_components=n_clusters,
                                    covariance_type=cov, max_iter=500,
                                    random_state=0, n_init=5, init_params='kmeans')
        t0 = time.time()
        estimator.fit(data)
        runtime = time.time() - t0
        clust_flg = estimator.predict(data)
        clust_prob = estimator.predict_proba(data)
        return clust_flg, runtime, clust_prob, estimator
//  [python]
def _gmm(self, data):
        n_clusters = self.params['n_clusters']
        cov = self.params['cov']
        estimator = GaussianMixture(n_components=n_clusters,
                                    covariance_type=cov, max_iter=500,
                                    random_state=0, n_init=5, init_params='kmeans')
        t0 = time.time()
        estimator.fit(data)
        runtime = time.time() - t0
        clust_flg = estimator.predict(data)
        clust_prob = estimator.predict_proba(data)
        return clust_flg, runtime, clust_prob, estimator
//  [python]
def get_gmm_model(data: ndarray, hyp_time: int) -> object:
    """
    Estimates GMM model.

    Parameters
    ----------
    data : array-like of shape (n_samples, n_features)
        Training sample.

    hyp_time : int
        The runtime setting (in seconds) for Hyperopt optimization.

    Returns
    -------
    gmm : instance
        Fitted generative model.
    """
    if hyp_time == 0:
        gmm = mixture.GaussianMixture(random_state=42)
        gmm.fit(data)
    else:
        space = {"n_components": hp.quniform("n_components", 1, int(round(data.shape[0] / 3 * 2) - 1), 1),
                 "covariance_type": hp.choice("covariance_type", ['full', 'tied', 'diag', 'spherical'])}
        best = fmin(fn=lambda params: calc_gmm_acc(params, data), space=space,
                    algo=tpe.suggest, timeout=hyp_time, rstate=np.random.seed(42))
        best = space_eval(space, best)
        gmm = mixture.GaussianMixture(n_components=int(best["n_components"]), covariance_type=best['covariance_type'],
                              random_state=42)
        gmm.fit(data)
    return gmm
//  [python]
def get_gmm_model(data: ndarray, hyp_time: int) -> object:
    """
    Estimates GMM model.

    Parameters
    ----------
    data : array-like of shape (n_samples, n_features)
        Training sample.

    hyp_time : int
        The runtime setting (in seconds) for Hyperopt optimization.

    Returns
    -------
    gmm : instance
        Fitted generative model.
    """
    if hyp_time == 0:
        gmm = GaussianMixture(random_state=42)
        gmm.fit(data)
    else:
        space = {"n_components": hp.quniform("n_components", 1, int(round(data.shape[0] / 3 * 2) - 1), 1),
                 "covariance_type": hp.choice("covariance_type", ['full', 'tied', 'diag', 'spherical'])}
        best = fmin(fn=lambda params: calc_gmm_acc(params, data), space=space,
                    algo=tpe.suggest, timeout=hyp_time, rstate=np.random.seed(42))
        best = space_eval(space, best)
        gmm = GaussianMixture(n_components=int(best["n_components"]), covariance_type=best['covariance_type'],
                              random_state=42)
        gmm.fit(data)
    return gmm
//  [python]
def generate_distributions(feature_vectors, k):
    """
    Generate Mixed Gaussian mixture model with k components
    :param feature_vectors: Data
    :param k: Number of components to get GMM
    :return: sklearn GMM
    """
    gmm = mixture.GaussianMixture(k, covariance_type="diag")
    gmm.fit(feature_vectors)

    return gmm
//  [python]
def generate_distributions(feature_vectors, k):
    """
    Generate Mixed Gaussian mixture model with k components
    :param feature_vectors: Data
    :param k: Number of components to get GMM
    :return: sklearn GMM
    """
    gmm = GaussianMixture(k, covariance_type="diag")
    gmm.fit(feature_vectors)

    return gmm
//  [python]
def fit_gmm(data, prefix):

    # Fit GMM to histogram
    X, _ = np.histogram(data, normed=True, bins=10)
    X = np.expand_dims(X, 1)

    gmm = mixture.GMM(n_components=2).fit(X)

    # Get means / variances in order of ascending mean
    mu = np.array([gmm.means_[0][0], gmm.means_[1][0]])
    var = np.array([gmm.covariances_[0][0], gmm.covariances_[1][0]])
    order = np.argsort(mu)

    # Means/variances of each component
    mu_0, mu_1 = mu[order]
    var_0, var_1 = var[order]

    return {'{}_mu0'.format(prefix): mu_0,
            '{}_mu1'.format(prefix): mu_1,
            '{}_var0'.format(prefix): var_0[0],
            '{}_var1'.format(prefix): var_1[0]}
//  [python]
def fit_gmm(data, prefix):

    # Fit GMM to histogram
    X, _ = np.histogram(data, normed=True, bins=10)
    X = np.expand_dims(X, 1)

    gmm = GaussianMixture(n_components=2).fit(X)

    # Get means / variances in order of ascending mean
    mu = np.array([gmm.means_[0][0], gmm.means_[1][0]])
    var = np.array([gmm.covariances_[0][0], gmm.covariances_[1][0]])
    order = np.argsort(mu)

    # Means/variances of each component
    mu_0, mu_1 = mu[order]
    var_0, var_1 = var[order]

    return {'{}_mu0'.format(prefix): mu_0,
            '{}_mu1'.format(prefix): mu_1,
            '{}_var0'.format(prefix): var_0[0],
            '{}_var1'.format(prefix): var_1[0]}
//  [python]
n_gaussians = means.shape[0]

points = []
for i in range(len(means)):
    x = np.random.multivariate_normal(means[i], covs[i], N )
    points.append(x)
points = np.concatenate(points)

#fit the gaussian model
gmm = mixture.GMM(n_components=n_gaussians, covariance_type='diag')
//  [python]
n_gaussians = means.shape[0]

points = []
for i in range(len(means)):
    x = np.random.multivariate_normal(means[i], covs[i], N )
    points.append(x)
points = np.concatenate(points)

#fit the gaussian model
gmm = GaussianMixture(n_components=n_gaussians, covariance_type='diag')
//  [python]
def estimate_delta(lfc_means: list[np.ndarray], coef=0.6, min_thres=0.3):
    """Computes a threshold LFC value based on means of LFCs.

    Parameters
    ----------
    lfc_means
        LFC means for each gene, should be 1d.
    coef
        Tunable hyperparameter to choose the threshold based on estimated modes, defaults to 0.6
    min_thres
        Minimum returned threshold value, defaults to 0.3
    """
    logger.debug("Estimating delta from effect size samples")
    if lfc_means.ndim >= 2:
        raise ValueError("lfc_means should be 1-dimensional of shape: (n_genes,).")
    gmm = mixture.GMM(n_components=3)
    gmm.fit(lfc_means[:, None])
    vals = np.sort(gmm.means_.squeeze())
    res = coef * np.abs(vals[[0, -1]]).mean()
    res = np.maximum(min_thres, res)
    return res
//  [python]
def estimate_delta(lfc_means: list[np.ndarray], coef=0.6, min_thres=0.3):
    """Computes a threshold LFC value based on means of LFCs.

    Parameters
    ----------
    lfc_means
        LFC means for each gene, should be 1d.
    coef
        Tunable hyperparameter to choose the threshold based on estimated modes, defaults to 0.6
    min_thres
        Minimum returned threshold value, defaults to 0.3
    """
    logger.debug("Estimating delta from effect size samples")
    if lfc_means.ndim >= 2:
        raise ValueError("lfc_means should be 1-dimensional of shape: (n_genes,).")
    gmm = GaussianMixture(n_components=3)
    gmm.fit(lfc_means[:, None])
    vals = np.sort(gmm.means_.squeeze())
    res = coef * np.abs(vals[[0, -1]]).mean()
    res = np.maximum(min_thres, res)
    return res
//  [python]
rnd = np.random.RandomState(seed=17)  # create a random state with fixed seed for reproducibility

n_components = 5
gmm = mixture.GaussianMixture(n_components=n_components, random_state=rnd, covariance_type='diag')
//  [python]
rnd = np.random.RandomState(seed=17)  # create a random state with fixed seed for reproducibility

n_components = 5
gmm = GaussianMixture(n_components=n_components, random_state=rnd, covariance_type='diag')
//  [python]
def getGMM(filename):

    y, sr = librosa.load(filename)
    # 提取 MFCC feature
    mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=24, hop_length=160, win_length=240)
    print(mfccs.shape)

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

    y, sr = librosa.load(filename)
    # 提取 MFCC feature
    mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=24, hop_length=160, win_length=240)
    print(mfccs.shape)

    # 高斯混合模型 5个样本时，聚类数量为3，效果最好
    # 使用贝叶斯GMM，可避免数量选择
    gmm = GaussianMixture(1, covariance_type='full', random_state=0).fit(mfccs.T)
    return gmm
//  [python]
def __init__(self, n_components=1, initial_iter=20, **kwargs):
        if not _XDGMM_AVAILABLE:
            raise RuntimeError("You need to have both sklearn and extreme_deconvolution installed to use XDGMM")
        self._kwargs = kwargs
        self._gmm = mixture.GaussianMixture(int(n_components), max_iter=int(initial_iter), covariance_type="full")
//  [python]
def __init__(self, n_components=1, initial_iter=20, **kwargs):
        if not _XDGMM_AVAILABLE:
            raise RuntimeError("You need to have both sklearn and extreme_deconvolution installed to use XDGMM")
        self._kwargs = kwargs
        self._gmm = GaussianMixture(int(n_components), max_iter=int(initial_iter), covariance_type="full")
//  [python]
def eval_train_dividemix(args, model, epoch, eval_loader):
    model.eval()
    num_samples = args.num_batches*args.batch_size
    losses = torch.zeros(num_samples)
    paths = []
    n=0
    with torch.no_grad():
        for batch_idx, (inputs, targets, path) in enumerate(eval_loader):
            inputs, targets = inputs.cuda(), targets.cuda() 
            outputs = model(inputs)
            if len(targets.size())==1: # hard label, i.e., an int
                loss = F.cross_entropy(outputs, targets, reduction='none')
            else: # soft label
                loss = -torch.sum(F.log_softmax(outputs, dim=1)*targets, dim=1)

            for b in range(inputs.size(0)):
                losses[n]=loss[b] 
                paths.append(path[b])
                n+=1
            
    losses = (losses-losses.min())/(losses.max()-losses.min())    
    losses = losses.reshape(-1,1)
    gmm = mixture.GaussianMixture(n_components=2,max_iter=10,reg_covar=5e-4,tol=1e-2)
    gmm.fit(losses)
    prob = gmm.predict_proba(losses) 
    prob = prob[:,gmm.means_.argmin()]

    return prob, paths
//  [python]
def eval_train_dividemix(args, model, epoch, eval_loader):
    model.eval()
    num_samples = args.num_batches*args.batch_size
    losses = torch.zeros(num_samples)
    paths = []
    n=0
    with torch.no_grad():
        for batch_idx, (inputs, targets, path) in enumerate(eval_loader):
            inputs, targets = inputs.cuda(), targets.cuda() 
            outputs = model(inputs)
            if len(targets.size())==1: # hard label, i.e., an int
                loss = F.cross_entropy(outputs, targets, reduction='none')
            else: # soft label
                loss = -torch.sum(F.log_softmax(outputs, dim=1)*targets, dim=1)

            for b in range(inputs.size(0)):
                losses[n]=loss[b] 
                paths.append(path[b])
                n+=1
            
    losses = (losses-losses.min())/(losses.max()-losses.min())    
    losses = losses.reshape(-1,1)
    gmm = GaussianMixture(n_components=2,max_iter=10,reg_covar=5e-4,tol=1e-2)
    gmm.fit(losses)
    prob = gmm.predict_proba(losses) 
    prob = prob[:,gmm.means_.argmin()]

    return prob, paths
//  [python]
def initialize(self, x):
        gmm = mixture.GaussianMixture(n_components=self._n_clusters, n_init=5)
        self._y_pred = gmm.fit_predict(self._encoder.predict(x))
        self._model.get_layer(name='clustering').set_weights([gmm.means_])
        return None
//  [python]
def initialize(self, x):
        gmm = GaussianMixture(n_components=self._n_clusters, n_init=5)
        self._y_pred = gmm.fit_predict(self._encoder.predict(x))
        self._model.get_layer(name='clustering').set_weights([gmm.means_])
        return None
//  [python]
def prior_distribution(x, y):
    from sklearn.mixture import GMM
    import warnings

    coeff = y / x
    with warnings.catch_warnings():
        warnings.filterwarnings('ignore', category=DeprecationWarning)
        models = [GMM(i).fit(coeff) for i in np.arange(1, 4)]
    AIC = [m.aic(coeff) for m in models]
    # BIC = [m.bic(coeff) for m in models]
    gmm = models[np.argmin(AIC)]
    return gmm
//  [python]
def prior_distribution(x, y):
    from sklearn.mixture import GaussianMixture
    import warnings

    coeff = y / x
    with warnings.catch_warnings():
        warnings.filterwarnings('ignore', category=DeprecationWarning)
        models = [GaussianMixture(i).fit(coeff) for i in np.arange(1, 4)]
    AIC = [m.aic(coeff) for m in models]
    # BIC = [m.bic(coeff) for m in models]
    gmm = models[np.argmin(AIC)]
    return gmm
//  [python]
def main():
    Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST()
    dae = DeepAutoEncoder([500, 300, 2])
    dae.fit(Xtrain)
    mapping = dae.map2center(Xtrain)
    plt.scatter(mapping[:,0], mapping[:,1], c=Ytrain, s=100, alpha=0.5)
    plt.show()

    # purity measure from unsupervised machine learning pt 1
    # NOTE: this will take a long time (i.e. just leave it overnight)
    gmm = mixture.GaussianMixture(n_components=10)
    gmm.fit(Xtrain)
    print("Finished GMM training")
    responsibilities_full = gmm.predict_proba(Xtrain)
    print("full purity:", purity(Ytrain, responsibilities_full))

    gmm.fit(mapping)
    responsibilities_reduced = gmm.predict_proba(mapping)
    print("reduced purity:", purity(Ytrain, responsibilities_reduced))
//  [python]
def main():
    Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST()
    dae = DeepAutoEncoder([500, 300, 2])
    dae.fit(Xtrain)
    mapping = dae.map2center(Xtrain)
    plt.scatter(mapping[:,0], mapping[:,1], c=Ytrain, s=100, alpha=0.5)
    plt.show()

    # purity measure from unsupervised machine learning pt 1
    # NOTE: this will take a long time (i.e. just leave it overnight)
    gmm = GaussianMixture(n_components=10)
    gmm.fit(Xtrain)
    print("Finished GMM training")
    responsibilities_full = gmm.predict_proba(Xtrain)
    print("full purity:", purity(Ytrain, responsibilities_full))

    gmm.fit(mapping)
    responsibilities_reduced = gmm.predict_proba(mapping)
    print("reduced purity:", purity(Ytrain, responsibilities_reduced))
//  [python]
def init_gmm_parameters(self, x):
        g = mixture.GaussianMixture(n_components=self.n_clusters, covariance_type='diag')
        sample = self.encoder.predict(x)
        sample = np.split(sample, 2, axis=1)[0]
        g.fit(sample)
        self.u_p.assign(g.means_.T)
        self.lambda_p.assign(g.covariances_.T)
//  [python]
def init_gmm_parameters(self, x):
        g = GaussianMixture(n_components=self.n_clusters, covariance_type='diag')
        sample = self.encoder.predict(x)
        sample = np.split(sample, 2, axis=1)[0]
        g.fit(sample)
        self.u_p.assign(g.means_.T)
        self.lambda_p.assign(g.covariances_.T)
//  [python]
def fit(self, X: np.ndarray, y=None) -> "GMMOutlierDetector":
        """Fit the `BayesianGMMOutlierDetector` model using `X`, `y` as training data.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features )
            The training data.
        y : array-like of shape (n_samples,)
            Ignored, present for compatibility.

        Returns
        -------
        self : GMMOutlierDetector
            The fitted estimator.

        Raises
        ------
        ValueError
            - If `method="quantile"` and `threshold` is not between (0, 1).
            - If `method="stddev"` and `threshold` is negative.
            - If `method` is not in `["quantile", "stddev"]`.
        """
        # GMM sometimes throws an error if you don't do this
        X = check_array(X, estimator=self, dtype=FLOAT_DTYPES)
        if len(X.shape) == 1:
            X = np.expand_dims(X, 1)

        if (self.method == "quantile") and ((self.threshold > 1) or (self.threshold < 0)):
            raise ValueError(f"Threshold {self.threshold} with method {self.method} needs to be 0 < threshold < 1")
        if (self.method == "stddev") and (self.threshold < 0):
            raise ValueError(f"Threshold {self.threshold} with method {self.method} needs to be 0 < threshold ")
        if self.method not in self._ALLOWED_METHODS:
            raise ValueError(f"Method not recognised. Method must be in {self._ALLOWED_METHODS}")

        self.gmm_ = mixture.GaussianMixture(
            n_components=self.n_components,
            covariance_type=self.covariance_type,
            tol=self.tol,
            reg_covar=self.reg_covar,
            max_iter=self.max_iter,
            n_init=self.n_init,
            init_params=self.init_params,
            weights_init=self.weights_init,
            means_init=self.means_init,
            precisions_init=self.precisions_init,
            random_state=self.random_state,
            warm_start=self.warm_start,
            verbose=self.verbose,
            verbose_interval=self.verbose_interval,
        )
        self.gmm_.fit(X)
        score_samples = self.gmm_.score_samples(X)

        if self.method == "quantile":
            self.likelihood_threshold_ = np.quantile(score_samples, 1 - self.threshold)

        if self.method == "stddev":
            density = gaussian_kde(score_samples)
            max_x_value = minimize_scalar(lambda x: -density(x)).x
            mean_likelihood = score_samples.mean()
            new_likelihoods = score_samples[score_samples < max_x_value]
            new_likelihoods_std = np.std(new_likelihoods - mean_likelihood)
            self.likelihood_threshold_ = mean_likelihood - (self.threshold * new_likelihoods_std)

        return self
//  [python]
def fit(self, X: np.ndarray, y=None) -> "GMMOutlierDetector":
        """Fit the `BayesianGMMOutlierDetector` model using `X`, `y` as training data.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features )
            The training data.
        y : array-like of shape (n_samples,)
            Ignored, present for compatibility.

        Returns
        -------
        self : GMMOutlierDetector
            The fitted estimator.

        Raises
        ------
        ValueError
            - If `method="quantile"` and `threshold` is not between (0, 1).
            - If `method="stddev"` and `threshold` is negative.
            - If `method` is not in `["quantile", "stddev"]`.
        """
        # GMM sometimes throws an error if you don't do this
        X = check_array(X, estimator=self, dtype=FLOAT_DTYPES)
        if len(X.shape) == 1:
            X = np.expand_dims(X, 1)

        if (self.method == "quantile") and ((self.threshold > 1) or (self.threshold < 0)):
            raise ValueError(f"Threshold {self.threshold} with method {self.method} needs to be 0 < threshold < 1")
        if (self.method == "stddev") and (self.threshold < 0):
            raise ValueError(f"Threshold {self.threshold} with method {self.method} needs to be 0 < threshold ")
        if self.method not in self._ALLOWED_METHODS:
            raise ValueError(f"Method not recognised. Method must be in {self._ALLOWED_METHODS}")

        self.gmm_ = GaussianMixture(
            n_components=self.n_components,
            covariance_type=self.covariance_type,
            tol=self.tol,
            reg_covar=self.reg_covar,
            max_iter=self.max_iter,
            n_init=self.n_init,
            init_params=self.init_params,
            weights_init=self.weights_init,
            means_init=self.means_init,
            precisions_init=self.precisions_init,
            random_state=self.random_state,
            warm_start=self.warm_start,
            verbose=self.verbose,
            verbose_interval=self.verbose_interval,
        )
        self.gmm_.fit(X)
        score_samples = self.gmm_.score_samples(X)

        if self.method == "quantile":
            self.likelihood_threshold_ = np.quantile(score_samples, 1 - self.threshold)

        if self.method == "stddev":
            density = gaussian_kde(score_samples)
            max_x_value = minimize_scalar(lambda x: -density(x)).x
            mean_likelihood = score_samples.mean()
            new_likelihoods = score_samples[score_samples < max_x_value]
            new_likelihoods_std = np.std(new_likelihoods - mean_likelihood)
            self.likelihood_threshold_ = mean_likelihood - (self.threshold * new_likelihoods_std)

        return self
//  [python]
def reset_gmms(self):
        """initialize GMM models for both foreground and background.

        Here the number of components are determined by self.bg_ncomp
        and self.fg_ncomp; covariance_type of GMM is set as `spherical`;
        and warm_start is set as True for speeding up.
        """
        self.bg_gmms = []
        self.fg_gmms = []
        for i in range(3):
            if self.use_opencv_gmm:
                bgmodel = cv2.ml.EM_create()
                bgmodel.setClustersNumber(self.bg_ncomp[i])
                bgmodel.setCovarianceMatrixType(cv2.ml.EM_COV_MAT_SPHERICAL)
                self.bg_gmms.append(bgmodel)
                fgmodel = cv2.ml.EM_create()
                fgmodel.setClustersNumber(self.fg_ncomp[i])
                fgmodel.setCovarianceMatrixType(cv2.ml.EM_COV_MAT_SPHERICAL)
                self.fg_gmms.append(fgmodel)
            else:
                self.bg_gmms.append(
                    mixture.GMM(
                        n_components=self.bg_ncomp[i],
                        covariance_type='spherical',
                        init_params='kmeans',
                        n_init=1))
                self.fg_gmms.append(
                    mixture.GMM(
                        n_components=self.fg_ncomp[i],
                        covariance_type='spherical',
                        init_params='kmeans',
                        n_init=1))
        self._is_trained = False
//  [python]
def reset_gmms(self):
        """initialize GMM models for both foreground and background.

        Here the number of components are determined by self.bg_ncomp
        and self.fg_ncomp; covariance_type of GMM is set as `spherical`;
        and warm_start is set as True for speeding up.
        """
        self.bg_gmms = []
        self.fg_gmms = []
        for i in range(3):
            if self.use_opencv_gmm:
                bgmodel = cv2.ml.EM_create()
                bgmodel.setClustersNumber(self.bg_ncomp[i])
                bgmodel.setCovarianceMatrixType(cv2.ml.EM_COV_MAT_SPHERICAL)
                self.bg_gmms.append(bgmodel)
                fgmodel = cv2.ml.EM_create()
                fgmodel.setClustersNumber(self.fg_ncomp[i])
                fgmodel.setCovarianceMatrixType(cv2.ml.EM_COV_MAT_SPHERICAL)
                self.fg_gmms.append(fgmodel)
            else:
                self.bg_gmms.append(
                    mixture.GaussianMixture(
                        n_components=self.bg_ncomp[i],
                        covariance_type='spherical',
                        warm_start=True))
                self.fg_gmms.append(
                    mixture.GaussianMixture(
                        n_components=self.fg_ncomp[i],
                        covariance_type='spherical',
                        warm_start=True))
        self._is_trained = False
//  [python]
def update_target_speed(self, debug=False):
        # Get acceleration and speed
        states = np.array(self._state_list)
        time_stamp = states[:,0]
        speed = states[:,8]

        acceleration = (speed[1:]-speed[0:-1]) / ((time_stamp[1:]-time_stamp[0:-1])/1000.0)
        time_stamp = time_stamp[1:]
        speed = speed[1:]

        # get speed while it is stable and no vehicle ahead
        index = (abs(acceleration) < 1) & (states[1:, 9] == 100)
        stable_speed = speed[index]
        # select half of the highest speed
        stable_speed = np.sort(stable_speed)[::-1]
        stable_speed = stable_speed[0:stable_speed.size/4]

        if stable_speed.size > 50:
            gmm = mixture.GaussianMixture(n_components=3, covariance_type='diag')
            result = gmm.fit(stable_speed[:, np.newaxis])
            weights, means, covars = result.weights_, result.means_, result.covariances_

            # Select the gaussian distribution with the maximum mean
            max_index = np.argmax(means)
            mean, covar = means[max_index][0], covars[max_index][0]

            if debug:
                
                plt.hist(stable_speed, bins = stable_speed.size, ec='red', alpha=0.5, density=True)
                x = np.linspace(stable_speed[0], stable_speed[-1], stable_speed.size).reshape(-1,1)
                y1 = weights[0] * norm.pdf(x, means[0], np.sqrt(covars[0])).ravel()
                y2 = weights[1] * norm.pdf(x, means[1], np.sqrt(covars[1])).ravel()
                y3 = weights[2] * norm.pdf(x, means[2], np.sqrt(covars[2])).ravel()
                plt.plot(x, y1+y2+y3, c='black')
                plt.plot(x, y1, c='red')
                plt.plot(x, y2, c='green')
                plt.plot(x, y3, c='blue')
                plt.xlabel("Velocity m/s")
                plt.ylabel("Density of probability")

                plt.legend()
                plt.show()

            # get new value
            self.save_data(np.array([mean, covar]), "target_speed_train_data.csv")
            print("Target Speed Data Saved")
        else:
            print("No availiable speed data")
//  [python]
def update_target_speed(self, debug=False):
        # Get acceleration and speed
        states = np.array(self._state_list)
        time_stamp = states[:,0]
        speed = states[:,8]

        acceleration = (speed[1:]-speed[0:-1]) / ((time_stamp[1:]-time_stamp[0:-1])/1000.0)
        time_stamp = time_stamp[1:]
        speed = speed[1:]

        # get speed while it is stable and no vehicle ahead
        index = (abs(acceleration) < 1) & (states[1:, 9] == 100)
        stable_speed = speed[index]
        # select half of the highest speed
        stable_speed = np.sort(stable_speed)[::-1]
        stable_speed = stable_speed[0:stable_speed.size/4]

        if stable_speed.size > 50:
            gmm = GaussianMixture(n_components=3, covariance_type='diag')
            result = gmm.fit(stable_speed[:, np.newaxis])
            weights, means, covars = result.weights_, result.means_, result.covariances_

            # Select the gaussian distribution with the maximum mean
            max_index = np.argmax(means)
            mean, covar = means[max_index][0], covars[max_index][0]

            if debug:
                
                plt.hist(stable_speed, bins = stable_speed.size, ec='red', alpha=0.5, density=True)
                x = np.linspace(stable_speed[0], stable_speed[-1], stable_speed.size).reshape(-1,1)
                y1 = weights[0] * norm.pdf(x, means[0], np.sqrt(covars[0])).ravel()
                y2 = weights[1] * norm.pdf(x, means[1], np.sqrt(covars[1])).ravel()
                y3 = weights[2] * norm.pdf(x, means[2], np.sqrt(covars[2])).ravel()
                plt.plot(x, y1+y2+y3, c='black')
                plt.plot(x, y1, c='red')
                plt.plot(x, y2, c='green')
                plt.plot(x, y3, c='blue')
                plt.xlabel("Velocity m/s")
                plt.ylabel("Density of probability")

                plt.legend()
                plt.show()

            # get new value
            self.save_data(np.array([mean, covar]), "target_speed_train_data.csv")
            print("Target Speed Data Saved")
        else:
            print("No availiable speed data")
//  [python]
def _sample_rows_same(self, X):
    """ uses efficient sklearn implementation to sample from gaussian mixture -> only works if all rows of X are the same"""
    weights, locs, scales = self._get_mixture_components(np.expand_dims(X[0], axis=0))

    # make sure that sum of weights < 1
    weights = weights.astype(np.float64)
    weights = weights / np.sum(weights)

    gmm = mixture.GaussianMixture(n_components=self.n_centers, covariance_type='diag', max_iter=5, tol=1e-1)
    gmm.fit(np.random.normal(size=(100,self.ndim_y))) # just pretending a fit
    # overriding the GMM parameters with own params
    gmm.converged_ = True
    gmm.weights_ = weights[0]
    gmm.means_ = locs[0]
    gmm.covariances_ = scales[0]
    y_sample, _ = gmm.sample(X.shape[0])
    assert y_sample.shape == (X.shape[0], self.ndim_y)
    return X, y_sample
//  [python]
def _sample_rows_same(self, X):
    """ uses efficient sklearn implementation to sample from gaussian mixture -> only works if all rows of X are the same"""
    weights, locs, scales = self._get_mixture_components(np.expand_dims(X[0], axis=0))

    # make sure that sum of weights < 1
    weights = weights.astype(np.float64)
    weights = weights / np.sum(weights)

    gmm = GaussianMixture(n_components=self.n_centers, covariance_type='diag', max_iter=5, tol=1e-1)
    gmm.fit(np.random.normal(size=(100,self.ndim_y))) # just pretending a fit
    # overriding the GMM parameters with own params
    gmm.converged_ = True
    gmm.weights_ = weights[0]
    gmm.means_ = locs[0]
    gmm.covariances_ = scales[0]
    y_sample, _ = gmm.sample(X.shape[0])
    assert y_sample.shape == (X.shape[0], self.ndim_y)
    return X, y_sample
//  [python]
def cluster_gmm(z, K, on_data=True, random_state=None, **kwargs):
    '''
    Cluster z by a K-component full covariance Gaussian mixture model
    
    Inputs:
        z (Ndata x zdim np.array): Latent encodings
        K (int): Number of clusters
        on_data (bool): Compute cluster center as nearest point on the data manifold
        random_state (int or None): Random seed used for GMM clustering
        **kwargs: Additional keyword arguments passed to sklearn.mixture.GaussianMixture

    Returns: 
        np.array (Ndata,) of cluster labels
        np.array (K x zdim) of cluster centers
    '''
    clf = mixture.GaussianMixture(n_components=K, covariance_type='full', random_state=None, **kwargs)
    labels = clf.fit_predict(z)
    centers = clf.means_
    if on_data:
        centers, centers_ind = get_nearest_point(z, centers)
    return labels, centers
//  [python]
def cluster_gmm(z, K, on_data=True, random_state=None, **kwargs):
    '''
    Cluster z by a K-component full covariance Gaussian mixture model
    
    Inputs:
        z (Ndata x zdim np.array): Latent encodings
        K (int): Number of clusters
        on_data (bool): Compute cluster center as nearest point on the data manifold
        random_state (int or None): Random seed used for GMM clustering
        **kwargs: Additional keyword arguments passed to sklearn.mixture.GaussianMixture

    Returns: 
        np.array (Ndata,) of cluster labels
        np.array (K x zdim) of cluster centers
    '''
    clf = GaussianMixture(n_components=K, covariance_type='full', random_state=None, **kwargs)
    labels = clf.fit_predict(z)
    centers = clf.means_
    if on_data:
        centers, centers_ind = get_nearest_point(z, centers)
    return labels, centers
//  [python]
def get_gating_gmm(channel_name, datasource_name):
    global datasource
    global source
    global ball_tree
    global config

    packet_gmm = {}

    # Load if not loaded
    if datasource_name != source:
        load_ball_tree(datasource_name)
    description = datasource.describe().to_dict()

    column_data = datasource[channel_name].to_numpy()
    [hist, bin_edges] = np.histogram(column_data[~np.isnan(column_data)], bins=50, density=True)
    midpoints = (bin_edges[1:] + bin_edges[:-1]) / 2

    gmm = mixture.GMM(n_components=2)
    gmm.fit(column_data.reshape((-1, 1)))
    i0, i1 = np.argsort(gmm.means_[:, 0])
    packet_gmm['gate'] = np.mean(gmm.means_)

    pdf_gmm1 = [gmm.weights_[i0] * norm.pdf(midpoints, gmm.means_[i0], np.sqrt(gmm.covariances_[i0]))][0][0]
    pdf_gmm2 = [gmm.weights_[i1] * norm.pdf(midpoints, gmm.means_[i1], np.sqrt(gmm.covariances_[i1]))][0][0]

    dat_gmm1 = []
    dat_gmm2 = []
    for i in range(len(hist)):
        obj1 = {}
        obj1['x'] = midpoints[i]
        obj1['y'] = pdf_gmm1[i]
        dat_gmm1.append(obj1)

        obj2 = {}
        obj2['x'] = midpoints[i]
        obj2['y'] = pdf_gmm2[i]
        dat_gmm2.append(obj2)

    packet_gmm['gmm_1'] = dat_gmm1
    packet_gmm['gmm_2'] = dat_gmm2

    return packet_gmm
//  [python]
def get_gating_gmm(channel_name, datasource_name):
    global datasource
    global source
    global ball_tree
    global config

    packet_gmm = {}

    # Load if not loaded
    if datasource_name != source:
        load_ball_tree(datasource_name)
    description = datasource.describe().to_dict()

    column_data = datasource[channel_name].to_numpy()
    [hist, bin_edges] = np.histogram(column_data[~np.isnan(column_data)], bins=50, density=True)
    midpoints = (bin_edges[1:] + bin_edges[:-1]) / 2

    gmm = GaussianMixture(n_components=2)
    gmm.fit(column_data.reshape((-1, 1)))
    i0, i1 = np.argsort(gmm.means_[:, 0])
    packet_gmm['gate'] = np.mean(gmm.means_)

    pdf_gmm1 = [gmm.weights_[i0] * norm.pdf(midpoints, gmm.means_[i0], np.sqrt(gmm.covariances_[i0]))][0][0]
    pdf_gmm2 = [gmm.weights_[i1] * norm.pdf(midpoints, gmm.means_[i1], np.sqrt(gmm.covariances_[i1]))][0][0]

    dat_gmm1 = []
    dat_gmm2 = []
    for i in range(len(hist)):
        obj1 = {}
        obj1['x'] = midpoints[i]
        obj1['y'] = pdf_gmm1[i]
        dat_gmm1.append(obj1)

        obj2 = {}
        obj2['x'] = midpoints[i]
        obj2['y'] = pdf_gmm2[i]
        dat_gmm2.append(obj2)

    packet_gmm['gmm_1'] = dat_gmm1
    packet_gmm['gmm_2'] = dat_gmm2

    return packet_gmm
//  [python]
def xgmeans(X, rng=(1, 10), random_state=None):
    """
    Gaussian mixture with BIC to select the optimal number
    of components

    Args:
        X (np.array): data to cluster
        rng (tuple): lower and upper bound on the number of components
        random_state (int/None): random state to use

    Returns:
        sklearn.GaussianMixture: Gaussian mixture model with the
                                    lowest BIC score
    """
    best_bic = np.inf
    best_mixture = None

    # do model fitting for all n_components in the specified range
    for n_components in range(rng[0], min([rng[1], len(X)])):
        gmm = mixture.GaussianMixture(n_components=n_components, random_state=random_state).fit(
            X
        )
        bic = gmm.bic(X)
        if bic < best_bic:
            best_bic = bic
            best_mixture = gmm

    return best_mixture
//  [python]
def xgmeans(X, rng=(1, 10), random_state=None):
    """
    Gaussian mixture with BIC to select the optimal number
    of components

    Args:
        X (np.array): data to cluster
        rng (tuple): lower and upper bound on the number of components
        random_state (int/None): random state to use

    Returns:
        sklearn.GaussianMixture: Gaussian mixture model with the
                                    lowest BIC score
    """
    best_bic = np.inf
    best_mixture = None

    # do model fitting for all n_components in the specified range
    for n_components in range(rng[0], min([rng[1], len(X)])):
        gmm = GaussianMixture(n_components=n_components, random_state=random_state).fit(
            X
        )
        bic = gmm.bic(X)
        if bic < best_bic:
            best_bic = bic
            best_mixture = gmm

    return best_mixture
//  [python]
def fit_gmm(self, net):
        losses = []
        net.eval()

        with torch.no_grad():
            for batch_idx, (inputs, targets, items, idxs) in enumerate(self.ldr_eval):
                inputs, targets = inputs.to(self.args.device), targets.to(self.args.device)
                outputs = net(inputs)
                loss = self.CE(outputs, targets)
                losses.append(loss)

        losses = torch.cat(losses).cpu().numpy()
        losses = (losses - losses.min()) / (losses.max() - losses.min())
        input_loss = losses.reshape(-1, 1)

        gmm = mixture.GaussianMixture(n_components=2, max_iter=100, tol=1e-2, reg_covar=5e-4)
        gmm.fit(input_loss)
        prob = gmm.predict_proba(input_loss)
        prob = prob[:, gmm.means_.argmin()]

        return prob
//  [python]
def fit_gmm(self, net):
        losses = []
        net.eval()

        with torch.no_grad():
            for batch_idx, (inputs, targets, items, idxs) in enumerate(self.ldr_eval):
                inputs, targets = inputs.to(self.args.device), targets.to(self.args.device)
                outputs = net(inputs)
                loss = self.CE(outputs, targets)
                losses.append(loss)

        losses = torch.cat(losses).cpu().numpy()
        losses = (losses - losses.min()) / (losses.max() - losses.min())
        input_loss = losses.reshape(-1, 1)

        gmm = GaussianMixture(n_components=2, max_iter=100, tol=1e-2, reg_covar=5e-4)
        gmm.fit(input_loss)
        prob = gmm.predict_proba(input_loss)
        prob = prob[:, gmm.means_.argmin()]

        return prob
//  [python]
def gmm(points, k, type='diag'):  # points: out_flatten
    from sklearn.mixture import GMM
    import scipy.stats

    gmm = GMM(n_components=k, covariance_type=type, max_iter=100).fit(points)
    labels = gmm.predict(points)
    print('weight:', gmm.weights_)

    centers = np.empty(shape=(gmm.n_components, points.shape[1]))
    for i in range(gmm.n_components):
        desity = scipy.stats.multivariate_normal.pdf(points, mean=gmm.means_[i], cov=gmm.covariances_[i], allow_singular=True)
        centers[i, :] = points[np.argmax(desity)]
    return centers, labels
//  [python]
def gmm(points, k, type='diag'):  # points: out_flatten
    from sklearn.mixture import GaussianMixture
    import scipy.stats

    gmm = GaussianMixture(n_components=k, covariance_type=type, max_iter=100).fit(points)
    labels = gmm.predict(points)
    print('weight:', gmm.weights_)

    centers = np.empty(shape=(gmm.n_components, points.shape[1]))
    for i in range(gmm.n_components):
        desity = scipy.stats.multivariate_normal.pdf(points, mean=gmm.means_[i], cov=gmm.covariances_[i], allow_singular=True)
        centers[i, :] = points[np.argmax(desity)]
    return centers, labels
//  [python]
def update_probabilties_split_data_indices(self, model, loss_history):
        model.eval()
        losses_lst = []
        idx_lst = []

        with torch.no_grad():
            for batch_idx, (inputs, targets, items, idxs) in enumerate(self.ldr_eval):
                inputs, targets = inputs.to(self.args.device), targets.to(self.args.device)
                outputs = model(inputs)
                losses_lst.append(self.CE(outputs, targets))
                idx_lst.append(idxs.cpu().numpy())

        indices = np.concatenate(idx_lst)
        losses = torch.cat(losses_lst).cpu().numpy()
        losses = (losses - losses.min()) / (losses.max() - losses.min())
        loss_history.append(losses)

        # Fit a two-component GMM to the loss
        input_loss = losses.reshape(-1, 1)
        gmm = mixture.GMM(n_components=2, covariance_type='full', n_iter=100, tol=1e-2, random_state=0)
        gmm.fit(input_loss)
        prob = gmm.predict_proba(input_loss)
        prob = prob[:, gmm.means_.argmin()]

        # Split data to labeled, unlabeled dataset
        pred = (prob > self.args.p_threshold)
        label_idx = pred.nonzero()[0]
        label_idx = indices[label_idx]

        unlabel_idx = (1 - pred).nonzero()[0]
        unlabel_idx = indices[unlabel_idx]

        # Data index : probability
        prob_dict = {idx: prob for idx, prob in zip(indices, prob)}

        return prob_dict, label_idx, unlabel_idx
//  [python]
def update_probabilties_split_data_indices(self, model, loss_history):
        model.eval()
        losses_lst = []
        idx_lst = []

        with torch.no_grad():
            for batch_idx, (inputs, targets, items, idxs) in enumerate(self.ldr_eval):
                inputs, targets = inputs.to(self.args.device), targets.to(self.args.device)
                outputs = model(inputs)
                losses_lst.append(self.CE(outputs, targets))
                idx_lst.append(idxs.cpu().numpy())

        indices = np.concatenate(idx_lst)
        losses = torch.cat(losses_lst).cpu().numpy()
        losses = (losses - losses.min()) / (losses.max() - losses.min())
        loss_history.append(losses)

        # Fit a two-component GMM to the loss
        input_loss = losses.reshape(-1, 1)
        gmm = GaussianMixture(n_components=2, max_iter=100, tol=1e-2, reg_covar=5e-4)
        gmm.fit(input_loss)
        prob = gmm.predict_proba(input_loss)
        prob = prob[:, gmm.means_.argmin()]

        # Split data to labeled, unlabeled dataset
        pred = (prob > self.args.p_threshold)
        label_idx = pred.nonzero()[0]
        label_idx = indices[label_idx]

        unlabel_idx = (1 - pred).nonzero()[0]
        unlabel_idx = indices[unlabel_idx]

        # Data index : probability
        prob_dict = {idx: prob for idx, prob in zip(indices, prob)}

        return prob_dict, label_idx, unlabel_idx
//  [python]
def get_channel_gmm(channel_name, datasource_name):
    global datasource
    global source
    global ball_tree
    global config

    packet_gmm = {}

    # Load if not loaded
    if datasource_name != source:
        load_ball_tree(datasource_name)

    image_channelIdx = next(
        index for (index, d) in enumerate(config[datasource_name]['imageData']) if d["fullname"] == channel_name) - 1
    image_data = zarray[image_channelIdx]
    img_log = np.log(image_data[image_data > 0])
    gmm = mixture.GMM(3, max_iter=1000, tol=1e-6)
    gmm.fit(img_log.reshape((-1, 1)))

    means = gmm.means_[:, 0]
    i0, i1, i2 = np.argsort(means)
    mean1, mean2 = means[[i1, i2]]
    std1, std2 = gmm.covariances_[[i1, i2], 0, 0] ** 0.5

    x = np.linspace(mean1, mean2, 50)
    y1 = norm(mean1, std1).pdf(x) * gmm.weights_[i1]
    y2 = norm(mean2, std2).pdf(x) * gmm.weights_[i2]

    lmax = mean2 + 2 * std2
    lmin = x[np.argmin(np.abs(y1 - y2))]
    if lmin >= mean2:
        lmin = mean2 - 2 * std2
    vmin = max(np.exp(lmin), image_data.min(), 0)
    vmax = min(np.exp(lmax), image_data.max())

    packet_gmm['vmin'] = np.rint(vmin)
    packet_gmm['vmax'] = np.rint(vmax)

    [hist, bin_edges] = np.histogram(img_log.flatten(), bins=50, density=True)
    midpoints = (bin_edges[1:] + bin_edges[:-1]) / 2

    covars = gmm.covariances_[:, 0, 0]
    weights = gmm.weights_
    pdf_gmm1 = weights[i0] * norm.pdf(midpoints, means[i0], np.sqrt(covars[i0]))
    pdf_gmm2 = weights[i1] * norm.pdf(midpoints, means[i1], np.sqrt(covars[i1]))
    pdf_gmm3 = weights[i2] * norm.pdf(midpoints, means[i2], np.sqrt(covars[i2]))

    dat_gmm1 = []
    dat_gmm2 = []
    dat_gmm3 = []
    for i in range(len(hist)):
        obj1 = {}
        obj1['x'] = midpoints[i]
        obj1['y'] = pdf_gmm1[i]
        dat_gmm1.append(obj1)

        obj2 = {}
        obj2['x'] = midpoints[i]
        obj2['y'] = pdf_gmm2[i]
        dat_gmm2.append(obj2)

        obj3 = {}
        obj3['x'] = midpoints[i]
        obj3['y'] = pdf_gmm3[i]
        dat_gmm3.append(obj3)

    packet_gmm['image_gmm_1'] = dat_gmm1
    packet_gmm['image_gmm_2'] = dat_gmm2
    packet_gmm['image_gmm_3'] = dat_gmm3

    return packet_gmm
//  [python]
def get_channel_gmm(channel_name, datasource_name):
    global datasource
    global source
    global ball_tree
    global config

    packet_gmm = {}

    # Load if not loaded
    if datasource_name != source:
        load_ball_tree(datasource_name)

    image_channelIdx = next(
        index for (index, d) in enumerate(config[datasource_name]['imageData']) if d["fullname"] == channel_name) - 1
    image_data = zarray[image_channelIdx]
    img_log = np.log(image_data[image_data > 0])
    gmm = GaussianMixture(3, max_iter=1000, tol=1e-6)
    gmm.fit(img_log.reshape((-1, 1)))

    means = gmm.means_[:, 0]
    i0, i1, i2 = np.argsort(means)
    mean1, mean2 = means[[i1, i2]]
    std1, std2 = gmm.covariances_[[i1, i2], 0, 0] ** 0.5

    x = np.linspace(mean1, mean2, 50)
    y1 = norm(mean1, std1).pdf(x) * gmm.weights_[i1]
    y2 = norm(mean2, std2).pdf(x) * gmm.weights_[i2]

    lmax = mean2 + 2 * std2
    lmin = x[np.argmin(np.abs(y1 - y2))]
    if lmin >= mean2:
        lmin = mean2 - 2 * std2
    vmin = max(np.exp(lmin), image_data.min(), 0)
    vmax = min(np.exp(lmax), image_data.max())

    packet_gmm['vmin'] = np.rint(vmin)
    packet_gmm['vmax'] = np.rint(vmax)

    [hist, bin_edges] = np.histogram(img_log.flatten(), bins=50, density=True)
    midpoints = (bin_edges[1:] + bin_edges[:-1]) / 2

    covars = gmm.covariances_[:, 0, 0]
    weights = gmm.weights_
    pdf_gmm1 = weights[i0] * norm.pdf(midpoints, means[i0], np.sqrt(covars[i0]))
    pdf_gmm2 = weights[i1] * norm.pdf(midpoints, means[i1], np.sqrt(covars[i1]))
    pdf_gmm3 = weights[i2] * norm.pdf(midpoints, means[i2], np.sqrt(covars[i2]))

    dat_gmm1 = []
    dat_gmm2 = []
    dat_gmm3 = []
    for i in range(len(hist)):
        obj1 = {}
        obj1['x'] = midpoints[i]
        obj1['y'] = pdf_gmm1[i]
        dat_gmm1.append(obj1)

        obj2 = {}
        obj2['x'] = midpoints[i]
        obj2['y'] = pdf_gmm2[i]
        dat_gmm2.append(obj2)

        obj3 = {}
        obj3['x'] = midpoints[i]
        obj3['y'] = pdf_gmm3[i]
        dat_gmm3.append(obj3)

    packet_gmm['image_gmm_1'] = dat_gmm1
    packet_gmm['image_gmm_2'] = dat_gmm2
    packet_gmm['image_gmm_3'] = dat_gmm3

    return packet_gmm
//  [python]
def _discretize_by_gmm(col, num_bins, random_state):
    nan_idx = col[col.isnull()].index
    gmm = mixture.GMM(n_components=num_bins,covariance_type='full',random_state=random_state)
    gmm = gmm.fit(X=np.expand_dims(col.dropna(), 1))
    if col.isnull().sum() == 0:
        group = gmm.predict(X=np.expand_dims(col, 1))
    else:
        group = gmm.predict(X=np.expand_dims(col.dropna(), 1)).astype(float)
        for idx in nan_idx:
            group = np.insert(group,idx,np.nan)
    return pd.Series(group)
//  [python]
def _discretize_by_gmm(col, num_bins, random_state):
    nan_idx = col[col.isnull()].index
    gmm = GaussianMixture(n_components=num_bins,covariance_type='full',random_state=random_state)
    gmm = gmm.fit(X=np.expand_dims(col.dropna(), 1))
    if col.isnull().sum() == 0:
        group = gmm.predict(X=np.expand_dims(col, 1))
    else:
        group = gmm.predict(X=np.expand_dims(col.dropna(), 1)).astype(float)
        for idx in nan_idx:
            group = np.insert(group,idx,np.nan)
    return pd.Series(group)