Python sklearn.linear_model.logistic.LogisticRegression() Examples

The following are 13 code examples of sklearn.linear_model.logistic.LogisticRegression(). 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.linear_model.logistic , or try the search function .
Example #1
Source File: test_bounds.py    From Mastering-Elasticsearch-7.0 with MIT License 6 votes vote down vote up
def check_l1_min_c(X, y, loss, fit_intercept=True, intercept_scaling=None):
    min_c = l1_min_c(X, y, loss, fit_intercept, intercept_scaling)

    clf = {
        'log': LogisticRegression(penalty='l1', solver='liblinear',
                                  multi_class='ovr'),
        'squared_hinge': LinearSVC(loss='squared_hinge',
                                   penalty='l1', dual=False),
    }[loss]

    clf.fit_intercept = fit_intercept
    clf.intercept_scaling = intercept_scaling

    clf.C = min_c
    clf.fit(X, y)
    assert (np.asarray(clf.coef_) == 0).all()
    assert (np.asarray(clf.intercept_) == 0).all()

    clf.C = min_c * 1.01
    clf.fit(X, y)
    assert ((np.asarray(clf.coef_) != 0).any() or
            (np.asarray(clf.intercept_) != 0).any()) 
Example #2
Source File: transitions.py    From recurrent-slds with MIT License 6 votes vote down vote up
def initialize_with_logistic_regression(self, zs, xs):
        from sklearn.linear_model.logistic import LogisticRegression
        lr = LogisticRegression(verbose=False, multi_class="multinomial", solver="lbfgs")

        # Make the covariates
        K, D = self.num_states, self.covariate_dim
        zs = zs if isinstance(zs, np.ndarray) else np.concatenate(zs, axis=0)
        xs = xs if isinstance(xs, np.ndarray) else np.concatenate(xs, axis=0)
        assert zs.shape[0] == xs.shape[0]
        assert zs.ndim == 1 and zs.dtype == np.int32 and zs.min() >= 0 and zs.max() < K
        assert xs.ndim == 2 and xs.shape[1] == D

        lr_X = xs[:-1]
        lr_y = zs[1:]
        lr.fit(lr_X, lr_y)

        # Now convert the logistic regression into weights
        used = np.bincount(zs, minlength=K) > 0
        self.W = np.zeros((D, K))
        self.W[:, used] = lr.coef_.T
        b = np.zeros((K,))
        b[used] += lr.intercept_
        b[~used] += -100.
        self.b = b 
Example #3
Source File: logistic_regression.py    From lale with Apache License 2.0 6 votes vote down vote up
def __init__(self, penalty='l2', dual=False, tol=0.0001, C=1.0, fit_intercept=True, intercept_scaling=1, class_weight='balanced', random_state=None, solver='liblinear', max_iter=100, multi_class='ovr', verbose=0, warm_start=False, n_jobs=None):
        self._hyperparams = {
            'penalty': penalty,
            'dual': dual,
            'tol': tol,
            'C': C,
            'fit_intercept': fit_intercept,
            'intercept_scaling': intercept_scaling,
            'class_weight': class_weight,
            'random_state': random_state,
            'solver': solver,
            'max_iter': max_iter,
            'multi_class': multi_class,
            'verbose': verbose,
            'warm_start': warm_start,
            'n_jobs': n_jobs}
        self._wrapped_model = Op(**self._hyperparams) 
Example #4
Source File: test_pipeline.py    From sparkit-learn with Apache License 2.0 6 votes vote down vote up
def test_pipeline_same_results(self):
        X, y, Z = self.make_classification(2, 10000, 2000)

        loc_clf = LogisticRegression()
        loc_filter = VarianceThreshold()
        loc_pipe = Pipeline([
            ('threshold', loc_filter),
            ('logistic', loc_clf)
        ])

        dist_clf = SparkLogisticRegression()
        dist_filter = SparkVarianceThreshold()
        dist_pipe = SparkPipeline([
            ('threshold', dist_filter),
            ('logistic', dist_clf)
        ])

        dist_filter.fit(Z)
        loc_pipe.fit(X, y)
        dist_pipe.fit(Z, logistic__classes=np.unique(y))

        assert_true(np.mean(np.abs(
            loc_pipe.predict(X) -
            np.concatenate(dist_pipe.predict(Z[:, 'X']).collect())
        )) < 0.1) 
