jiahuimbzuai/CoQuIR huggingface dataset:
//  [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 = GaussianMixture(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
//  [python]
def main():
    # Short demo.
    K = 3
    N = 600

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

    print('Data normalization.')
    scaler = StandardScaler()
    # train normalization
    xx_tr = scaler.fit_transform(xx_tr)
    xx_tr = power_normalize(xx_tr, 0.5)
    xx_tr = L2_normalize(xx_tr)
    # test normalization
    xx_te = scaler.fit_transform(xx_te)
    xx_te = power_normalize(xx_te, 0.5)
    xx_te = L2_normalize(xx_te)

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

    fv_tr = fisher_vector(xx_tr, gmm)
    fv_te = fisher_vector(xx_te, gmm)

    lin_clf = svm.LinearSVC()
    lin_clf.fit(X, Y)
//  [python]
def main():
    # Short demo.
    K = 3
    N = 600

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

    print('Data normalization.')
    scaler = StandardScaler()
    # train normalization
    xx_tr = scaler.fit_transform(xx_tr)
    xx_tr = power_normalize(xx_tr, 0.5)
    xx_tr = L2_normalize(xx_tr)
    # test normalization
    xx_te = scaler.fit_transform(xx_te)
    xx_te = power_normalize(xx_te, 0.5)
    xx_te = L2_normalize(xx_te)

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

    fv_tr = fisher_vector(xx_tr, gmm)
    fv_te = fisher_vector(xx_te, gmm)

    lin_clf = svm.LinearSVC()
    lin_clf.fit(X, Y)
//  [python]
def cluster_GMM_cluster(num_clusters, word_vectors):
	# Initalize a GMM object and use it for clustering.
	clf =  GMM(n_components=num_clusters,
                    covariance_type="tied", init_params='wc', n_iter=10)
	# Get cluster assignments.
	idx = clf.fit_predict(word_vectors)
	print("Clustering Done...", time.time()-start, "seconds")
	# Get probabilities of cluster assignments.
	idx_proba = clf.predict_proba(word_vectors)
	# Dump cluster assignments and probability of cluster assignments.
	joblib.dump(idx, 'data_files/gmm_latestclusmodel_len2alldata.pkl')
	print("Cluster Assignments Saved...")
	joblib.dump(idx_proba, 'data_files/gmm_prob_latestclusmodel_len2alldata.pkl')
	print("Probabilities of Cluster Assignments Saved...")
	return (idx, idx_proba)
//  [python]
def cluster_GMM_cluster(num_clusters, word_vectors):
	# Initalize a GMM object and use it for clustering.
	clf =  GaussianMixture(n_components=num_clusters,
                    covariance_type="tied", init_params='wc', n_iter=10)
	# Get cluster assignments.
	idx = clf.fit_predict(word_vectors)
	print("Clustering Done...", time.time()-start, "seconds")
	# Get probabilities of cluster assignments.
	idx_proba = clf.predict_proba(word_vectors)
	# Dump cluster assignments and probability of cluster assignments.
	joblib.dump(idx, 'data_files/gmm_latestclusmodel_len2alldata.pkl')
	print("Cluster Assignments Saved...")
	joblib.dump(idx_proba, 'data_files/gmm_prob_latestclusmodel_len2alldata.pkl')
	print("Probabilities of Cluster Assignments Saved...")
	return (idx, idx_proba)
