jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def fit(self, x, random_state=None, tol=1e-3, min_covar=1e-3,
            n_iter=100, n_init=1, params='wmc', init_params='wmc'):
        """
        Estimate model parameters with the expectation-maximization algorithm.

        A initialization step is performed before entering the em
        algorithm. If you want to avoid this step, set the keyword
        argument init_params to the empty string '' when creating the
        GMM object. Likewise, if you would like just to do an
        initialization, set n_iter=0.

        Parameters
        ----------
        x : array_like, shape (n, n_features)
            List of n_features-dimensional data points.  Each row corresponds
            to a single data point.
        random_state: RandomState or an int seed (0 by default)
            A random number generator instance.
        min_covar : float, optional
            Floor on the diagonal of the covariance matrix to prevent
            overfitting.
        tol : float, optional
            Convergence threshold. EM iterations will stop when average
            gain in log-likelihood is below this threshold.
        n_iter : int, optional
            Number of EM iterations to perform.
        n_init : int, optional
            Number of initializations to perform, the best results is kept.
        params : str, optional
            Controls which parameters are updated in the training process.
            Can contain any combination of 'w' for weights, 'm' for means,
            and 'c' for covars.
        init_params : str, optional
            Controls which parameters are updated in the initialization
            process.  Can contain any combination of 'w' for weights,
            'm' for means, and 'c' for covars.

        """
        import sklearn.mixture
        # first initialise a sklearn.mixture.GMM object
        gmm = sklearn.mixture.GMM(n_components=self.n_components,
                                  covariance_type=self.covariance_type,
                                  random_state=random_state, tol=tol,
                                  min_covar=min_covar, n_iter=n_iter,
                                  n_init=n_init, params=params,
                                  init_params=init_params)
        # fit this GMM
        gmm.fit(x)
        # copy the needed information
        self.covars = gmm.covars_
        self.means = gmm.means_
        self.weights = gmm.weights_
        # and return self
        return self
//  [python]
def fit(self, x, random_state=None, tol=1e-3, min_covar=1e-3,
            n_iter=100, n_init=1, params='wmc', init_params='wmc'):
        """
        Estimate model parameters with the expectation-maximization algorithm.

        A initialization step is performed before entering the em
        algorithm. If you want to avoid this step, set the keyword
        argument init_params to the empty string '' when creating the
        GMM object. Likewise, if you would like just to do an
        initialization, set n_iter=0.

        Parameters
        ----------
        x : array_like, shape (n, n_features)
            List of n_features-dimensional data points.  Each row corresponds
            to a single data point.
        random_state: RandomState or an int seed (0 by default)
            A random number generator instance.
        min_covar : float, optional
            Floor on the diagonal of the covariance matrix to prevent
            overfitting.
        tol : float, optional
            Convergence threshold. EM iterations will stop when average
            gain in log-likelihood is below this threshold.
        n_iter : int, optional
            Number of EM iterations to perform.
        n_init : int, optional
            Number of initializations to perform, the best results is kept.
        params : str, optional
            Controls which parameters are updated in the training process.
            Can contain any combination of 'w' for weights, 'm' for means,
            and 'c' for covars.
        init_params : str, optional
            Controls which parameters are updated in the initialization
            process.  Can contain any combination of 'w' for weights,
            'm' for means, and 'c' for covars.

        """
        import sklearn.mixture
        # first initialise a sklearn.mixture.GMM object
        gmm = sklearn.mixture.GaussianMixture(n_components=self.n_components,
                                  covariance_type=self.covariance_type,
                                  random_state=random_state, tol=tol,
                                  min_covar=min_covar, n_iter=n_iter,
                                  n_init=n_init, params=params,
                                  init_params=init_params)
        # fit this GMM
        gmm.fit(x)
        # copy the needed information
        self.covars = gmm.covariances_
        self.means = gmm.means_
        self.weights = gmm.weights_
        # and return self
        return self
//  [python]
def get_gmm_metastable_graph(data, n_states=None, covariance_type='diag',
                             n_init=30, 
                             connection_estimation_method='max_path_distance_diff', 
                             min_paths=3, memberships=None):

    if covariance_type not in ['diag', 'spherical', 'full']:
        raise ValueError('Invalid covariance type %s' % covariance_type)
    if memberships is None:
        if n_states is None:
            MAX_STATES = 10
            MIN_SAMPLES = 3
            bics = np.zeros([MAX_STATES])
            bics[:] = np.inf
            for curr_states in range(2, MAX_STATES):
                gmm = GMM(n_components=curr_states, params='mc', 
                        covariance_type=covariance_type, n_init=n_init)
                gmm.fit(data)
                found_singleton = False
                for state, sample_count in Counter(gmm.predict(data)).iteritems():
                    if sample_count < MIN_SAMPLES:
                        found_singleton = True
                        break
                if not found_singleton:
                    bics[curr_states] = gmm.bic(data)
            n_states = max(1, int(np.argmin(bics)) - 1)

        gmm = GMM(n_components=n_states, params='mc', 
            covariance_type=covariance_type, n_init=n_init)
        gmm.fit(data)
    else:
        gmm = _gmm_from_memberships(data, memberships, covariance_type)
    gmm.centroids = gmm.means_
    state_edges = utils.get_metastable_connections_from_gmm(data, gmm, 
                                                            connection_estimation_method=connection_estimation_method,
                                                            min_paths=min_paths)
    return MetastableGraph(gmm, [(utils.create_state_index(i), utils.create_state_index(j)) for i, j in state_edges])