Example #5
Source File: test_bounds.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def check_l1_min_c(X, y, loss, fit_intercept=True, intercept_scaling=None):
    min_c = l1_min_c(X, y, loss, fit_intercept, intercept_scaling)

    clf = {
        'log': LogisticRegression(penalty='l1'),
        'squared_hinge': LinearSVC(loss='squared_hinge',
                                   penalty='l1', dual=False),
    }[loss]

    clf.fit_intercept = fit_intercept
    clf.intercept_scaling = intercept_scaling

    clf.C = min_c
    clf.fit(X, y)
    assert_true((np.asarray(clf.coef_) == 0).all())
    assert_true((np.asarray(clf.intercept_) == 0).all())

    clf.C = min_c * 1.01
    clf.fit(X, y)
    assert_true((np.asarray(clf.coef_) != 0).any() or
                (np.asarray(clf.intercept_) != 0).any()) 
Example #6
Source File: train.py    From AI_for_Financial_Data with Apache License 2.0 5 votes vote down vote up
def new_grid_search():
    """ Create new GridSearch obj with models pipeline """
    pipeline = Pipeline([
        # TODO some smart preproc can be added here
        (u"clf", LogisticRegression(class_weight="balanced")),
    ])
    search_params = {"clf__C": (1e-4, 1e-2, 1e0, 1e2, 1e4)}
    return GridSearchCV(
        estimator=pipeline,
        param_grid=search_params,
        scoring="recall_macro",
        cv=10,
        n_jobs=-1,
        verbose=3,
    ) 
Example #7
Source File: 02_ceps_based_classifier.py    From Building-Machine-Learning-Systems-With-Python-Second-Edition with MIT License 5 votes vote down vote up
def create_model():
    from sklearn.linear_model.logistic import LogisticRegression
    clf = LogisticRegression()

    return clf 
Example #8
Source File: 01_fft_based_classifier.py    From Building-Machine-Learning-Systems-With-Python-Second-Edition with MIT License 5 votes vote down vote up
def create_model():
    from sklearn.linear_model.logistic import LogisticRegression
    clf = LogisticRegression()

    return clf 
Example #9
Source File: meta_classifier.py    From coling2018_fake-news-challenge with Apache License 2.0 5 votes vote down vote up
def getEstimator(scorer_type):
    if scorer_type == 'grad_boost':
        clf = GradientBoostingClassifier(n_estimators=200, random_state=14128, verbose=True)

    if scorer_type == 'svm1': # stochastic gradient decent classifier
        clf = svm.SVC(gamma=0.001, C=100., verbose=True)

    if scorer_type == 'logistic_regression' :
        clf = logistic.LogisticRegression()

    if scorer_type == 'svm3':
        clf = svm.SVC(kernel='poly', C=1.0, probability=True, class_weight='unbalanced')

    if scorer_type == "bayes":
        clf = naive_bayes.GaussianNB()

    if scorer_type == 'voting_hard_svm_gradboost_logistic':
        svm2 = svm.SVC(kernel='linear', C=1.0, probability=True, class_weight='balanced', verbose=True)
        log_reg = logistic.LogisticRegression()
        gradboost = GradientBoostingClassifier(n_estimators=200, random_state=14128, verbose=True)

        clf = VotingClassifier(estimators=[  # ('gb', gb),
            ('svm', svm2),
            ('grad_boost', gradboost),
            ('logisitc_regression', log_reg)
        ],  n_jobs=1,
            voting='hard')

    if scorer_type == 'voting_hard_bayes_gradboost':
        bayes = naive_bayes.GaussianNB()
        gradboost = GradientBoostingClassifier(n_estimators=200, random_state=14128, verbose=True)

        clf = VotingClassifier(estimators=[  # ('gb', gb),
            ('bayes', bayes),
            ('grad_boost', gradboost),
        ],  n_jobs=1,
            voting='hard')

    return clf 