//  [python]
def train(self,workDir,classifier = 'LinearSvm'):

        print("Loading embeddings.")

        fname = "{}/labels.csv".format(workDir)

        labels = pd.read_csv(fname, header=None).as_matrix()[:, 1]
        labels = map(itemgetter(1),
                    map(os.path.split,
                        map(os.path.dirname, labels)))

        fname = "{}/reps.csv".format(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 classifier == 'LinearSvm':

            clf = SVC(C=1, kernel='linear', probability=True)

        elif classifier == 'GMM':

            clf = GMM(n_components=nClasses)

        elif classifier == 'RadialSvm':

            clf = SVC(C=1, kernel='rbf', probability=True, gamma=2)

        elif classifier == 'DecisionTree':

            clf = DecisionTreeClassifier(max_depth=20)

        elif classifier == 'GaussianNB':

            clf = GaussianNB()

        elif classifier == 'DBN':

            from nolearn.dbn import DBN

            clf = DBN([embeddings.shape[1], 500, labelsNum[-1:][0] + 1],
                    learn_rates=0.3,
                    learn_rate_decays=0.9,
                    epochs=300,
                    verbose=1)

        if self.ldaDim > 0:

            clf_final = clf
            clf = Pipeline([('lda', LDA(n_components=self.ldaDim)),
                            ('clf', clf_final)])

        clf.fit(embeddings, labelsNum)

        fName = "{}/classifier.pkl".format(workDir)

        print("Saving classifier to '{}'".format(fName))

        with open(fName, 'w') as f:

            pickle.dump((le, clf), f)
//  [python]
def train(self,workDir,classifier = 'LinearSvm'):

        print("Loading embeddings.")

        fname = "{}/labels.csv".format(workDir)

        labels = pd.read_csv(fname, header=None).as_matrix()[:, 1]
        labels = map(itemgetter(1),
                    map(os.path.split,
                        map(os.path.dirname, labels)))

        fname = "{}/reps.csv".format(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 classifier == 'LinearSvm':

            clf = SVC(C=1, kernel='linear', probability=True)

        elif classifier == 'GMM':

            clf = GaussianMixture(n_components=nClasses)

        elif classifier == 'RadialSvm':

            clf = SVC(C=1, kernel='rbf', probability=True, gamma=2)

        elif classifier == 'DecisionTree':

            clf = DecisionTreeClassifier(max_depth=20)

        elif classifier == 'GaussianNB':

            clf = GaussianNB()

        elif classifier == 'DBN':

            from nolearn.dbn import DBN

            clf = DBN([embeddings.shape[1], 500, labelsNum[-1:][0] + 1],
                    learn_rates=0.3,
                    learn_rate_decays=0.9,
                    epochs=300,
                    verbose=1)

        if self.ldaDim > 0:

            clf_final = clf
            clf = Pipeline([('lda', LDA(n_components=self.ldaDim)),
                            ('clf', clf_final)])

        clf.fit(embeddings, labelsNum)

        fName = "{}/classifier.pkl".format(workDir)

        print("Saving classifier to '{}'".format(fName))

        with open(fName, 'w') as f:

            pickle.dump((le, clf), f)
//  [python]
def _minimize_mimic(
        fun,
        x0,  # only used to get number of features
        args=(),
        callback=None,
        sampler=None,
        initial_distribution=None,
        domain="gaussian",
        n_iter=10,
        starting_percentile=0.5,
        batch_size=100,  # number of trials for each percentile
        maxiter=None):
    if maxiter is not None:
        batch_size = int(maxiter / n_iter)

    if sampler is None or sampler == "gmm":
        sampler = GMM()
    elif sampler == "kde":
        sampler = KernelDensity()
    elif sampler == "dependencytree":
        sampler = MIDependencyTree(GMM())

    if initial_distribution is None:
        init_size = (batch_size, len(x0))
        if domain is None:
            # assume that the sampler is already fit to sample from
            initial_distribution = sampler.sample(batch_size)
        elif domain == "gaussian":
            initial_distribution = np.random.randn(**init_size)
        elif domain == "binary":
            initial_distribution = np.random.randint(2, size=init_size)
        else:
            raise ValueError("Improper domain: %s" % domain)

    population = initial_distribution

    for percentile in np.linspace(starting_percentile, 0, n_iter):
        # fit previous population
        sampler.fit(population)
        # generate `sample_size` more
        population = sampler.sample(batch_size)
        # compute obj value
        scores = score_multi(fun, population, args, callback)
        # select `percentile` bottom (since we are minimizing)

        cutoff = np.percentile(scores, percentile * 100)
        idx = scores <= cutoff
        # repeat
        population = population[idx]
    x = population[0]
    fval = scores[idx[0]]
    nfev = batch_size * n_iter
    return to_result(x=x, fun=fval, niter=n_iter, nfev=nfev)
//  [python]
def _minimize_mimic(
        fun,
        x0,  # only used to get number of features
        args=(),
        callback=None,
        sampler=None,
        initial_distribution=None,
        domain="gaussian",
        n_iter=10,
        starting_percentile=0.5,
        batch_size=100,  # number of trials for each percentile
        maxiter=None):
    if maxiter is not None:
        batch_size = int(maxiter / n_iter)

    if sampler is None or sampler == "gmm":
        sampler = GaussianMixture()
    elif sampler == "kde":
        sampler = KernelDensity()
    elif sampler == "dependencytree":
        sampler = MIDependencyTree(GaussianMixture())

    if initial_distribution is None:
        init_size = (batch_size, len(x0))
        if domain is None:
            # assume that the sampler is already fit to sample from
            initial_distribution = sampler.sample(batch_size)
        elif domain == "gaussian":
            initial_distribution = np.random.randn(**init_size)
        elif domain == "binary":
            initial_distribution = np.random.randint(2, size=init_size)
        else:
            raise ValueError("Improper domain: %s" % domain)

    population = initial_distribution

    for percentile in np.linspace(starting_percentile, 0, n_iter):
        # fit previous population
        sampler.fit(population)
        # generate `sample_size` more
        population = sampler.sample(batch_size)
        # compute obj value
        scores = score_multi(fun, population, args, callback)
        # select `percentile` bottom (since we are minimizing)

        cutoff = np.percentile(scores, percentile * 100)
        idx = scores <= cutoff
        # repeat
        population = population[idx]
    x = population[0]
    fval = scores[idx[0]]
    nfev = batch_size * n_iter
    return to_result(x=x, fun=fval, niter=n_iter, nfev=nfev)
//  [python]
def gmm_factory(df):
    class Classifier(object):
        def fit_df(self, df):
            df = df[df.columns.difference(['event'])]
            n_components = int(round(np.sqrt(df.groupby(level=[0, 1]).size().mean())))
            self.model = GMM(n_components=n_components, covariance_type='spherical', min_covar=0.01)
            self.model.fit(df.values)

        def score_events_df(self, df):
            df = df[df.columns.difference(['event'])]
            df['score'] = self.model.score(df.values)
            return df

        def predict_states_df(self, df):
            df['state'] = 0
            return df

    clf = Classifier()
    clf.fit_df(df)
    return clf
//  [python]
def gmm_factory(df):
    class Classifier(object):
        def fit_df(self, df):
            df = df[df.columns.difference(['event'])]
            n_components = int(round(np.sqrt(df.groupby(level=[0, 1]).size().mean())))
            self.model = GaussianMixture(n_components=n_components, covariance_type='spherical', min_covar=0.01)
            self.model.fit(df.values)

        def score_events_df(self, df):
            df = df[df.columns.difference(['event'])]
            df['score'] = self.model.score_samples(df.values)
            return df

        def predict_states_df(self, df):
            df['state'] = 0
            return df

    clf = Classifier()
    clf.fit_df(df)
    return clf
//  [python]
def _pair_overlap(waves1, waves2, mean1, mean2, cov1, cov2):
    """ Calculate FP/FN estimates for two gaussian clusters
    """
    from sklearn import mixture

    means = sp.vstack([[mean1], [mean2]])
    covars = sp.vstack([[cov1], [cov2]])
    weights = sp.array([waves1.shape[1], waves2.shape[1]], dtype=float)
    weights /= weights.sum()

    # Create mixture of two Gaussians from the existing estimates
    mix = mixture.GMM(n_components=2, covariance_type='full', init_params='')
    mix.covars_ = covars
    mix.weights_ = weights
    mix.means_ = means

    posterior1 = mix.predict_proba(waves1.T)[:, 1]
    posterior2 = mix.predict_proba(waves2.T)[:, 0]

    return (posterior1.mean(), posterior2.sum() / len(posterior1),
            posterior2.mean(), posterior1.sum() / len(posterior2))
//  [python]
def _pair_overlap(waves1, waves2, mean1, mean2, cov1, cov2):
    """ Calculate FP/FN estimates for two gaussian clusters
    """
    from sklearn import mixture

    means = sp.vstack([[mean1], [mean2]])
    covars = sp.vstack([[cov1], [cov2]])
    weights = sp.array([waves1.shape[1], waves2.shape[1]], dtype=float)
    weights /= weights.sum()

    # Create mixture of two Gaussians from the existing estimates
    mix = mixture.GaussianMixture(n_components=2, covariance_type='full', init_params='')
    mix.covars_ = covars
    mix.weights_ = weights
    mix.means_ = means

    posterior1 = mix.predict_proba(waves1.T)[:, 1]
    posterior2 = mix.predict_proba(waves2.T)[:, 0]

    return (posterior1.mean(), posterior2.sum() / len(posterior1),
            posterior2.mean(), posterior1.sum() / len(posterior2))
//  [python]
def clustering_algorithm(lengths, covs, kmers, algorithm='dirichlet', K=300,
                         max_epoch=25, t=0, seed=None, mu_pkl=None):
  """Clusters using given algorithm

  Takes as argument cluster names, lengths, and coverage/kmer matrices.
  """

  # create matrix for clustering
  logging.info('Creating data matrix')
  X = create_matrix(lengths, covs, kmers)

  # project down dimension
  logging.info('Performing dimensionality reduction')
  X = reduce_dimensionality(X)

  # do the clustering
  logging.info('Starting clustering algorithm')
  if algorithm == 'sk-gmm':
    gmm = GMM(n_components=K, covariance_type='full', n_iter=500)
    gmm.fit(X)
    z = gmm.predict(X)
    return z
  elif algorithm == 'sk-dpgmm':
    gmm = DPGMM(n_components=K, covariance_type='full', n_iter=500)
    gmm.fit(X)
    z = gmm.predict(X)
    return z
  elif algorithm == 'dirichlet':
    n_data = X.shape[0]
    mu_pred_dem, Sigma_pred_dem, asgn_dem, llik = dirichlet_em(X.T, K=K, 
      n_minibatch=n_data, max_epoch=max_epoch, seed=seed)

    if mu_pkl:
      with open(mu_pkl, 'wb') as f:
        pickle.dump((mu_pred_dem, asgn_dem), f)

    # further compress stuff 
    compressed_clusters = agglomerative(mu_pred_dem.T, t=t)
    transl_dict = { i : c for i, c in enumerate(compressed_clusters)}
    asign_dem_agg = np.array([transl_dict[i] for i in asgn_dem])

    return asign_dem_agg
  elif algorithm == 'ard':
    n_data = X.shape[0]
    _, _, asgn, _ = variational_em(X.T, K=K, n_minibatch=n_data, max_epoch=max_epoch)
    return asgn
  else:
    raise ValueError("Invalid algorithm name")
//  [python]
def clustering_algorithm(lengths, covs, kmers, algorithm='dirichlet', K=300,
                         max_epoch=25, t=0, seed=None, mu_pkl=None):
  """Clusters using given algorithm

  Takes as argument cluster names, lengths, and coverage/kmer matrices.
  """

  # create matrix for clustering
  logging.info('Creating data matrix')
  X = create_matrix(lengths, covs, kmers)

  # project down dimension
  logging.info('Performing dimensionality reduction')
  X = reduce_dimensionality(X)

  # do the clustering
  logging.info('Starting clustering algorithm')
  if algorithm == 'sk-gmm':
    gmm = GaussianMixture(n_components=K, covariance_type='full', n_iter=500)
    gmm.fit(X)
    z = gmm.predict(X)
    return z
  elif algorithm == 'sk-dpgmm':
    gmm = BayesianGaussianMixture(n_components=K, covariance_type='full', n_iter=500)
    gmm.fit(X)
    z = gmm.predict(X)
    return z
  elif algorithm == 'dirichlet':
    n_data = X.shape[0]
    mu_pred_dem, Sigma_pred_dem, asgn_dem, llik = dirichlet_em(X.T, K=K, 
      n_minibatch=n_data, max_epoch=max_epoch, seed=seed)

    if mu_pkl:
      with open(mu_pkl, 'wb') as f:
        pickle.dump((mu_pred_dem, asgn_dem), f)

    # further compress stuff 
    compressed_clusters = agglomerative(mu_pred_dem.T, t=t)
    transl_dict = { i : c for i, c in enumerate(compressed_clusters)}
    asign_dem_agg = np.array([transl_dict[i] for i in asgn_dem])

    return asign_dem_agg
  elif algorithm == 'ard':
    n_data = X.shape[0]
    _, _, asgn, _ = variational_em(X.T, K=K, n_minibatch=n_data, max_epoch=max_epoch)
    return asgn
  else:
    raise ValueError("Invalid algorithm name")
//  [python]
def __init__(self, n_components=1, n_mix=1,
                 startprob_prior=1.0, transmat_prior=1.0,
                 covariance_type='diag', covars_prior=1e-2,
                 algorithm="viterbi", random_state=None,
                 n_iter=10, tol=1e-2, verbose=False,
                 params="stmcw", init_params="stmcw"):
        _BaseHMM.__init__(self, n_components,
                          startprob_prior=startprob_prior,
                          transmat_prior=transmat_prior,
                          algorithm=algorithm, random_state=random_state,
                          n_iter=n_iter, tol=tol, verbose=verbose,
                          params=params, init_params=init_params)

        if covariance_type != "diag":
            warnings.warn("Fitting a GMMHMM with {0!r} covariance type "
                          "is broken in 0.2.0. Please update to 0.2.1 once "
                          "it's available.".format(covariance_type),
                          UserWarning)

        # XXX: Hotfit for n_mix that is incompatible with the scikit's
        # BaseEstimator API
        self.n_mix = n_mix
        self.covariance_type = covariance_type
        self.covars_prior = covars_prior
        self.gmms_ = []
        for x in range(self.n_components):
            if covariance_type is None:
                gmm = GMM(n_mix, random_state=self.random_state)
            else:
                gmm = GMM(n_mix, covariance_type=covariance_type,
                        random_state=self.random_state)
            self.gmms_.append(gmm)
//  [python]
def __init__(self, n_components=1, n_mix=1,
                 startprob_prior=1.0, transmat_prior=1.0,
                 covariance_type='diag', covars_prior=1e-2,
                 algorithm="viterbi", random_state=None,
                 n_iter=10, tol=1e-2, verbose=False,
                 params="stmcw", init_params="stmcw"):
        _BaseHMM.__init__(self, n_components,
                          startprob_prior=startprob_prior,
                          transmat_prior=transmat_prior,
                          algorithm=algorithm, random_state=random_state,
                          n_iter=n_iter, tol=tol, verbose=verbose,
                          params=params, init_params=init_params)

        if covariance_type != "diag":
            warnings.warn("Fitting a GMMHMM with {0!r} covariance type "
                          "is broken in 0.2.0. Please update to 0.2.1 once "
                          "it's available.".format(covariance_type),
                          UserWarning)

        # XXX: Hotfit for n_mix that is incompatible with the scikit's
        # BaseEstimator API
        self.n_mix = n_mix
        self.covariance_type = covariance_type
        self.covars_prior = covars_prior
        self.gmms_ = []
        for x in range(self.n_components):
            if covariance_type is None:
                gmm = GaussianMixture(n_mix, random_state=self.random_state)
            else:
                gmm = GaussianMixture(n_mix, covariance_type=covariance_type,
                        random_state=self.random_state)
            self.gmms_.append(gmm)
//  [python]
def train(args):
    print("Loading embeddings.")
    fname = "{}/labels.csv".format(args.workDir)
    labels = pd.read_csv(fname, header=None).as_matrix()[:, 1]
    labels = list(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, 'wb') 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 = list(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, 'wb') as f:
        pickle.dump((le, clf), f)
//  [python]
def _accumulate_sufficient_statistics(self, stats, X, framelogprob,
                                          posteriors, fwdlattice, bwdlattice):
        super(GMMHMM, self)._accumulate_sufficient_statistics(
            stats, X, framelogprob, posteriors, fwdlattice, bwdlattice)

        for state, g in enumerate(self.gmms_):
            lgmm_posteriors = (np.log(g.predict_proba(X))
                               + np.log(posteriors[:, state][:, np.newaxis]
                                        + np.finfo(np.float).eps))
            gmm_posteriors = np.exp(lgmm_posteriors)

            n_features = g.means_.shape[1]
            tmp_gmm = GMM(g.n_components, covariance_type=g.covariance_type)
            tmp_gmm._set_covars(
                distribute_covar_matrix_to_match_covariance_type(
                    np.eye(n_features), g.covariance_type,
                    g.n_components))
            norm = tmp_gmm._do_mstep(X, gmm_posteriors, self.params)

            if np.any(np.isnan(tmp_gmm.covars_)):
                raise ValueError

            stats['norm'][state] += norm
            if 'm' in self.params:
                stats['means'][state] += tmp_gmm.means_ * norm[:, np.newaxis]
            if 'c' in self.params:
                if tmp_gmm.covariance_type == 'tied':
                    stats['covars'][state] += tmp_gmm.covars_ * norm.sum()
                else:
                    cvnorm = np.copy(norm)
                    shape = np.ones(tmp_gmm.covars_.ndim, dtype=np.int)
                    shape[0] = np.shape(tmp_gmm.covars_)[0]
                    cvnorm.shape = shape
                    stats['covars'][state] += (tmp_gmm.covars_
                                               + tmp_gmm.means_**2) * cvnorm
//  [python]
def _accumulate_sufficient_statistics(self, stats, X, framelogprob,
                                          posteriors, fwdlattice, bwdlattice):
        super(GMMHMM, self)._accumulate_sufficient_statistics(
            stats, X, framelogprob, posteriors, fwdlattice, bwdlattice)

        for state, g in enumerate(self.gmms_):
            lgmm_posteriors = (np.log(g.predict_proba(X))
                               + np.log(posteriors[:, state][:, np.newaxis]
                                        + np.finfo(np.float).eps))
            gmm_posteriors = np.exp(lgmm_posteriors)

            n_features = g.means_.shape[1]
            tmp_gmm = GaussianMixture(g.n_components, covariance_type=g.covariance_type)
            tmp_gmm._set_covars(
                distribute_covar_matrix_to_match_covariance_type(
                    np.eye(n_features), g.covariance_type,
                    g.n_components))
            norm = tmp_gmm._do_mstep(X, gmm_posteriors, self.params)

            if np.any(np.isnan(tmp_gmm.covars_)):
                raise ValueError

            stats['norm'][state] += norm
            if 'm' in self.params:
                stats['means'][state] += tmp_gmm.means_ * norm[:, np.newaxis]
            if 'c' in self.params:
                if tmp_gmm.covariance_type == 'tied':
                    stats['covars'][state] += tmp_gmm.covars_ * norm.sum()
                else:
                    cvnorm = np.copy(norm)
                    shape = np.ones(tmp_gmm.covars_.ndim, dtype=np.int)
                    shape[0] = np.shape(tmp_gmm.covars_)[0]
                    cvnorm.shape = shape
                    stats['covars'][state] += (tmp_gmm.covars_
                                               + tmp_gmm.means_**2) * cvnorm
//  [python]
def fit(self,train_bags,train_labels,**kwargs):
        """
        @param train_bags : a sequence of n bags; each bag is an m-by-k array-like
                      object containing m instances with k features
        @param train_labels : an array-like object of length n containing -1/+1 labels
        
        @param k : Number of 'words'
                 
        @param covar_type  : Type of covariance matrix (default = 'diag')        
        """
        k = kwargs['k']
        covar_type = kwargs['covar_type']
        n_iter = kwargs['n_iter']
        X, Y = MIL2SIL(train_bags,train_labels)
        self._gauss_mix_model= GMM(n_components=k,covariance_type=covar_type, init_params='wc', n_iter=n_iter)
        self._gauss_mix_model.fit(X)
        out_hist = self._gauss_mix_model.predict_proba(X)
        
        #Logistic separate positive histograms from negative histograms
        self._logistic = LogisticRegression()
        self._logistic = self._logistic.fit(out_hist,Y)
//  [python]
def fit(self,train_bags,train_labels,**kwargs):
        """
        @param train_bags : a sequence of n bags; each bag is an m-by-k array-like
                      object containing m instances with k features
        @param train_labels : an array-like object of length n containing -1/+1 labels
        
        @param k : Number of 'words'
                 
        @param covar_type  : Type of covariance matrix (default = 'diag')        
        """
        k = kwargs['k']
        covar_type = kwargs['covar_type']
        n_iter = kwargs['n_iter']
        X, Y = MIL2SIL(train_bags,train_labels)
        self._gauss_mix_model= GaussianMixture(n_components=k,covariance_type=covar_type, init_params='wc', n_init=n_iter)
        self._gauss_mix_model.fit(X)
        out_hist = self._gauss_mix_model.predict_proba(X)
        
        #Logistic separate positive histograms from negative histograms
        self._logistic = LogisticRegression()
        self._logistic = self._logistic.fit(out_hist,Y)
//  [python]
def create_random_gmm(n_mix, n_features, covariance_type, prng=0):
    prng = check_random_state(prng)
    g = GMM(n_mix, covariance_type=covariance_type)
    g.means_ = prng.randint(-20, 20, (n_mix, n_features))
    g.covars_ = make_covar_matrix(covariance_type, n_mix, n_features)
    g.weights_ = normalized(prng.rand(n_mix))
    return g
//  [python]
def create_random_gmm(n_mix, n_features, covariance_type, prng=0):
    prng = check_random_state(prng)
    g = GaussianMixture(n_mix, covariance_type=covariance_type)
    g.means_ = prng.randint(-20, 20, (n_mix, n_features))
    g.covars_ = make_covar_matrix(covariance_type, n_mix, n_features)
    g.weights_ = normalized(prng.rand(n_mix))
    return g
//  [python]
def create_gmms(self, coords_occur, coords_cooccur):
        '''Assumes input is dict of numpy array? '''
        
        depths = np.unique(coords_occur['z'])
        
        gmm_abs_pos = {}
        for depth in depths:
            idxs = coords_occur['z'] == depth
            coords = np.vstack( (coords_occur['x'][idxs], 
                                 coords_occur['y'][idxs]) ).transpose()
            np.random.seed(1)
            gmm = sklearn.mixture.GMM(n_components=self.gmm_abs_k)
            gmm_abs_pos[depth] = gmm.fit(coords)
        
        coords = np.vstack( (coords_cooccur['del_x'], 
                             coords_cooccur['del_y']) ).transpose()
        np.random.seed(1)
        gmm = sklearn.mixture.GMM(n_components=self.gmm_rel_k)
        gmm_rel_pos = gmm.fit(coords)

        return gmm_abs_pos, gmm_rel_pos
//  [python]
def create_gmms(self, coords_occur, coords_cooccur):
        '''Assumes input is dict of numpy array? '''
        
        depths = np.unique(coords_occur['z'])
        
        gmm_abs_pos = {}
        for depth in depths:
            idxs = coords_occur['z'] == depth
            coords = np.vstack( (coords_occur['x'][idxs], 
                                 coords_occur['y'][idxs]) ).transpose()
            np.random.seed(1)
            gmm = sklearn.mixture.GaussianMixture(n_components=self.gmm_abs_k)
            gmm_abs_pos[depth] = gmm.fit(coords)
        
        coords = np.vstack( (coords_cooccur['del_x'], 
                             coords_cooccur['del_y']) ).transpose()
        np.random.seed(1)
        gmm = sklearn.mixture.GaussianMixture(n_components=self.gmm_rel_k)
        gmm_rel_pos = gmm.fit(coords)

        return gmm_abs_pos, gmm_rel_pos
//  [python]
BIC = np.zeros((T,1))
AIC = np.zeros((T,1))
CVE = np.zeros((T,1))

# K-fold crossvalidation
CV = cross_validation.KFold(N,10,shuffle=True)

for t,K in enumerate(KRange):
        print('Fitting model for K={0}\n'.format(K))

        # Fit Gaussian mixture model
        gmm = GMM(n_components=K, covariance_type=covar_type, n_init=reps, params='wmc').fit(X)

        # Get BIC and AIC
        BIC[t,0] = gmm.bic(X)
        AIC[t,0] = gmm.aic(X)

        # For each crossvalidation fold
        for train_index, test_index in CV:

            # extract training and test set for current CV fold
            X_train = X[train_index]
            X_test = X[test_index]

            # Fit Gaussian mixture model to X_train
            gmm = GMM(n_components=K, covariance_type=covar_type, n_init=reps, params='wmc').fit(X_train)

            # compute negative log likelihood of X_test
            CVE[t] += -gmm.score(X_test).sum()
//  [python]
BIC = np.zeros((T,1))
AIC = np.zeros((T,1))
CVE = np.zeros((T,1))

# K-fold crossvalidation
CV = cross_validation.KFold(N,10,shuffle=True)

for t,K in enumerate(KRange):
        print('Fitting model for K={0}\n'.format(K))

        # Fit Gaussian mixture model
        gmm = GaussianMixture(n_components=K, covariance_type=covar_type, n_init=reps, params='wmc').fit(X)

        # Get BIC and AIC
        BIC[t,0] = gmm.bic(X)
        AIC[t,0] = gmm.aic(X)

        # For each crossvalidation fold
        for train_index, test_index in CV:

            # extract training and test set for current CV fold
            X_train = X[train_index]
            X_test = X[test_index]

            # Fit Gaussian mixture model to X_train
            gmm = GaussianMixture(n_components=K, covariance_type=covar_type, n_init=reps, params='wmc').fit(X_train)

            # compute negative log likelihood of X_test
            CVE[t] += -gmm.score_samples(X_test).sum()
//  [python]
def nb_clust_with_bic_(coord_tc, min_nc=2, max_nc=20):
    """
    USAGE
    Compute a Gaussian mixture on the coord_tc dataset for number of cluster between min_nc and max_nc.
    Find best number of cluster according to the BIC criterion.
    Return the best number of cluster, the model according to best number of cluster and bic value of the model.

    INPUT
    coord_tc : numpy array of the coordinates
    min_nc : min number of cluster to test:
    max_nc : max_number of cluster to test

    OUTPUT
    nc : The best number of cluster according to the BIC criterion
    bic : bic value of the model
    gmm : model according to best number of cluster
    """
    max_ncf = min(max_nc, len(coord_tc))
    nc_bic = [-1, np.inf, []]
    for nc in range(min_nc, max_ncf):
        gmm = GMM(n_components=nc, covariance_type="full", min_covar=10e-8)
        gmm.fit(coord_tc)
        bic = gmm.bic(coord_tc)
        nc_bic = min([nc_bic, [nc, bic, gmm]], key=lambda x: x[1])
    nc, bic, gmm = nc_bic
    return nc, bic, gmm
//  [python]
def nb_clust_with_bic_(coord_tc, min_nc=2, max_nc=20):
    """
    USAGE
    Compute a Gaussian mixture on the coord_tc dataset for number of cluster between min_nc and max_nc.
    Find best number of cluster according to the BIC criterion.
    Return the best number of cluster, the model according to best number of cluster and bic value of the model.

    INPUT
    coord_tc : numpy array of the coordinates
    min_nc : min number of cluster to test:
    max_nc : max_number of cluster to test

    OUTPUT
    nc : The best number of cluster according to the BIC criterion
    bic : bic value of the model
    gmm : model according to best number of cluster
    """
    max_ncf = min(max_nc, len(coord_tc))
    nc_bic = [-1, np.inf, []]
    for nc in range(min_nc, max_ncf):
        gmm = GaussianMixture(n_components=nc, covariance_type="full", min_covar=10e-8)
        gmm.fit(coord_tc)
        bic = gmm.bic(coord_tc)
        nc_bic = min([nc_bic, [nc, bic, gmm]], key=lambda x: x[1])
    nc, bic, gmm = nc_bic
    return nc, bic, gmm
//  [python]
def getbic(X, ncomp=[3]):
    '''Simple function to compute the Bayesian information criterion.'''
    bic = [GMM(n_components=nc, covariance_type="full").fit(X).bic(X) for nc in ncomp]
    #for ii in range(len(ncomp)):
    #    print(ncomp[ii], bic[ii])
    return bic
//  [python]
def getbic(X, ncomp=[3]):
    '''Simple function to compute the Bayesian information criterion.'''
    bic = [GaussianMixture(n_components=nc, covariance_type="full").fit(X).aic(X) for nc in ncomp]
    #for ii in range(len(ncomp)):
    #    print(ncomp[ii], bic[ii])
    return bic
//  [python]
def cause(n, k=4, p1=2, p2=2):
    g = GMM(k)
    g.means_ = p1 * np.random.randn(k, 1)
    g.covars_ = np.power(abs(p2 * np.random.randn(k, 1) + 1), 2)
    g.weights_ = abs(np.random.rand(k, 1))
    g.weights_ = g.weights_ / sum(g.weights_)
    # return scale(g.sample(n)).flatten()
    return np.random.uniform(-1, 1, n)
//  [python]
def cause(n, k=4, p1=2, p2=2):
    g = GaussianMixture(k)
    g.means_ = p1 * np.random.randn(k, 1)
    g.covars_ = np.power(abs(p2 * np.random.randn(k, 1) + 1), 2)
    g.weights_ = abs(np.random.rand(k, 1))
    g.weights_ = g.weights_ / sum(g.weights_)
    # return scale(g.sample(n)).flatten()
    return np.random.uniform(-1, 1, n)
//  [python]
def generate(self, store):
        points = get_points(self._user, store)
        k_max = min(self.K_MAX, len(points) / (2 * DIMENSIONS))
        if k_max < 1:
            self._user.model = None
            self._gmm = None
            return
        candidates = list()
        for k in range(1, k_max+1):
            gmm = sklearn.mixture.GMM(n_components=k, covariance_type='full',
                    min_covar=self.MIN_COVAR)
            gmm.fit(points)
            candidates.append((gmm, gmm.bic(points)))
        self._gmm, bic = min(candidates, key=itemgetter(1))
        self._user.model = unicode(pickle.dumps(self._gmm))
        store.flush()
//  [python]
def generate(self, store):
        points = get_points(self._user, store)
        k_max = min(self.K_MAX, len(points) / (2 * DIMENSIONS))
        if k_max < 1:
            self._user.model = None
            self._gmm = None
            return
        candidates = list()
        for k in range(1, k_max+1):
            gmm = sklearn.mixture.GaussianMixture(n_components=k, covariance_type='full',
                    min_covar=self.MIN_COVAR)
            gmm.fit(points)
            candidates.append((gmm, gmm.bic(points)))
        self._gmm, bic = min(candidates, key=itemgetter(1))
        self._user.model = unicode(pickle.dumps(self._gmm))
        store.flush()
//  [python]
def get_gmm(where):
    nr_mixture = 256
    nr_iteration = 1
    if where == 'pygmm':
        return pygmm.GMM(nr_mixture = nr_mixture,
                min_covar = 1e-3,
                nr_iteration = nr_iteration,
                init_with_kmeans = 0,
                concurrency = 1)
    elif where == 'sklearn':
        return SKGMM(nr_mixture, n_iter = nr_iteration)
    return None
//  [python]
def get_gmm(where):
    nr_mixture = 256
    nr_iteration = 1
    if where == 'pygmm':
        return pygmm.GMM(nr_mixture = nr_mixture,
                min_covar = 1e-3,
                nr_iteration = nr_iteration,
                init_with_kmeans = 0,
                concurrency = 1)
    elif where == 'sklearn':
        return GaussianMixture(n_components=nr_mixture, n_init=nr_iteration)
    return None
//  [python]
def GMM(self,K,meanSampStd,varSampStd,size):
		## setting up gaussian mixture model for sampling 

		"""
			meanSampleStd : std for sampling in mean 
			varSampStd    : std for sampling in std of GMM

			weights is assumed to be sampled from normalizedGaussian (0,1)

		"""

		"""
		NOTE : 
		For random samples from N(\\mu, \\sigma^2), use:
		sigma * np.random.randn(...) + mu
		https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.randn.html
		"""

		sampler  = GMM(K)
		sampler.means_   = meanSampStd*np.random.randn(K,1)# Gaussian(0,ri)
		sampler.covars_ = np.power(abs(varSampStd*np.random.randn(K,1)),2) # standard deviation from Gaussian(0,si)
		sampler.weights_ = abs(np.random.randn(K,1))
		sampler.weights_ = sampler.weights_/sum(sampler.weights_)
		return self.normalize(sampler.sample(size))
//  [python]
def GMM(self,K,meanSampStd,varSampStd,size):
		## setting up gaussian mixture model for sampling 

		"""
			meanSampleStd : std for sampling in mean 
			varSampStd    : std for sampling in std of GMM

			weights is assumed to be sampled from normalizedGaussian (0,1)

		"""

		"""
		NOTE : 
		For random samples from N(\\mu, \\sigma^2), use:
		sigma * np.random.randn(...) + mu
		https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.randn.html
		"""

		sampler  = GaussianMixture(K)
		sampler.means_   = meanSampStd*np.random.randn(K,1)# Gaussian(0,ri)
		sampler.covars_ = np.power(abs(varSampStd*np.random.randn(K,1)),2) # standard deviation from Gaussian(0,si)
		sampler.weights_ = abs(np.random.randn(K,1))
		sampler.weights_ = sampler.weights_/sum(sampler.weights_)
		return self.normalize(sampler.sample(size))
//  [python]
def gmm_scores(X, n_components = 2):
    """ Fits a Gaussian Mixture Model to the data and detects anomalies based on that model.
    
    The component with the highest weight will be considered the model for the nominal part of
    the time-series. If the a-priori score for a point under any other component is higher, it
    will be considered anomalous.
    """
    
    gmm = GMM(n_components, 'full', n_init = 10)
    gmm.fit(X.T)
    nominal_component = gmm.weights_.argmax()
    if n_components == 2:
        scores = gmm.score_samples(X.T)[1][:, 1 - nominal_component]
    else:
        scores = (1.0 - gmm.score_samples(X.T)[1][:, nominal_component])
    if np.ma.isMaskedArray(X):
        scores = np.ma.MaskedArray(scores, X.mask[0,:] if X.ndim > 1 else X.mask)
    return scores
//  [python]
def gmm_scores(X, n_components = 2):
    """ Fits a Gaussian Mixture Model to the data and detects anomalies based on that model.
    
    The component with the highest weight will be considered the model for the nominal part of
    the time-series. If the a-priori score for a point under any other component is higher, it
    will be considered anomalous.
    """
    
    gmm = GaussianMixture(n_components, covariance_type='full', n_init = 10)
    gmm.fit(X.T)
    nominal_component = gmm.weights_.argmax()
    if n_components == 2:
        scores = gmm.score_samples(X.T)[:, 1 - nominal_component]
    else:
        scores = (1.0 - gmm.score_samples(X.T)[:, nominal_component])
    if np.ma.isMaskedArray(X):
        scores = np.ma.MaskedArray(scores, X.mask[0,:] if X.ndim > 1 else X.mask)
    return scores
//  [python]
def get_learned_gmm(points, n_gaussians, covariance_type='diag'):
    """
    Learn weights, means and covariances for a gmm based on input data using sklearn EM algorithm

    :param points: num_points_per_model*nummodels X 3 - xyz coordinates
    :param n_gaussians: scalar of number of gaussians /  3 element list of number of subdivisions for grid type
    :param covariance_type: Specify the type of covariance mmatrix : 'diag', 'full','tied', 'spherical' (Note that the Fisher Vector method relies on diagonal covariance matrix)
        See sklearn documentation : http://scikit-learn.org/stable/modules/generated/sklearn.mixture.GaussianMixture.html
    :return gmm: gmm: instance of sklearn GaussianMixture (GMM) object Gauassian mixture model
    """
    gmm = mixture.GaussianMixture(n_components = n_gaussians, covariance_type=covariance_type)
    gmm.fit(points.astype(np.float64))
    return gmm
//  [python]
def get_learned_gmm(points, n_gaussians, covariance_type='diag'):
    """
    Learn weights, means and covariances for a gmm based on input data using sklearn EM algorithm

    :param points: num_points_per_model*nummodels X 3 - xyz coordinates
    :param n_gaussians: scalar of number of gaussians /  3 element list of number of subdivisions for grid type
    :param covariance_type: Specify the type of covariance mmatrix : 'diag', 'full','tied', 'spherical' (Note that the Fisher Vector method relies on diagonal covariance matrix)
        See sklearn documentation : http://scikit-learn.org/stable/modules/generated/sklearn.mixture.GaussianMixture.html
    :return gmm: gmm: instance of sklearn GaussianMixture (GMM) object Gauassian mixture model
    """
    gmm = GaussianMixture(n_components = n_gaussians, covariance_type=covariance_type)
    gmm.fit(points.astype(np.float64))
    return gmm
//  [python]
def add_user(name,path):

    #Voice authentication
    FORMAT = pyaudio.paInt16
    CHANNELS = 1
    RATE = 44100
    CHUNK = 1024
    RECORD_SECONDS = 5

    path = "./"+ path
    #source = "./voice_database"


    #os.mkdir(source)



    dest =  "./gmm_models/"
    #count = 1
    #max_count = len(os.listdir(source))

    #for path in os.listdir(source):
        #path = os.path.join(source, path)


    features = np.array([])

    # reading audio files of speaker
    (sr, audio) = read(path)

    # extract 40 dimensional MFCC & delta MFCC features
    vector   = extract_features(audio,sr)

    if features.size == 0:
        features = vector
    else:
        features = np.vstack((features, vector))

    # when features of 3 files of speaker are concatenated, then do model training
    #if count == max_count:
    gmm = GMM(n_components = 16, n_iter = 200, covariance_type='diag',n_init = 3)
    gmm.fit(features)


    # saving the trained gaussian model
    pickle.dump(gmm, open(dest + name + '.gmm', 'wb'))
    #json.dump(gmm, a)
    print(name + ' added successfully')

    features = np.asarray(())
    return gmm,dest + name + '.gmm'