//  [python]
def get_gmm_metastable_graph(data, n_states=None, covariance_type='diag',
                             n_init=30, 
                             connection_estimation_method='max_path_distance_diff', 
                             min_paths=3, memberships=None):

    if covariance_type not in ['diag', 'spherical', 'full']:
        raise ValueError('Invalid covariance type %s' % covariance_type)
    if memberships is None:
        if n_states is None:
            MAX_STATES = 10
            MIN_SAMPLES = 3
            bics = np.zeros([MAX_STATES])
            bics[:] = np.inf
            for curr_states in range(2, MAX_STATES):
                gmm = GaussianMixture(n_components=curr_states, covariance_type=covariance_type, n_init=n_init)
                gmm.fit(data)
                found_singleton = False
                for state, sample_count in Counter(gmm.predict(data)).iteritems():
                    if sample_count < MIN_SAMPLES:
                        found_singleton = True
                        break
                if not found_singleton:
                    bics[curr_states] = gmm.bic(data)
            n_states = max(1, int(np.argmin(bics)) - 1)

        gmm = GaussianMixture(n_components=n_states, covariance_type=covariance_type, n_init=n_init)
        gmm.fit(data)
    else:
        gmm = _gmm_from_memberships(data, memberships, covariance_type)
    gmm.centroids = gmm.means_
    state_edges = utils.get_metastable_connections_from_gmm(data, gmm, 
                                                            connection_estimation_method=connection_estimation_method,
                                                            min_paths=min_paths)
    return MetastableGraph(gmm, [(utils.create_state_index(i), utils.create_state_index(j)) for i, j in state_edges])
//  [python]
def process(self):
        females, males = self.get_file_paths(self.females_training_path,
                                             self.males_training_path)
        # collect voice features
        female_voice_features = self.collect_features(females)
        male_voice_features   = self.collect_features(males)
        # generate gaussian mixture models
        females_gmm = GMM(n_components = 16, n_iter = 200, covariance_type='diag', n_init = 3)
        males_gmm   = GMM(n_components = 16, n_iter = 200, covariance_type='diag', n_init = 3)
        # fit features to models
        females_gmm.fit(female_voice_features)
        males_gmm.fit(male_voice_features)
        # save models
        self.save_gmm(females_gmm, "females")
        self.save_gmm(males_gmm,   "males")
//  [python]
def process(self):
        females, males = self.get_file_paths(self.females_training_path,
                                             self.males_training_path)
        # collect voice features
        female_voice_features = self.collect_features(females)
        male_voice_features   = self.collect_features(males)
        # generate gaussian mixture models
        females_gmm = GaussianMixture(n_components = 16, n_init = 3, max_iter = 200, covariance_type='diag')
        males_gmm   = GaussianMixture(n_components = 16, n_init = 3, max_iter = 200, covariance_type='diag')
        # fit features to models
        females_gmm.fit(female_voice_features)
        males_gmm.fit(male_voice_features)
        # save models
        self.save_gmm(females_gmm, "females")
        self.save_gmm(males_gmm,   "males")
//  [python]
def estimate_error_rate(freq):
    gmm_bics = list()
    for n in range(1, min(21, freq.shape[0]+1)):
        gmm = sklearn.mixture.GMM(n_components=n, covariance_type='spherical')
        gmm.fit(freq)
        gmm_bics.append((gmm.bic(freq), n))
    n = sorted(gmm_bics, key=lambda a: a[0])[0][1]
    gmm = sklearn.mixture.GMM(n_components=n, covariance_type='spherical')
    gmm.fit(freq)
    return np.sqrt(np.mean(gmm.covars_))
//  [python]
def estimate_error_rate(freq):
    gmm_bics = list()
    for n in range(1, min(21, freq.shape[0]+1)):
        gmm = sklearn.mixture.GaussianMixture(n_components=n, covariance_type='spherical')
        gmm.fit(freq)
        gmm_bics.append((gmm.bic(freq), n))
    n = sorted(gmm_bics, key=lambda a: a[0])[0][1]
    gmm = sklearn.mixture.GaussianMixture(n_components=n, covariance_type='spherical')
    gmm.fit(freq)
    return np.sqrt(np.mean(gmm.covariances_))
//  [python]
def __init__(self, **kwargs):
        self._model = sklearn.mixture.GMM(**kwargs)
//  [python]
def __init__(self, **kwargs):
        self._model = sklearn.mixture.GaussianMixture(**kwargs)
//  [python]
def compute_GMM_results(components, attributes):
    clfs = []

    for attr, X in zip(attributes, Xarrays):
        clfs_i = []

        for comp in components:
            print("  - %i component fit" % comp)
            clf = GMM(comp, covariance_type='full',
                      random_state=0, n_iter=500)
            clf.fit(X)
            clfs_i.append(clf)

            if not clf.converged_:
                print("           NOT CONVERGED!")

        clfs.append(clfs_i)
    return clfs
//  [python]
def compute_GMM_results(components, attributes):
    clfs = []

    for attr, X in zip(attributes, Xarrays):
        clfs_i = []

        for comp in components:
            print("  - %i component fit" % comp)
            clf = GaussianMixture(comp, covariance_type='full',
                      random_state=0, n_init=500)
            clf.fit(X)
            clfs_i.append(clf)

            if not clf.converged_:
                print("           NOT CONVERGED!")

        clfs.append(clfs_i)
    return clfs
