Python sklearn.discriminant_analysis.LinearDiscriminantAnalysis() Examples

The following are 30 code examples of sklearn.discriminant_analysis.LinearDiscriminantAnalysis(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sklearn.discriminant_analysis , or try the search function .
Example #1
Source File: testScoreWithAdapaSklearn.py    From nyoka with Apache License 2.0 6 votes vote down vote up
def test_12_lda(self):
        print("\ntest 12 (LDA with preprocessing) [binary-class]\n")
        X, X_test, y, features, target, test_file = self.data_utility.get_data_for_binary_classification()

        model = LinearDiscriminantAnalysis()
        pipeline_obj = Pipeline([
            ("scaler", StandardScaler()),
            ("model", model)
        ])
        pipeline_obj.fit(X,y)
        file_name = 'test12sklearn.pmml'
        
        skl_to_pmml(pipeline_obj, features, target, file_name)
        model_name  = self.adapa_utility.upload_to_zserver(file_name)
        predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, test_file)
        model_pred = pipeline_obj.predict(X_test)
        model_prob = pipeline_obj.predict_proba(X_test)
        self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True)
        self.assertEqual(self.adapa_utility.compare_probability(probabilities, model_prob), True) 
Example #2
Source File: test_sklearn_glm_classifier_converter.py    From sklearn-onnx with MIT License 6 votes vote down vote up
def test_model_logistic_linear_discriminant_analysis_decfunc(self):
        X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
        y = np.array([1, 1, 1, 2, 2, 2])
        X_test = np.array([[-0.8, -1], [0, 1]], dtype=np.float32)
        model = LinearDiscriminantAnalysis().fit(X, y)
        model_onnx = convert_sklearn(
            model, "linear model",
            [("input", FloatTensorType([None, X_test.shape[1]]))],
            options={id(model): {'raw_scores': True}})
        self.assertIsNotNone(model_onnx)
        dump_data_and_model(
            X_test, model, model_onnx,
            basename="SklearnLinearDiscriminantAnalysisBinRawScore-Out0",
            # Operator cast-1 is not implemented in onnxruntime
            allow_failure="StrictVersion(onnx.__version__)"
                          " < StrictVersion('1.3') or "
                          "StrictVersion(onnxruntime.__version__)"
                          " <= StrictVersion('0.2.1')",
            methods=['predict', 'decision_function']
        ) 
Example #3
Source File: test_sklearn_glm_classifier_converter.py    From sklearn-onnx with MIT License 6 votes vote down vote up
def test_model_logistic_linear_discriminant_analysis(self):
        X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
        y = np.array([1, 1, 1, 2, 2, 2])
        X_test = np.array([[-0.8, -1], [-2, -1]], dtype=np.float32)
        model = LinearDiscriminantAnalysis().fit(X, y)
        model_onnx = convert_sklearn(
            model, "linear model",
            [("input", FloatTensorType([None, X_test.shape[1]]))])
        self.assertIsNotNone(model_onnx)
        dump_data_and_model(
            X_test,
            model,
            model_onnx,
            basename="SklearnLinearDiscriminantAnalysisBin-Dec3",
            # Operator cast-1 is not implemented in onnxruntime
            allow_failure="StrictVersion(onnx.__version__)"
                          " < StrictVersion('1.3') or "
                          "StrictVersion(onnxruntime.__version__)"
                          " <= StrictVersion('0.2.1')",
        ) 
Example #4
Source File: test_sklearn_glm_classifier_converter.py    From sklearn-onnx with MIT License 6 votes vote down vote up
def test_model_logistic_linear_discriminant_analysis_decfunc3(self):
        X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
        y = np.array([1, 1, 1, 2, 2, 3])
        X_test = np.array([[-0.8, -1], [0, 1]], dtype=np.float32)
        model = LinearDiscriminantAnalysis().fit(X, y)
        model_onnx = convert_sklearn(
            model, "linear model",
            [("input", FloatTensorType([None, X_test.shape[1]]))],
            options={id(model): {'raw_scores': True}})
        self.assertIsNotNone(model_onnx)
        dump_data_and_model(
            X_test, model, model_onnx,
            basename="SklearnLinearDiscriminantAnalysisBinRawScore3-Out0",
            # Operator cast-1 is not implemented in onnxruntime
            allow_failure="StrictVersion(onnx.__version__)"
                          " < StrictVersion('1.3') or "
                          "StrictVersion(onnxruntime.__version__)"
                          " <= StrictVersion('0.2.1')",
            methods=['predict', 'decision_function']
        ) 