Example #10
Source File: train.py    From fraud-detection with Apache License 2.0 5 votes vote down vote up
def new_grid_search():
    """ Create new GridSearch obj with models pipeline """
    pipeline = Pipeline([
        # TODO some smart preproc can be added here
        (u"clf", LogisticRegression(class_weight="balanced")),
    ])
    search_params = {"clf__C": (1e-4, 1e-2, 1e0, 1e2, 1e4)}
    return GridSearchCV(
        estimator=pipeline,
        param_grid=search_params,
        scoring="recall_macro",
        cv=10,
        n_jobs=-1,
        verbose=3,
    ) 
Example #11
Source File: test_util.py    From cxplain with MIT License 5 votes vote down vote up
def get_classification_models():
        models = [
            LogisticRegression(random_state=1),
            RandomForestClassifier(n_estimators=64, max_depth=5, random_state=1),
        ]
        return models 
Example #12
Source File: test_util.py    From cxplain with MIT License 5 votes vote down vote up
def fit_proxy(explained_model, x, y):
        if isinstance(explained_model, LogisticRegression):
            y_cur = np.argmax(y, axis=-1)
        else:
            y_cur = y
        explained_model.fit(x, y_cur) 
Example #13
Source File: transitions.py    From recurrent-slds with MIT License 4 votes vote down vote up
def initialize_with_logistic_regression(self, zs, xs, initialize=False):
        from sklearn.linear_model.logistic import LogisticRegression
        if not hasattr(self, '_lr'):
            self._lr = LogisticRegression(verbose=False,
                                          multi_class="multinomial",
                                          solver="lbfgs",
                                          warm_start=True,
                                          max_iter=10)
        lr = self._lr

        # Make the covariates
        K, D = self.num_states, self.covariate_dim

        # Split zs into prevs and nexts
        zps = zs[:-1] if isinstance(zs, np.ndarray) else np.concatenate([z[:-1] for z in zs], axis=0)
        zns = zs[1:] if isinstance(zs, np.ndarray) else np.concatenate([z[1:] for z in zs], axis=0)
        xps = xs[:-1] if isinstance(xs, np.ndarray) else np.concatenate([x[:-1] for x in xs], axis=0)

        assert zps.shape[0] == xps.shape[0]
        assert zps.ndim == 1 and zps.dtype == np.int32 and zps.min() >= 0 and zps.max() < K
        assert zns.ndim == 1 and zns.dtype == np.int32 and zns.min() >= 0 and zns.max() < K
        assert xps.ndim == 2 and xps.shape[1] == D

        used = np.bincount(zns, minlength=K) > 0
        K_used = np.sum(used)

        lr_X = np.column_stack((one_hot(zps, K), xps))
        lr_y = zns

        # The logistic regression solver fails if we only have one class represented
        # In this case, set the regression weights to zero and set logpi to have
        # high probability of the visited class
        if K_used == 1:
            self.W = np.zeros((D, K))
            self.log_pi = np.zeros((K, K))
            self.log_pi[:, used] = 3.0
        else:
            lr.fit(lr_X, lr_y)

            # Now convert the logistic regression into weights
            if K_used > 2:
                self.W = np.zeros((D, K))
                self.W[:, used] = lr.coef_[:, K:].T
                self.logpi = np.zeros((K, K))
                self.logpi[:, used] = lr.coef_[:, :K].T
                self.logpi[:, used] += lr.intercept_[None, :]
                self.logpi[:, ~used] += -100.

            elif K_used == 2:
                # LogisticRegression object only represents one
                # set of weights for binary problems
                self.W = np.zeros((D, K))
                self.W[:, 1] = lr.coef_[0, K:]
                self.logpi = np.zeros((K, K))
                self.logpi[:, 1] = lr.coef_[0, :K].T
                self.logpi[:, 1] += lr.intercept_