//  [python]
def train(args):
    print("Loading embeddings.")
    fname = "{}/labels.csv".format(args.workDir)
    labels = pd.read_csv(fname, header=None).as_matrix()[:, 1]
    labels = map(itemgetter(1),
                 map(os.path.split,
                     map(os.path.dirname, labels)))  # Get the directory.
    fname = "{}/reps.csv".format(args.workDir)
    embeddings = pd.read_csv(fname, header=None).as_matrix()
    le = LabelEncoder().fit(labels)
    labelsNum = le.transform(labels)
    nClasses = len(le.classes_)
    print("Training for {} classes.".format(nClasses))

    if args.classifier == 'LinearSvm':
        clf = SVC(C=1, kernel='linear', probability=True)
    elif args.classifier == 'GridSearchSvm':
        print("""
        Warning: In our experiences, using a grid search over SVM hyper-parameters only
        gives marginally better performance than a linear SVM with C=1 and
        is not worth the extra computations of performing a grid search.
        """)
        param_grid = [
            {'C': [1, 10, 100, 1000],
             'kernel': ['linear']},
            {'C': [1, 10, 100, 1000],
             'gamma': [0.001, 0.0001],
             'kernel': ['rbf']}
        ]
        clf = GridSearchCV(SVC(C=1, probability=True), param_grid, cv=5)
    elif args.classifier == 'GMM':  # Doesn't work best
        clf = GMM(n_components=nClasses)

    # ref:
    # http://scikit-learn.org/stable/auto_examples/classification/plot_classifier_comparison.html#example-classification-plot-classifier-comparison-py
    elif args.classifier == 'RadialSvm':  # Radial Basis Function kernel
        # works better with C = 1 and gamma = 2
        clf = SVC(C=1, kernel='rbf', probability=True, gamma=2)
    elif args.classifier == 'DecisionTree':  # Doesn't work best
        clf = DecisionTreeClassifier(max_depth=20)
    elif args.classifier == 'GaussianNB':
        clf = GaussianNB()

    # ref: https://jessesw.com/Deep-Learning/
    elif args.classifier == 'DBN':
        from nolearn.dbn import DBN
        clf = DBN([embeddings.shape[1], 500, labelsNum[-1:][0] + 1],  # i/p nodes, hidden nodes, o/p nodes
                  learn_rates=0.3,
                  # Smaller steps mean a possibly more accurate result, but the
                  # training will take longer
                  learn_rate_decays=0.9,
                  # a factor the initial learning rate will be multiplied by
                  # after each iteration of the training
                  epochs=300,  # no of iternation
                  # dropouts = 0.25, # Express the percentage of nodes that
                  # will be randomly dropped as a decimal.
                  verbose=1)

    if args.ldaDim > 0:
        clf_final = clf
        clf = Pipeline([('lda', LDA(n_components=args.ldaDim)),
                        ('clf', clf_final)])

    clf.fit(embeddings, labelsNum)

    fName = "{}/classifier.pkl".format(args.workDir)
    print("Saving classifier to '{}'".format(fName))
    with open(fName, 'w') as f:
        pickle.dump((le, clf), f)
//  [python]
def train(args):
    print("Loading embeddings.")
    fname = "{}/labels.csv".format(args.workDir)
    labels = pd.read_csv(fname, header=None).as_matrix()[:, 1]
    labels = map(itemgetter(1),
                 map(os.path.split,
                     map(os.path.dirname, labels)))  # Get the directory.
    fname = "{}/reps.csv".format(args.workDir)
    embeddings = pd.read_csv(fname, header=None).as_matrix()
    le = LabelEncoder().fit(labels)
    labelsNum = le.transform(labels)
    nClasses = len(le.classes_)
    print("Training for {} classes.".format(nClasses))

    if args.classifier == 'LinearSvm':
        clf = SVC(C=1, kernel='linear', probability=True)
    elif args.classifier == 'GridSearchSvm':
        print("""
        Warning: In our experiences, using a grid search over SVM hyper-parameters only
        gives marginally better performance than a linear SVM with C=1 and
        is not worth the extra computations of performing a grid search.
        """)
        param_grid = [
            {'C': [1, 10, 100, 1000],
             'kernel': ['linear']},
            {'C': [1, 10, 100, 1000],
             'gamma': [0.001, 0.0001],
             'kernel': ['rbf']}
        ]
        clf = GridSearchCV(SVC(C=1, probability=True), param_grid, cv=5)
    elif args.classifier == 'GMM':  # Doesn't work best
        clf = GaussianMixture(n_components=nClasses)

    # ref:
    # http://scikit-learn.org/stable/auto_examples/classification/plot_classifier_comparison.html#example-classification-plot-classifier-comparison-py
    elif args.classifier == 'RadialSvm':  # Radial Basis Function kernel
        # works better with C = 1 and gamma = 2
        clf = SVC(C=1, kernel='rbf', probability=True, gamma=2)
    elif args.classifier == 'DecisionTree':  # Doesn't work best
        clf = DecisionTreeClassifier(max_depth=20)
    elif args.classifier == 'GaussianNB':
        clf = GaussianNB()

    # ref: https://jessesw.com/Deep-Learning/
    elif args.classifier == 'DBN':
        from nolearn.dbn import DBN
        clf = DBN([embeddings.shape[1], 500, labelsNum[-1:][0] + 1],  # i/p nodes, hidden nodes, o/p nodes
                  learn_rates=0.3,
                  # Smaller steps mean a possibly more accurate result, but the
                  # training will take longer
                  learn_rate_decays=0.9,
                  # a factor the initial learning rate will be multiplied by
                  # after each iteration of the training
                  epochs=300,  # no of iternation
                  # dropouts = 0.25, # Express the percentage of nodes that
                  # will be randomly dropped as a decimal.
                  verbose=1)

    if args.ldaDim > 0:
        clf_final = clf
        clf = Pipeline([('lda', LDA(n_components=args.ldaDim)),
                        ('clf', clf_final)])

    clf.fit(embeddings, labelsNum)

    fName = "{}/classifier.pkl".format(args.workDir)
    print("Saving classifier to '{}'".format(fName))
    with open(fName, 'w') as f:
        pickle.dump((le, clf), f)
//  [python]
def cluster_gmm(points, nclusters):
  preds = sklearn.mixture.GMM(n_components=nclusters).fit_predict(points)
  return preds
//  [python]
def cluster_gmm(points, nclusters):
  preds = sklearn.mixture.GaussianMixture(n_components=nclusters).fit_predict(points)
  return preds
//  [python]
def main():
    # Short demo.
    K = 64
    N = 1000

    xx, _ = make_classification(n_samples=N)
    xx_tr, xx_te = xx[: -100], xx[-100: ]

    gmm = GMM(n_components=K, covariance_type='diag')
    gmm.fit(xx_tr)

    fv = fisher_vector(xx_te, gmm)
    pdb.set_trace()
//  [python]
def main():
    # Short demo.
    K = 64
    N = 1000

    xx, _ = make_classification(n_samples=N)
    xx_tr, xx_te = xx[: -100], xx[-100: ]

    gmm = GaussianMixture(n_components=K, covariance_type='diag')
    gmm.fit(xx_tr)

    fv = fisher_vector(xx_te, gmm)
    pdb.set_trace()