Example #5
Source File: create.py    From pyrsa with GNU Lesser General Public License v3.0 6 votes vote down vote up
def rdm_lda_kfold(x, labels):
    from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
    from sklearn.model_selection import RepeatedStratifiedKFold
    from sklearn.model_selection import cross_val_score
    lda = LinearDiscriminantAnalysis(solver='lsqr', shrinkage='auto')
    folding = RepeatedStratifiedKFold(n_splits=3, n_repeats=3)

    objects = numpy.unique(labels)
    pairs = list(itertools.combinations(objects, 2))
    npairs = len(pairs)
    utv = numpy.full([npairs,], numpy.nan)
    for p in trange(npairs, desc='pairs', leave=False, ascii=True):
        pair = pairs[p]
        pair_mask = numpy.isin(labels, pair)
        x_pair = x[pair_mask, :]
        labels_pair = labels[pair_mask]
        scores = cross_val_score(lda, x_pair, labels_pair, cv=folding)
        utv[p] = scores.mean()
    return utv 
Example #6
Source File: common.py    From gumpy with MIT License 6 votes vote down vote up
def __init__(self, **kwargs):
        """Initializes a ShrinkingLDA classifier.

        Additional arguments will be forwarded to the underlying classifier
        instantiation, which is
        ``sklearn.discriminant_analysis.LinearDiscriminantAnalysis`` here.

        Keyword Arguments
        -----------------
        solver: string, default = lsqr
            Solver used in LDA
        shrinkage: string, default = 'auto'

        """
        super(ShrinkingLDA, self).__init__()
        self.solver = kwargs.pop('solver', 'lsqr')
        self.shrinkage = kwargs.pop('shrinkage', 'auto')
        self.clf = _LinearDiscriminantAnalysis(solver=self.solver, shrinkage=self.shrinkage, **kwargs) 
Example #7
Source File: feature_selection.py    From default-credit-card-prediction with MIT License 6 votes vote down vote up
def lda_selection(X,y,n_components):
	"""
	Performs the Fisher's Linear Discrimination Analysis keeps the most discriminative features

	Keyword arguments:
	X -- The feature vectors
	y -- The target vector
	n_components -- Number of features to keep
	"""

	if verbose:
		print '\nPerforming Linear Discrimination Analysis ...'

	lda = LDA(n_components = n_components,solver='eigen')
	discriminative_attributes = lda.fit(X, y).transform(X)
	return discriminative_attributes

#Random Forest Classifier with an additional attribute coef_, in order to be usable by the Recursive Feature Elimination method 
Example #8
Source File: BaggedLDA.py    From Awesome-Scripts with MIT License 6 votes vote down vote up
def main():
	# prepare data
	trainingSet=[]
	testSet=[]
	accuracy = 0.0
	split = 0.25
	loadDataset('../Dataset/LDAdata.csv', split, trainingSet, testSet)
	print('Train set: ' + repr(len(trainingSet)))
	print('Test set: ' + repr(len(testSet)))
	trainData = np.array(trainingSet)[:,0:np.array(trainingSet).shape[1] - 1]
	columns = trainData.shape[1] 
	X = np.array(trainData)
	y = np.array(trainingSet)[:,columns]
	clf = BaggingClassifier(LDA())
	clf.fit(X, y)
	testData = np.array(testSet)[:,0:np.array(trainingSet).shape[1] - 1]
	X_test = np.array(testData)
	y_test = np.array(testSet)[:,columns]
	accuracy = clf.score(X_test,y_test)
	accuracy *= 100
	print("Accuracy %:",accuracy) 
Example #9
Source File: test_discriminant_analysis.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_lda_priors():
    # Test priors (negative priors)
    priors = np.array([0.5, -0.5])
    clf = LinearDiscriminantAnalysis(priors=priors)
    msg = "priors must be non-negative"
    assert_raise_message(ValueError, msg, clf.fit, X, y)

    # Test that priors passed as a list are correctly handled (run to see if
    # failure)
    clf = LinearDiscriminantAnalysis(priors=[0.5, 0.5])
    clf.fit(X, y)

    # Test that priors always sum to 1
    priors = np.array([0.5, 0.6])
    prior_norm = np.array([0.45, 0.55])
    clf = LinearDiscriminantAnalysis(priors=priors)
    assert_warns(UserWarning, clf.fit, X, y)
    assert_array_almost_equal(clf.priors_, prior_norm, 2) 
Example #10
Source File: testScoreWithAdapaSklearn.py    From nyoka with Apache License 2.0 6 votes vote down vote up
def test_11_lda(self):
        print("\ntest 11 (LDA with preprocessing) [multi-class]\n")
        X, X_test, y, features, target, test_file = self.data_utility.get_data_for_multi_class_classification()

        model = LinearDiscriminantAnalysis()
        pipeline_obj = Pipeline([
            ("scaler", MaxAbsScaler()),
            ("model", model)
        ])
        pipeline_obj.fit(X,y)
        file_name = 'test11sklearn.pmml'
        
        skl_to_pmml(pipeline_obj, features, target, file_name)
        model_name  = self.adapa_utility.upload_to_zserver(file_name)
        predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, test_file)
        model_pred = pipeline_obj.predict(X_test)
        model_prob = pipeline_obj.predict_proba(X_test)
        self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True)
        self.assertEqual(self.adapa_utility.compare_probability(probabilities, model_prob), True) 
Example #11
Source File: test_discriminant_analysis.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_lda_coefs():
    # Test if the coefficients of the solvers are approximately the same.
    n_features = 2
    n_classes = 2
    n_samples = 1000
    X, y = make_blobs(n_samples=n_samples, n_features=n_features,
                      centers=n_classes, random_state=11)

    clf_lda_svd = LinearDiscriminantAnalysis(solver="svd")
    clf_lda_lsqr = LinearDiscriminantAnalysis(solver="lsqr")
    clf_lda_eigen = LinearDiscriminantAnalysis(solver="eigen")

    clf_lda_svd.fit(X, y)
    clf_lda_lsqr.fit(X, y)
    clf_lda_eigen.fit(X, y)

    assert_array_almost_equal(clf_lda_svd.coef_, clf_lda_lsqr.coef_, 1)
    assert_array_almost_equal(clf_lda_svd.coef_, clf_lda_eigen.coef_, 1)
    assert_array_almost_equal(clf_lda_eigen.coef_, clf_lda_lsqr.coef_, 1) 
Example #12
Source File: test_discriminant_analysis.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_lda_explained_variance_ratio():
    # Test if the sum of the normalized eigen vectors values equals 1,
    # Also tests whether the explained_variance_ratio_ formed by the
    # eigen solver is the same as the explained_variance_ratio_ formed
    # by the svd solver

    state = np.random.RandomState(0)
    X = state.normal(loc=0, scale=100, size=(40, 20))
    y = state.randint(0, 3, size=(40,))

    clf_lda_eigen = LinearDiscriminantAnalysis(solver="eigen")
    clf_lda_eigen.fit(X, y)
    assert_almost_equal(clf_lda_eigen.explained_variance_ratio_.sum(), 1.0, 3)
    assert_equal(clf_lda_eigen.explained_variance_ratio_.shape, (2,),
                 "Unexpected length for explained_variance_ratio_")

    clf_lda_svd = LinearDiscriminantAnalysis(solver="svd")
    clf_lda_svd.fit(X, y)
    assert_almost_equal(clf_lda_svd.explained_variance_ratio_.sum(), 1.0, 3)
    assert_equal(clf_lda_svd.explained_variance_ratio_.shape, (2,),
                 "Unexpected length for explained_variance_ratio_")

    assert_array_almost_equal(clf_lda_svd.explained_variance_ratio_,
                              clf_lda_eigen.explained_variance_ratio_) 