//  [python]
def run_simulated():
  points, labels = generate_simulated_points()
  preds = sklearn.cluster.KMeans(n_clusters=3).fit_predict(points)
  preds = sklearn.mixture.GMM(n_components=3).fit_predict(points)
  plot_simulated(points, preds, labels)
//  [python]
def run_simulated():
  points, labels = generate_simulated_points()
  preds = sklearn.cluster.KMeans(n_clusters=3).fit_predict(points)
  preds = sklearn.mixture.GaussianMixture(n_components=3).fit_predict(points)
  plot_simulated(points, preds, labels)
//  [python]
def test_predict_untrained():
    alg = UnsupervisedAlgorithm(GMM())
    alg.predict([0])
//  [python]
def test_predict_untrained():
    alg = UnsupervisedAlgorithm(GaussianMixture())
    alg.predict([0])
//  [python]
def get_gmm(where):
    nr_mixture = 256
    nr_iteration = 1000
    if where == 'pygmm':
        return pygmm.GMM(nr_mixture = nr_mixture,
                min_covar = 1e-3,
                nr_iteration = nr_iteration,
                concurrency = 4)
    elif where == 'sklearn':
        return SKGMM(nr_mixture, n_iter = nr_iteration)
    return None
//  [python]
def get_gmm(where):
    nr_mixture = 256
    nr_iteration = 1000
    if where == 'pygmm':
        return pygmm.GMM(nr_mixture = nr_mixture,
                min_covar = 1e-3,
                nr_iteration = nr_iteration,
                concurrency = 4)
    elif where == 'sklearn':
        return GaussianMixture(n_components=nr_mixture, n_init=nr_iteration)
    return None
//  [python]
def compute_super_codebook(self, feature_size):
        '''
        Merges the GMMs that were computed for each class
        into one GMM (super codebook).
        @param feature_size: dimensionality of a feature vector 
        '''

        # Get number of classes
        ncl = len(self.labels)
        # Compute vocabsize per class
        vocab_size_per_cl = max(1, self.vocab_size / ncl)
        # Create GMM for overall repr
        print >> sys.stderr, 'Using GMM with', self.vocab_size, 'and', vocab_size_per_cl , 'per class.'
        self.gmm = GMM(self.vocab_size, n_iter=1, params='w', covariance_type='diag')
        # Init by fitting with ones
        self.gmm.fit(np.ones((self.vocab_size, feature_size)))

        # Overwrite values from supervised codebook GMM by class GMMs
        index = 0
        for _, sgmm in self.gmms.iteritems():
            vocab_size_per_cl = len(sgmm.means_)
            self.gmm.means_  [index:index + vocab_size_per_cl, :] = sgmm.means_
            self.gmm.covars_ [index:index + vocab_size_per_cl, :] = sgmm.covars_
            index += vocab_size_per_cl

        # Set uniform GMM weights
        self.gmm.weights_ = np.ones(self.vocab_size) / float(self.vocab_size)
        return
//  [python]
def compute_super_codebook(self, feature_size):
        '''
        Merges the GMMs that were computed for each class
        into one GMM (super codebook).
        @param feature_size: dimensionality of a feature vector 
        '''

        # Get number of classes
        ncl = len(self.labels)
        # Compute vocabsize per class
        vocab_size_per_cl = max(1, self.vocab_size / ncl)
        # Create GMM for overall repr
        print >> sys.stderr, 'Using GMM with', self.vocab_size, 'and', vocab_size_per_cl , 'per class.'
        self.gmm = GaussianMixture(n_components=self.vocab_size, n_iter=1, params='w', covariance_type='diag')
        # Init by fitting with ones
        self.gmm.fit(np.ones((self.vocab_size, feature_size)))

        # Overwrite values from supervised codebook GMM by class GMMs
        index = 0
        for _, sgmm in self.gmms.iteritems():
            vocab_size_per_cl = len(sgmm.means_)
            self.gmm.means_  [index:index + vocab_size_per_cl, :] = sgmm.means_
            self.gmm.covars_ [index:index + vocab_size_per_cl, :] = sgmm.covars_
            index += vocab_size_per_cl

        # Set uniform GMM weights
        self.gmm.weights_ = np.ones(self.vocab_size) / float(self.vocab_size)
        return
//  [python]
gmm = GMM()
kde = KernelDensity()
count = CountGenerator()
mi_tree_gmm = MIDependencyTree(GMM())
//  [python]
gmm = GaussianMixture()
kde = KernelDensity()
count = CountGenerator()
mi_tree_gmm = MIDependencyTree(GaussianMixture())
//  [python]
def train(args):
    start = time.time()
    for clfChoice in clfChoices:
        print("Loading embeddings.")
        fname = "{}/labels.csv".format(args.workDir)
        labels = pd.read_csv(fname, header=None).as_matrix()[:, 1]
        labels = map(itemgetter(1),
                     map(os.path.split,
                         map(os.path.dirname, labels)))  # Get the directory.
        fname = "{}/reps.csv".format(args.workDir)
        embeddings = pd.read_csv(fname, header=None).as_matrix()
        le = LabelEncoder().fit(labels)
        labelsNum = le.transform(labels)
        nClasses = len(le.classes_)
        print("Training for {} classes.".format(nClasses))

        if clfChoice == 'LinearSvm':
            clf = SVC(C=1, kernel='linear', probability=True)
        elif clfChoice == 'GMM':  # Doesn't work best
            clf = GMM(n_components=nClasses)

        # ref:
        # http://scikit-learn.org/stable/auto_examples/classification/plot_classifier_comparison.html#example-classification-plot-classifier-comparison-py
        elif clfChoice == 'RadialSvm':  # Radial Basis Function kernel
            # works better with C = 1 and gamma = 2
            clf = SVC(C=1, kernel='rbf', probability=True, gamma=2)
        elif clfChoice == 'DecisionTree':  # Doesn't work best
            clf = DecisionTreeClassifier(max_depth=20)
        elif clfChoice == 'GaussianNB':
            clf = GaussianNB()

        # ref: https://jessesw.com/Deep-Learning/
        elif clfChoice == 'DBN':
            if args.verbose:
                verbose = 1
            else:
                verbose = 0
            clf = DBN([embeddings.shape[1], 500, labelsNum[-1:][0] + 1],  # i/p nodes, hidden nodes, o/p nodes
                      learn_rates=0.3,
                      # Smaller steps mean a possibly more accurate result, but the
                      # training will take longer
                      learn_rate_decays=0.9,
                      # a factor the initial learning rate will be multiplied by
                      # after each iteration of the training
                      epochs=300,  # no of iternation
                      # dropouts = 0.25, # Express the percentage of nodes that
                      # will be randomly dropped as a decimal.
                      verbose=verbose)

        if args.ldaDim > 0:
            clf_final = clf
            clf = Pipeline([('lda', LDA(n_components=args.ldaDim)),
                            ('clf', clf_final)])

        clf.fit(embeddings, labelsNum)

        fName = os.path.join(args.workDir, clfChoice + ".pkl")
        print("Saving classifier to '{}'".format(fName))
        with open(fName, 'w') as f:
            pickle.dump((le, clf), f)
    if args.verbose:
        print(
            "Training and saving the classifiers took {} seconds.".format(
                time.time() - start))