Example #13
Source File: test_discriminant_analysis.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_lda_scaling():
    # Test if classification works correctly with differently scaled features.
    n = 100
    rng = np.random.RandomState(1234)
    # use uniform distribution of features to make sure there is absolutely no
    # overlap between classes.
    x1 = rng.uniform(-1, 1, (n, 3)) + [-10, 0, 0]
    x2 = rng.uniform(-1, 1, (n, 3)) + [10, 0, 0]
    x = np.vstack((x1, x2)) * [1, 100, 10000]
    y = [-1] * n + [1] * n

    for solver in ('svd', 'lsqr', 'eigen'):
        clf = LinearDiscriminantAnalysis(solver=solver)
        # should be able to separate the data perfectly
        assert_equal(clf.fit(x, y).score(x, y), 1.0,
                     'using covariance: %s' % solver) 
Example #14
Source File: test_discriminant_analysis.py    From Mastering-Elasticsearch-7.0 with MIT License 6 votes vote down vote up
def test_lda_scaling():
    # Test if classification works correctly with differently scaled features.
    n = 100
    rng = np.random.RandomState(1234)
    # use uniform distribution of features to make sure there is absolutely no
    # overlap between classes.
    x1 = rng.uniform(-1, 1, (n, 3)) + [-10, 0, 0]
    x2 = rng.uniform(-1, 1, (n, 3)) + [10, 0, 0]
    x = np.vstack((x1, x2)) * [1, 100, 10000]
    y = [-1] * n + [1] * n

    for solver in ('svd', 'lsqr', 'eigen'):
        clf = LinearDiscriminantAnalysis(solver=solver)
        # should be able to separate the data perfectly
        assert_equal(clf.fit(x, y).score(x, y), 1.0,
                     'using covariance: %s' % solver) 
Example #15
Source File: test_discriminant_analysis.py    From Mastering-Elasticsearch-7.0 with MIT License 6 votes vote down vote up
def test_lda_explained_variance_ratio():
    # Test if the sum of the normalized eigen vectors values equals 1,
    # Also tests whether the explained_variance_ratio_ formed by the
    # eigen solver is the same as the explained_variance_ratio_ formed
    # by the svd solver

    state = np.random.RandomState(0)
    X = state.normal(loc=0, scale=100, size=(40, 20))
    y = state.randint(0, 3, size=(40,))

    clf_lda_eigen = LinearDiscriminantAnalysis(solver="eigen")
    clf_lda_eigen.fit(X, y)
    assert_almost_equal(clf_lda_eigen.explained_variance_ratio_.sum(), 1.0, 3)
    assert_equal(clf_lda_eigen.explained_variance_ratio_.shape, (2,),
                 "Unexpected length for explained_variance_ratio_")

    clf_lda_svd = LinearDiscriminantAnalysis(solver="svd")
    clf_lda_svd.fit(X, y)
    assert_almost_equal(clf_lda_svd.explained_variance_ratio_.sum(), 1.0, 3)
    assert_equal(clf_lda_svd.explained_variance_ratio_.shape, (2,),
                 "Unexpected length for explained_variance_ratio_")

    assert_array_almost_equal(clf_lda_svd.explained_variance_ratio_,
                              clf_lda_eigen.explained_variance_ratio_) 
Example #16
Source File: test_common.py    From Mastering-Elasticsearch-7.0 with MIT License 6 votes vote down vote up
def _tested_estimators():
    for name, Estimator in all_estimators():
        if issubclass(Estimator, BiclusterMixin):
            continue
        if name.startswith("_"):
            continue
        # FIXME _skip_test should be used here (if we could)

        required_parameters = getattr(Estimator, "_required_parameters", [])
        if len(required_parameters):
            if required_parameters in (["estimator"], ["base_estimator"]):
                if issubclass(Estimator, RegressorMixin):
                    estimator = Estimator(Ridge())
                else:
                    estimator = Estimator(LinearDiscriminantAnalysis())
            else:
                warnings.warn("Can't instantiate estimator {} which requires "
                              "parameters {}".format(name,
                                                     required_parameters),
                              SkipTestWarning)
                continue
        else:
            estimator = Estimator()
        yield name, estimator 
Example #17
Source File: explorers.py    From yass with Apache License 2.0 6 votes vote down vote up
def plot_lda(self, group_ids, channels, sample=None, ax=None):
        """
        Reduce dimensionality using LDA and plot data
        """
        ax = plt if ax is None else ax

        scores, labels = self.scores_for_groups(group_ids, channels)

        lda = LDA(n_components=2)
        reduced = lda.fit_transform(scores, labels)

        for color in np.unique(group_ids).astype('int'):
            x = reduced[labels == color, 0]
            y = reduced[labels == color, 1]

            if sample:
                x = np.random.choice(x, size=int(sample*len(x)), replace=False)
                y = np.random.choice(x, size=int(sample*len(y)), replace=False)

            ax.scatter(x, y, label='Group {}'.format(color), alpha=0.7)

        ax.legend() 
Example #18
Source File: explorers.py    From yass with Apache License 2.0 6 votes vote down vote up
def plot_closest_clusters_to(self, group_id, k, mode='LDA',
                                 sample=None, ax=None):
        """Visualize close clusters
        """
        ax = plt if ax is None else ax

        # get similar templates to template with id group_id
        groups = self.close_templates(group_id, k)

        # get the neighbors for the main channel in group_id
        main = self.main_channel_for_group(group_id)
        neighbors = self.recording_explorer.neighbors_for_channel(main)

        if mode == 'LDA':
            self.plot_lda(groups, neighbors, sample=sample, ax=ax)
        elif mode == 'PCA':
            self.plot_pca(groups, neighbors, sample=sample, ax=ax)
        else:
            raise ValueError('Only PCA and LDA modes are supported') 
Example #19
Source File: test_discriminant_analysis.py    From Mastering-Elasticsearch-7.0 with MIT License 6 votes vote down vote up
def test_lda_priors():
    # Test priors (negative priors)
    priors = np.array([0.5, -0.5])
    clf = LinearDiscriminantAnalysis(priors=priors)
    msg = "priors must be non-negative"
    assert_raise_message(ValueError, msg, clf.fit, X, y)

    # Test that priors passed as a list are correctly handled (run to see if
    # failure)
    clf = LinearDiscriminantAnalysis(priors=[0.5, 0.5])
    clf.fit(X, y)

    # Test that priors always sum to 1
    priors = np.array([0.5, 0.6])
    prior_norm = np.array([0.45, 0.55])
    clf = LinearDiscriminantAnalysis(priors=priors)
    assert_warns(UserWarning, clf.fit, X, y)
    assert_array_almost_equal(clf.priors_, prior_norm, 2) 
Example #20
Source File: merge.py    From yass with Apache License 2.0 5 votes vote down vote up
def run_diptest2(features, assignment):

    '''
    Parameters
    ----------
    pca_wf:  pca projected data
    assignment:  spike assignments
    '''

    _, n_spikes = np.unique(assignment, return_counts=True)
    id_big = np.argmax(n_spikes)
    id_small = np.argmin(n_spikes)
    n_diff = n_spikes[id_big] - n_spikes[id_small]

    if n_diff > 0:
        n_repeat = int(np.ceil(np.max(n_spikes)/np.min(n_spikes)))
        idx_big = np.where(assignment == id_big)[0]

        pvals = np.zeros(n_repeat)
        for j in range(n_repeat):
            idx_remove = np.random.choice(idx_big, n_diff, replace=False)
            idx_in = np.ones(len(assignment), 'bool')
            idx_in[idx_remove] = False

            # fit lda
            lda = LDA(n_components = 1)
            lda_feat = lda.fit_transform(features[idx_in], assignment[idx_in]).ravel()

            # check tp of lda
            pvals[j] = dp(lda_feat)[1]

        pval = np.median(pvals)
    else:
        lda = LDA(n_components = 1)
        lda_feat = lda.fit_transform(features, assignment).ravel()
        pval = dp(lda_feat)[1]

    return pval 