//  [python]
def train(args):
    start = time.time()
    for clfChoice in clfChoices:
        print("Loading embeddings.")
        fname = "{}/labels.csv".format(args.workDir)
        labels = pd.read_csv(fname, header=None).as_matrix()[:, 1]
        labels = map(itemgetter(1),
                     map(os.path.split,
                         map(os.path.dirname, labels)))  # Get the directory.
        fname = "{}/reps.csv".format(args.workDir)
        embeddings = pd.read_csv(fname, header=None).as_matrix()
        le = LabelEncoder().fit(labels)
        labelsNum = le.transform(labels)
        nClasses = len(le.classes_)
        print("Training for {} classes.".format(nClasses))

        if clfChoice == 'LinearSvm':
            clf = SVC(C=1, kernel='linear', probability=True)
        elif clfChoice == 'GMM':  # Doesn't work best
            clf = GaussianMixture(n_components=nClasses)

        # ref:
        # http://scikit-learn.org/stable/auto_examples/classification/plot_classifier_comparison.html#example-classification-plot-classifier-comparison-py
        elif clfChoice == 'RadialSvm':  # Radial Basis Function kernel
            # works better with C = 1 and gamma = 2
            clf = SVC(C=1, kernel='rbf', probability=True, gamma=2)
        elif clfChoice == 'DecisionTree':  # Doesn't work best
            clf = DecisionTreeClassifier(max_depth=20)
        elif clfChoice == 'GaussianNB':
            clf = GaussianNB()

        # ref: https://jessesw.com/Deep-Learning/
        elif clfChoice == 'DBN':
            if args.verbose:
                verbose = 1
            else:
                verbose = 0
            clf = DBN([embeddings.shape[1], 500, labelsNum[-1:][0] + 1],  # i/p nodes, hidden nodes, o/p nodes
                      learn_rates=0.3,
                      # Smaller steps mean a possibly more accurate result, but the
                      # training will take longer
                      learn_rate_decays=0.9,
                      # a factor the initial learning rate will be multiplied by
                      # after each iteration of the training
                      epochs=300,  # no of iternation
                      # dropouts = 0.25, # Express the percentage of nodes that
                      # will be randomly dropped as a decimal.
                      verbose=verbose)

        if args.ldaDim > 0:
            clf_final = clf
            clf = Pipeline([('lda', LDA(n_components=args.ldaDim)),
                            ('clf', clf_final)])

        clf.fit(embeddings, labelsNum)

        fName = os.path.join(args.workDir, clfChoice + ".pkl")
        print("Saving classifier to '{}'".format(fName))
        with open(fName, 'w') as f:
            pickle.dump((le, clf), f)
    if args.verbose:
        print(
            "Training and saving the classifiers took {} seconds.".format(
                time.time() - start))
//  [python]
def compute_gmms(
        df, places, max_comp=12,
        x_cutoff=None, y_cutoff=None, limit=None, min_covar=1e-5):
    start_time = time.time()
    res = np.zeros((len(places[:limit]), 3 + 5 * max_comp), dtype=object)
    for i, dfp in enumerate(place_iterator(df, places, limit)):
        if i % 100 == 0:
            print('%d: %.2fs' % (i, time.time() - start_time))
        xs, ys = remove_outliers(dfp.x, dfp.y, x_cutoff, y_cutoff)
        data = np.concatenate(
            (xs.reshape((-1, 1)), ys.reshape((-1, 1))), axis=1)
        num_points = len(data)
        best_aic, best_gmm = float('inf'), None
        if num_points >= 2:  # GMM needs at least 2 points
            max_n_comp = min(num_points, max_comp)
            for n_comp in range(1, 1 + max_n_comp):
                gmm = GMM(
                    n_components=n_comp,
                    covariance_type='diag',
                    min_covar=min_covar,
                    random_state=random_state)
                gmm.fit(data)
                aic = gmm.aic(data)
                if aic < best_aic:
                    best_aic, best_gmm = aic, gmm
        res[i, 0] = dfp.place_id[0]
        res[i, 1] = num_points
        if best_gmm is not None:
            res[i, 2] = best_gmm.n_components
            for j in range(best_gmm.n_components):
                res[i, 3 + 5 * j] = best_gmm.weights_[j]
                res[i, 4 + 5 * j] = best_gmm.means_[j, 0]
                res[i, 5 + 5 * j] = best_gmm.means_[j, 1]
                res[i, 6 + 5 * j] = best_gmm.covars_[j, 0]
                res[i, 7 + 5 * j] = best_gmm.covars_[j, 1]
    col_names = ['place_id', 'n_pts', 'n_components']
    for j in range(max_comp):
        col_names += [
            'weight_%d' % j,
            'mean_x_%d' % j,
            'mean_y_%d' % j,
            'var_x_%d' % j,
            'var_y_%d' % j,
        ]
    return pd.DataFrame(res, columns=col_names)