Example #21
Source File: test_discriminant_analysis.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_lda_orthogonality():
    # arrange four classes with their means in a kite-shaped pattern
    # the longer distance should be transformed to the first component, and
    # the shorter distance to the second component.
    means = np.array([[0, 0, -1], [0, 2, 0], [0, -2, 0], [0, 0, 5]])

    # We construct perfectly symmetric distributions, so the LDA can estimate
    # precise means.
    scatter = np.array([[0.1, 0, 0], [-0.1, 0, 0], [0, 0.1, 0], [0, -0.1, 0],
                        [0, 0, 0.1], [0, 0, -0.1]])

    X = (means[:, np.newaxis, :] + scatter[np.newaxis, :, :]).reshape((-1, 3))
    y = np.repeat(np.arange(means.shape[0]), scatter.shape[0])

    # Fit LDA and transform the means
    clf = LinearDiscriminantAnalysis(solver="svd").fit(X, y)
    means_transformed = clf.transform(means)

    d1 = means_transformed[3] - means_transformed[0]
    d2 = means_transformed[2] - means_transformed[1]
    d1 /= np.sqrt(np.sum(d1 ** 2))
    d2 /= np.sqrt(np.sum(d2 ** 2))

    # the transformed within-class covariance should be the identity matrix
    assert_almost_equal(np.cov(clf.transform(scatter).T), np.eye(2))

    # the means of classes 0 and 3 should lie on the first component
    assert_almost_equal(np.abs(np.dot(d1[:2], [1, 0])), 1.0)

    # the means of classes 1 and 2 should lie on the second component
    assert_almost_equal(np.abs(np.dot(d2[:2], [0, 1])), 1.0) 
Example #22
Source File: merge.py    From yass with Apache License 2.0 5 votes vote down vote up
def run_ldatest(features, assignment):

    # determine which one has more spikes
    _, n_spikes = np.unique(assignment, return_counts=True)
    id_big = np.argmax(n_spikes)
    id_small = np.argmin(n_spikes)
    n_diff = n_spikes[id_big] - n_spikes[id_small]

    if n_diff > 0:
        n_repeat = int(np.ceil(np.max(n_spikes)/np.min(n_spikes)))
        idx_big = np.where(assignment == id_big)[0]

        lda_probs = np.zeros(n_repeat)
        for j in range(n_repeat):
            idx_remove = np.random.choice(idx_big, n_diff, replace=False)
            idx_in = np.ones(len(assignment), 'bool')
            idx_in[idx_remove] = False

            # fit lda
            lda = LDA(n_components = 1)
            lda.fit(features[idx_in], assignment[idx_in])

            # check tp of lda
            lda_probs[j] = lda.score(features[idx_in],
                                     assignment[idx_in])
        lda_prob = np.median(lda_probs)
    else:
        lda = LDA(n_components = 1)
        lda.fit(features, assignment)
        lda_prob = lda.score(features,
                             assignment)

    return lda_prob 
Example #23
Source File: explorers.py    From yass with Apache License 2.0 5 votes vote down vote up
def plot_all_clusters(self, k, mode='LDA', sample=None, ax=None,
                          sharex=True, sharey=False, max_cols=None):
        ax = plt if ax is None else ax

        fn = partial(self.plot_closest_clusters_to, k=k, mode=mode,
                     sample=sample)

        _make_grid_plot(fn, self.all_ids, ax, sharex, sharey, max_cols) 