//  [python]
def compute_gmms(
        df, places, max_comp=12,
        x_cutoff=None, y_cutoff=None, limit=None, min_covar=1e-5):
    start_time = time.time()
    res = np.zeros((len(places[:limit]), 3 + 5 * max_comp), dtype=object)
    for i, dfp in enumerate(place_iterator(df, places, limit)):
        if i % 100 == 0:
            print('%d: %.2fs' % (i, time.time() - start_time))
        xs, ys = remove_outliers(dfp.x, dfp.y, x_cutoff, y_cutoff)
        data = np.concatenate(
            (xs.reshape((-1, 1)), ys.reshape((-1, 1))), axis=1)
        num_points = len(data)
        best_aic, best_gmm = float('inf'), None
        if num_points >= 2:  # GMM needs at least 2 points
            max_n_comp = min(num_points, max_comp)
            for n_comp in range(1, 1 + max_n_comp):
                gmm = GaussianMixture(
                    n_components=n_comp,
                    covariance_type='diag',
                    min_covar=min_covar,
                    random_state=random_state)
                gmm.fit(data)
                aic = gmm.aic(data)
                if aic < best_aic:
                    best_aic, best_gmm = aic, gmm
        res[i, 0] = dfp.place_id[0]
        res[i, 1] = num_points
        if best_gmm is not None:
            res[i, 2] = best_gmm.n_components
            for j in range(best_gmm.n_components):
                res[i, 3 + 5 * j] = best_gmm.weights_[j]
                res[i, 4 + 5 * j] = best_gmm.means_[j, 0]
                res[i, 5 + 5 * j] = best_gmm.means_[j, 1]
                res[i, 6 + 5 * j] = best_gmm.covariances_[j, 0, 0]
                res[i, 7 + 5 * j] = best_gmm.covariances_[j, 1, 1]
    col_names = ['place_id', 'n_pts', 'n_components']
    for j in range(max_comp):
        col_names += [
            'weight_%d' % j,
            'mean_x_%d' % j,
            'mean_y_%d' % j,
            'var_x_%d' % j,
            'var_y_%d' % j,
        ]
    return pd.DataFrame(res, columns=col_names)
//  [python]
def train(self, datadict, labels, rand_features=True):
        '''
        Trains a scipy GMM for each class, joins them into a super codebook.
        @param datadict: Dictionary of class labels. 
        Inside each label there is a list of feature matrices for each window [frames x feature]
        @param labels: the labels of the datadict in a given order
        @param rand_features: Shuffles the samples before running the GMM
        '''
        self.criterion = []
        # Stack the features
        allfeatures = np.vstack(list([np.vstack(x) for x in datadict.values()]))

        # Determine the normalisation statistics and remember them
        self.norm = FeatureNormalizer()
        self.norm.setup(allfeatures)


        # Get number of classes
        ncl = len(labels)
        # Compute vocabsize per class
        vocab_size_per_cl = max(1, self.vocab_size / ncl)
        # Update vocabsize to account for rounding errors
        self.vocab_size = vocab_size_per_cl * ncl

        #
        # Train GMMs for each class
        #
        self.gmms = {}
        self.labels = labels
        for label in labels:
            # Compute feature representations
            feats = np.vstack(datadict[label])
            if rand_features:
                np.random.shuffle(feats)
            if self.normalize:
                norm_features = self.norm.normalize(feats)
            else:
                norm_features = (feats)
            print >> sys.stderr, ("Training a GMM for label %s, using scipy and data of shape %s"
                                 % (label, str(np.shape(norm_features))))
            # Train the gmm
            sub_gmm = GMM(vocab_size_per_cl, covariance_type='diag', n_iter=100)
            sub_gmm.fit(norm_features)
            # Set GMM for class
            self.gmms[label] = sub_gmm
        #
        # Combine GMMs to super codebook
        #
        self.compute_super_codebook(allfeatures.shape[1])
        return
//  [python]
def train(self, datadict, labels, rand_features=True):
        '''
        Trains a scipy GMM for each class, joins them into a super codebook.
        @param datadict: Dictionary of class labels. 
        Inside each label there is a list of feature matrices for each window [frames x feature]
        @param labels: the labels of the datadict in a given order
        @param rand_features: Shuffles the samples before running the GMM
        '''
        self.criterion = []
        # Stack the features
        allfeatures = np.vstack(list([np.vstack(x) for x in datadict.values()]))

        # Determine the normalisation statistics and remember them
        self.norm = FeatureNormalizer()
        self.norm.setup(allfeatures)


        # Get number of classes
        ncl = len(labels)
        # Compute vocabsize per class
        vocab_size_per_cl = max(1, self.vocab_size / ncl)
        # Update vocabsize to account for rounding errors
        self.vocab_size = vocab_size_per_cl * ncl

        #
        # Train GMMs for each class
        #
        self.gmms = {}
        self.labels = labels
        for label in labels:
            # Compute feature representations
            feats = np.vstack(datadict[label])
            if rand_features:
                np.random.shuffle(feats)
            if self.normalize:
                norm_features = self.norm.normalize(feats)
            else:
                norm_features = (feats)
            print >> sys.stderr, ("Training a GMM for label %s, using scipy and data of shape %s"
                                 % (label, str(np.shape(norm_features))))
            # Train the gmm
            sub_gmm = GaussianMixture(vocab_size_per_cl, covariance_type='diag', n_init=100)
            sub_gmm.fit(norm_features)
            # Set GMM for class
            self.gmms[label] = sub_gmm
        #
        # Combine GMMs to super codebook
        #
        self.compute_super_codebook(allfeatures.shape[1])
        return
//  [python]
def test_n_parameters():
    n_samples, n_dim, n_components = 7, 5, 2
    X = rng.randn(n_samples, n_dim)
    n_params = {'spherical': 13, 'diag': 21, 'tied': 26, 'full': 41}
    for cv_type in ['full', 'tied', 'diag', 'spherical']:
        g = mixture.GMM(n_components=n_components, covariance_type=cv_type,
                        random_state=rng, min_covar=1e-7, n_iter=1)
        g.fit(X)
        assert_true(g._n_parameters() == n_params[cv_type])
//  [python]
def test_n_parameters():
    n_samples, n_dim, n_components = 7, 5, 2
    X = rng.randn(n_samples, n_dim)
    n_params = {'spherical': 13, 'diag': 21, 'tied': 26, 'full': 41}
    for cv_type in ['full', 'tied', 'diag', 'spherical']:
        g = mixture.GaussianMixture(n_components=n_components, covariance_type=cv_type,
                        random_state=rng, min_covar=1e-7, max_iter=1)
        g.fit(X)
        assert_true(g._n_parameters() == n_params[cv_type])
//  [python]
def gmm_sky(values, **extras):
    """Use a gaussian mixture model, via expectation maximization.
    of course, there's only one gaussian.  could add another for
    faint sources, bad pixels, but..."""

    gmm = sklearn.mixture.GMM()
    r = gmm.fit(values)
    return r.means_[0, 0], np.sqrt(r.covars_[0, 0])
//  [python]
def gmm_sky(values, **extras):
    """Use a gaussian mixture model, via expectation maximization.
    of course, there's only one gaussian.  could add another for
    faint sources, bad pixels, but..."""

    gmm = sklearn.mixture.GaussianMixture()
    r = gmm.fit(values)
    return r.means_[0, 0], np.sqrt(r.covars_[0, 0])
//  [python]
def generate_gmm(data, ax=None):
    ##reshape the data
    X = data.reshape(data.shape[0], 1)
    ##fit models with 1-10 components
    N = np.arange(1,11)
    models = [None for i in range(len(N))]
    for i in range(len(N)):
        models[i] = GMM(N[i]).fit(X)
    ##compute AIC
    AIC = [m.aic(X) for m in models]
    ##figure out the best-fit mixture
    M_best = models[np.argmin(AIC)]
    x = np.linspace(data.min()-1, data.max()+1, data.size)
    ##compute the pdf
    logprob, responsibilities = M_best.score_samples(x.reshape(x.size, 1))
    pdf = np.exp(logprob)
    pdf_individual = responsibilities * pdf[:, np.newaxis]
    #plot the stuff
    if ax is None:
        fig, ax = plt.subplots()
    ax.hist(X, 50, normed = True, histtype = 'stepfilled', alpha = 0.4)
    ax.plot(x, pdf, '-k')
    ax.plot(x, pdf_individual, '--k')
    ax.text(0.04, 0.96, "Best-fit Mixture",
        ha='left', va='top', transform=ax.transAxes)
    ax.set_xlabel('$x$')
    ax.set_ylabel('$p(x)$')
    return x, pdf, pdf_individual
//  [python]
def generate_gmm(data, ax=None):
    ##reshape the data
    X = data.reshape(data.shape[0], 1)
    ##fit models with 1-10 components
    N = np.arange(1,11)
    models = [None for i in range(len(N))]
    for i in range(len(N)):
        models[i] = GaussianMixture(N[i]).fit(X)
    ##compute AIC
    AIC = [m.aic(X) for m in models]
    ##figure out the best-fit mixture
    M_best = models[np.argmin(AIC)]
    x = np.linspace(data.min()-1, data.max()+1, data.size)
    ##compute the pdf
    logprob, responsibilities = M_best.score_samples(x.reshape(x.size, 1))
    pdf = np.exp(logprob)
    pdf_individual = responsibilities * pdf[:, np.newaxis]
    #plot the stuff
    if ax is None:
        fig, ax = plt.subplots()
    ax.hist(X, 50, normed = True, histtype = 'stepfilled', alpha = 0.4)
    ax.plot(x, pdf, '-k')
    ax.plot(x, pdf_individual, '--k')
    ax.text(0.04, 0.96, "Best-fit Mixture",
        ha='left', va='top', transform=ax.transAxes)
    ax.set_xlabel('$x$')
    ax.set_ylabel('$p(x)$')
    return x, pdf, pdf_individual
//  [python]
def train(self, datadict, labels):
        '''
        Trains the model using the given data
        @param datadict: dictonary of label,features 
        @param labels: the labels of the datadict in a given order
        '''
        # Stack the features
        allfeatures = np.vstack(list([np.vstack(x) for x in datadict.values()]))

        # Determine the normalisation statistics and remember them
        self.norm = FeatureNormalizer()
        self.norm.setup(allfeatures)

        # Get number of classes
        ncl = len(labels)
        # Compute vocabsize per class
        vocab_size_per_cl = max(1, self.vocab_size / ncl)
        # Update vocabsize to account for rounding errors
        self.vocab_size = vocab_size_per_cl * ncl


        #
        # Initialize complete GMM (used for supercodebook)
        # This will later on be overwritten and is
        # a workaround to pre-initialize an sklearn GMM
        #
        self.gmms = []
        print >> sys.stderr, 'Initialising GMM with', self.vocab_size, 'components,', vocab_size_per_cl, 'per class.'
        self.gmm = GMM(self.vocab_size, n_iter=2, params='w')

        # Initialize by fitting with ones
        self.gmm.fit(np.ones((self.vocab_size, allfeatures.shape[1] + 1)))

        #
        # For each class train a GMM
        #
        index = 0
        for label in labels:
            # Compute feature representations
            temp_feat_reprs = []
            for feat in datadict[label]:
                feat = self.norm.normalize(feat)
                feat = self.__augment_temporal(feat)
                temp_feat_reprs.append(feat)
            temp_feat_reprs = np.vstack(temp_feat_reprs)

            print >> sys.stderr, ("Training a GMM for label %s with %d densities, using data of shape %s"
                                 % (label, vocab_size_per_cl, str(np.shape(temp_feat_reprs))))

            # Train the GMM
            gmm = GMM(vocab_size_per_cl, covariance_type='diag')
            gmm.fit(temp_feat_reprs)

            # Overwrite values from supervised codebook GMM by class GMMs
            self.gmm.means_  [index * vocab_size_per_cl:(index + 1) * vocab_size_per_cl, :] = gmm.means_
            self.gmm.covars_ [index * vocab_size_per_cl:(index + 1) * vocab_size_per_cl, :] = gmm.covars_
            self.gmm.weights_[index * vocab_size_per_cl:(index + 1) * vocab_size_per_cl] = gmm.weights_ / float(ncl)
            # Append the GMM
            self.gmms.append(gmm)
            index += 1

        # Set uniform GMM weights
        self.gmm.weights_ = np.ones(self.vocab_size) / self.vocab_size
        # Set feature size
        self.featuresize = self.vocab_size
        return