Example #24
Source File: test_discriminant_analysis.py    From pandas-ml with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_objectmapper(self):
        df = pdml.ModelFrame([])
        self.assertIs(df.discriminant_analysis.LinearDiscriminantAnalysis,
                      da.LinearDiscriminantAnalysis)
        self.assertIs(df.discriminant_analysis.QuadraticDiscriminantAnalysis,
                      da.QuadraticDiscriminantAnalysis)

        self.assertIs(df.da.LinearDiscriminantAnalysis,
                      da.LinearDiscriminantAnalysis)
        self.assertIs(df.da.QuadraticDiscriminantAnalysis,
                      da.QuadraticDiscriminantAnalysis) 
Example #25
Source File: test_discriminant_analysis.py    From pandas-ml with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_objectmapper_deprecated(self):
        df = pdml.ModelFrame([])
        with tm.assert_produces_warning(FutureWarning):
            self.assertIs(df.lda.LinearDiscriminantAnalysis,
                          da.LinearDiscriminantAnalysis)
        with tm.assert_produces_warning(FutureWarning):
            self.assertIs(df.qda.QuadraticDiscriminantAnalysis,
                          da.QuadraticDiscriminantAnalysis) 
Example #26
Source File: tangentspace.py    From decoding-brain-challenge-2016 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _fit_lda(self, X, y, sample_weight=None):
        """Helper to fit LDA."""
        self.classes_ = numpy.unique(y)
        self._lda = LDA(n_components=len(self.classes_) - 1,
                        solver='lsqr',
                        shrinkage='auto')

        ts = self._ts.fit_transform(X, sample_weight=sample_weight)
        self._lda.fit(ts, y)

        W = self._lda.coef_.copy()
        self._W = numpy.dot(
            numpy.dot(W.T, numpy.linalg.pinv(numpy.dot(W, W.T))), W)
        return ts 
Example #27
Source File: fextraction.py    From Open-Myo with GNU General Public License v3.0 5 votes vote down vote up
def feature_scaling(feature_matrix,target,reductor=None,scaler=None):
    lda = LDA(n_components=2)
    minmax = MinMaxScaler(feature_range=(-1,1))
    if not reductor:
        reductor = lda.fit(feature_matrix,target)
    feature_matrix_lda = reductor.transform(feature_matrix)
    if not scaler:
        scaler = minmax.fit(feature_matrix_lda)
    feature_matrix_scaled = scaler.transform(feature_matrix_lda)
    return feature_matrix_scaled,reductor,scaler 
Example #28
Source File: lda.py    From SecuML with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, conf):
        SemiSupervisedProjection.__init__(self, conf)
        self.projection = discriminant_analysis.LinearDiscriminantAnalysis(
            n_components=conf.num_components)
        if not self.conf.multiclass:
            self.conf.logger.warning(
                    'Lda projection without families supervision. '
                    'The projection space is of dimension 1, and so the '
                    'projected instances cannot be displayed with hexagonal '
                    'binnnings.') 
Example #29
Source File: lda.py    From SecuML with GNU General Public License v2.0 5 votes vote down vote up
def gen_input_labels(self, instances):
        labels, instances = SemiSupervisedProjection.gen_input_labels(
                                                                     self,
                                                                     instances)
        num_classes = len(set(labels))
        if (self.conf.num_components is not None and
                self.conf.num_components > num_classes - 1):
            self.conf.logger.warning(
                    'The embedding dimension must be smaller '
                    'than the number of classes - 1. '
                    'num_components is set to %d.' % (num_classes - 1))
            self.num_components = num_classes - 1
            self.projection = discriminant_analysis.LinearDiscriminantAnalysis(
                n_components=self.conf.num_components)
        return labels, instances 
Example #30
Source File: test_testing.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_set_random_state():
    lda = LinearDiscriminantAnalysis()
    tree = DecisionTreeClassifier()
    # Linear Discriminant Analysis doesn't have random state: smoke test
    set_random_state(lda, 3)
    set_random_state(tree, 3)
    assert_equal(tree.random_state, 3)