//  [python]
def train(self, datadict, labels):
        '''
        Trains the model using the given data
        @param datadict: dictonary of label,features 
        @param labels: the labels of the datadict in a given order
        '''
        # Stack the features
        allfeatures = np.vstack(list([np.vstack(x) for x in datadict.values()]))

        # Determine the normalisation statistics and remember them
        self.norm = FeatureNormalizer()
        self.norm.setup(allfeatures)

        # Get number of classes
        ncl = len(labels)
        # Compute vocabsize per class
        vocab_size_per_cl = max(1, self.vocab_size / ncl)
        # Update vocabsize to account for rounding errors
        self.vocab_size = vocab_size_per_cl * ncl


        #
        # Initialize complete GMM (used for supercodebook)
        # This will later on be overwritten and is
        # a workaround to pre-initialize an sklearn GMM
        #
        self.gmms = []
        print >> sys.stderr, 'Initialising GMM with', self.vocab_size, 'components,', vocab_size_per_cl, 'per class.'
        self.gmm = GaussianMixture(self.vocab_size, n_init=2, max_iter=2, random_state=0)

        # Initialize by fitting with ones
        self.gmm.fit(np.ones((self.vocab_size, allfeatures.shape[1] + 1)))

        #
        # For each class train a GMM
        #
        index = 0
        for label in labels:
            # Compute feature representations
            temp_feat_reprs = []
            for feat in datadict[label]:
                feat = self.norm.normalize(feat)
                feat = self.__augment_temporal(feat)
                temp_feat_reprs.append(feat)
            temp_feat_reprs = np.vstack(temp_feat_reprs)

            print >> sys.stderr, ("Training a GMM for label %s with %d densities, using data of shape %s"
                                 % (label, vocab_size_per_cl, str(np.shape(temp_feat_reprs))))

            # Train the GMM
            gmm = GaussianMixture(vocab_size_per_cl, covariance_type='diag', random_state=0)
            gmm.fit(temp_feat_reprs)

            # Overwrite values from supervised codebook GMM by class GMMs
            self.gmm.means_  [index * vocab_size_per_cl:(index + 1) * vocab_size_per_cl, :] = gmm.means_
            self.gmm.covariances_ [index * vocab_size_per_cl:(index + 1) * vocab_size_per_cl, :] = gmm.covariances_
            self.gmm.weights_[index * vocab_size_per_cl:(index + 1) * vocab_size_per_cl] = gmm.weights_ / float(ncl)
            # Append the GMM
            self.gmms.append(gmm)
            index += 1

        # Set uniform GMM weights
        self.gmm.weights_ = np.ones(self.vocab_size) / self.vocab_size
        # Set feature size
        self.featuresize = self.vocab_size
        return
//  [python]
def estimate_gmm(data, means, win_width=5, debug=False):
    n_comps = len(means)
    covs = np.zeros(n_comps)
    for i in range(n_comps):
        inners_m = (data >= (means[i] - win_width)) * (data <= (means[i] + win_width))
        inners = data[np.nonzero(inners_m)]
        covs[i] = np.cov(inners)

    gmm = skimix.GMM(n_comps)
    gmm.means_ = np.array(means.reshape(n_comps, 1))
    gmm.covars_ = np.array(covs.reshape(n_comps, 1))

    if debug:
        colors = 'rgbcmy'
        x = np.arange(0, 256, 1)
        plt.figure()
        for i in range(n_comps):
            plt.plot(x, scista.norm(means[i], covs[i]).pdf(x), colors[np.mod(i, len(colors))])

    return gmm
//  [python]
def estimate_gmm(data, means, win_width=5, debug=False):
    n_comps = len(means)
    covs = np.zeros(n_comps)
    for i in range(n_comps):
        inners_m = (data >= (means[i] - win_width)) * (data <= (means[i] + win_width))
        inners = data[np.nonzero(inners_m)]
        covs[i] = np.cov(inners)

    gmm = mixture.GaussianMixture(n_components=n_comps)
    gmm.means_ = np.array(means.reshape(n_comps, 1))
    gmm.covars_ = np.array(covs.reshape(n_comps, 1))

    if debug:
        colors = 'rgbcmy'
        x = np.arange(0, 256, 1)
        plt.figure()
        for i in range(n_comps):
            plt.plot(x, scista.norm(means[i], covs[i]).pdf(x), colors[np.mod(i, len(colors))])

    return gmm
//  [python]
def generate_GMM(audio_file_path_list, model_path):
    """Generate a Gaussian Mixture model

    Parameters
    ----------
    audio_file_path_list : list of paths to a person's wav files
    model_path : should be named after that person
        Example: model_path = VDA.gmm
    """
    mfcc_feat_person = get_mfcc_feat_person(audio_file_path_list)
    personal_GMM = GMM(n_components=15, n_iter=200, covariance_type='diag', n_init=3)
    personal_GMM.fit(mfcc_feat_person)
    with open(model_path, 'wb') as GMM_file:
        pickle.dump(personal_GMM, GMM_file)

    return None