本文整理汇总了Python中sklearn.linear_model.LogisticRegression方法的典型用法代码示例。如果您正苦于以下问题:Python linear_model.LogisticRegression方法的具体用法?Python linear_model.LogisticRegression怎么用?Python linear_model.LogisticRegression使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.linear_model
的用法示例。
在下文中一共展示了linear_model.LogisticRegression方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _build_model
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import LogisticRegression [as 别名]
def _build_model(self,model_name,params=None):
if params==None:
if model_name=='xgb':
self.model=XGBClassifier(n_estimators=100,learning_rate=0.02)
elif model_name=='svm':
kernel_function=chi2_kernel if not (self.model_kernel=='linear' or self.model_kernel=='rbf') else self.model_kernel
self.model=SVC(C=1,kernel=kernel_function,gamma=1,probability=True)
elif model_name=='lr':
self.model=LR(C=1,penalty='l1',tol=1e-6)
else:
if model_name=='xgb':
self.model=XGBClassifier(n_estimators=1000,learning_rate=0.02,**params)
elif model_name=='svm':
self.model=SVC(C=1,kernel=kernel_function,gamma=1,probability=True)
elif model_name=='lr':
self.model=LR(C=1,penalty='l1',tol=1e-6)
log.l.info('=======> built the model {} done'.format(self.model_name))
示例2: _check_autograd_supported
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import LogisticRegression [as 别名]
def _check_autograd_supported(base_algorithm):
supported = ['LogisticRegression', 'SGDClassifier', 'RidgeClassifier', 'StochasticLogisticRegression', 'LinearRegression']
if not base_algorithm.__class__.__name__ in supported:
raise ValueError("Automatic gradients only implemented for the following classes: " + ", ".join(supported))
if base_algorithm.__class__.__name__ == 'LogisticRegression':
if base_algorithm.penalty != 'l2':
raise ValueError("Automatic gradients only defined for LogisticRegression with l2 regularization.")
if base_algorithm.intercept_scaling != 1:
raise ValueError("Automatic gradients for LogisticRegression not implemented with 'intercept_scaling'.")
if base_algorithm.__class__.__name__ == 'RidgeClassifier':
if base_algorithm.normalize:
raise ValueError("Automatic gradients for LogisticRegression only implemented without 'normalize'.")
if base_algorithm.__class__.__name__ == 'SGDClassifier':
if base_algorithm.loss != 'log':
raise ValueError("Automatic gradients for LogisticRegression only implemented with logistic loss.")
if base_algorithm.penalty != 'l2':
raise ValueError("Automatic gradients only defined for LogisticRegression with l2 regularization.")
try:
if base_algorithm.class_weight is not None:
raise ValueError("Automatic gradients for LogisticRegression not supported with 'class_weight'.")
except:
pass
示例3: __init__
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import LogisticRegression [as 别名]
def __init__(self, lambda_=1., fit_intercept=True, alpha=0.95,
m=1.0, ts=False, ts_from_ci=True, sample_unique=False, random_state=1):
self.conf_coef = alpha
self.m = m
self.fit_intercept = fit_intercept
self.lambda_ = lambda_
self.ts = ts
self.ts_from_ci = ts_from_ci
self.warm_start = True
self.sample_unique = bool(sample_unique)
self.random_state = _check_random_state(random_state)
self.is_fitted = False
self.model = LogisticRegression(C=1./lambda_, penalty="l2",
fit_intercept=fit_intercept,
solver='lbfgs', max_iter=15000,
warm_start=True)
self.Sigma = np.empty((0,0), dtype=np.float64)
示例4: test_bad_params
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import LogisticRegression [as 别名]
def test_bad_params(self):
X = [[1]]
y = [0]
with self.assertRaises(ValueError):
LogisticRegression(data_norm=1, C=-1).fit(X, y)
with self.assertRaises(ValueError):
LogisticRegression(data_norm=1, C=1.2).fit(X, y)
with self.assertRaises(ValueError):
LogisticRegression(data_norm=1, max_iter=-1).fit(X, y)
with self.assertRaises(ValueError):
LogisticRegression(data_norm=1, max_iter="100").fit(X, y)
with self.assertRaises(ValueError):
LogisticRegression(data_norm=1, tol=-1).fit(X, y)
with self.assertRaises(ValueError):
LogisticRegression(data_norm=1, tol="1").fit(X, y)
示例5: test_different_results
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import LogisticRegression [as 别名]
def test_different_results(self):
from sklearn import datasets
from sklearn import linear_model
from sklearn.model_selection import train_test_split
dataset = datasets.load_iris()
X_train, X_test, y_train, y_test = train_test_split(dataset.data, dataset.target, test_size=0.2)
clf = LogisticRegression(data_norm=12)
clf.fit(X_train, y_train)
predict1 = clf.predict(X_test)
clf = LogisticRegression(data_norm=12)
clf.fit(X_train, y_train)
predict2 = clf.predict(X_test)
clf = linear_model.LogisticRegression(solver="lbfgs", multi_class="ovr")
clf.fit(X_train, y_train)
predict3 = clf.predict(X_test)
self.assertFalse(np.all(predict1 == predict2))
self.assertFalse(np.all(predict3 == predict1) and np.all(predict3 == predict2))
示例6: test_same_results
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import LogisticRegression [as 别名]
def test_same_results(self):
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn import linear_model
dataset = datasets.load_iris()
X_train, X_test, y_train, y_test = train_test_split(dataset.data, dataset.target, test_size=0.2)
clf = LogisticRegression(data_norm=12, epsilon=float("inf"))
clf.fit(X_train, y_train)
predict1 = clf.predict(X_test)
clf = linear_model.LogisticRegression(solver="lbfgs", multi_class="ovr")
clf.fit(X_train, y_train)
predict2 = clf.predict(X_test)
self.assertTrue(np.all(predict1 == predict2))
示例7: test_accountant
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import LogisticRegression [as 别名]
def test_accountant(self):
from diffprivlib.accountant import BudgetAccountant
acc = BudgetAccountant()
X = np.array(
[0.50, 0.75, 1.00, 1.25, 1.50, 1.75, 1.75, 2.00, 2.25, 2.50, 2.75, 3.00, 3.25, 3.50, 4.00, 4.25, 4.50, 4.75,
5.00, 5.50])
y = np.array([0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1])
X = X[:, np.newaxis]
X -= 3.0
X /= 2.5
clf = LogisticRegression(epsilon=2, data_norm=1.0, accountant=acc)
clf.fit(X, y)
self.assertEqual((2, 0), acc.total())
with BudgetAccountant(3, 0) as acc2:
clf = LogisticRegression(epsilon=2, data_norm=1.0)
clf.fit(X, y)
self.assertEqual((2, 0), acc2.total())
with self.assertRaises(BudgetError):
clf.fit(X, y)
示例8: prepare_fit_model_for_factors
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import LogisticRegression [as 别名]
def prepare_fit_model_for_factors(model_type, x_train, y_train):
"""
Given a model type, train and test data
Args:
model_type (str): 'classification' or 'regression'
x_train:
y_train:
Returns:
(sklearn.base.BaseEstimator): A fit model.
"""
if model_type == 'classification':
algorithm = LogisticRegression()
elif model_type == 'regression':
algorithm = LinearRegression()
else:
algorithm = None
if algorithm is not None:
algorithm.fit(x_train, y_train)
return algorithm
示例9: __init__
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import LogisticRegression [as 别名]
def __init__(self, *args, **kwargs):
super(MaximumLossReductionMaximalConfidence, self).__init__(*args, **kwargs)
# self.n_labels = len(self.dataset.get_labeled_entries()[0][1])
self.n_labels = len(self.dataset.get_labeled_entries()[1][0])
random_state = kwargs.pop('random_state', None)
self.random_state_ = seed_random_state(random_state)
self.logreg_param = kwargs.pop('logreg_param',
{'multi_class': 'multinomial',
'solver': 'newton-cg',
'random_state': random_state})
self.logistic_regression_ = LogisticRegression(**self.logreg_param)
self.br_base = kwargs.pop('br_base',
SklearnProbaAdapter(SVC(kernel='linear',
probability=True,
gamma="auto",
random_state=random_state)))
示例10: compute_acc
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import LogisticRegression [as 别名]
def compute_acc(emb, labels, train_nids, val_nids, test_nids):
"""
Compute the accuracy of prediction given the labels.
"""
emb = emb.cpu().numpy()
train_nids = train_nids.cpu().numpy()
train_labels = labels[train_nids].cpu().numpy()
val_nids = val_nids.cpu().numpy()
val_labels = labels[val_nids].cpu().numpy()
test_nids = test_nids.cpu().numpy()
test_labels = labels[test_nids].cpu().numpy()
emb = (emb - emb.mean(0, keepdims=True)) / emb.std(0, keepdims=True)
lr = lm.LogisticRegression(multi_class='multinomial', max_iter=10000)
lr.fit(emb[train_nids], labels[train_nids])
pred = lr.predict(emb)
f1_micro_eval = skm.f1_score(labels[val_nids], pred[val_nids], average='micro')
f1_micro_test = skm.f1_score(labels[test_nids], pred[test_nids], average='micro')
f1_macro_eval = skm.f1_score(labels[val_nids], pred[val_nids], average='macro')
f1_macro_test = skm.f1_score(labels[test_nids], pred[test_nids], average='macro')
return f1_micro_eval, f1_micro_test
示例11: __init__
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import LogisticRegression [as 别名]
def __init__(self, base_estimator=LogisticRegression(penalty='l1'), lambda_name='C',
lambda_grid=np.logspace(-5, -2, 25), n_bootstrap_iterations=100,
sample_fraction=0.5, threshold=0.6, bootstrap_func=bootstrap_without_replacement,
bootstrap_threshold=None, verbose=0, n_jobs=1, pre_dispatch='2*n_jobs',
random_state=None):
self.base_estimator = base_estimator
self.lambda_name = lambda_name
self.lambda_grid = lambda_grid
self.n_bootstrap_iterations = n_bootstrap_iterations
self.sample_fraction = sample_fraction
self.threshold = threshold
self.bootstrap_func = bootstrap_func
self.bootstrap_threshold = bootstrap_threshold
self.verbose = verbose
self.n_jobs = n_jobs
self.pre_dispatch = pre_dispatch
self.random_state = random_state
示例12: run_logreg
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import LogisticRegression [as 别名]
def run_logreg(X_train, y_train, selection_threshold=0.2):
print("\nrunning logistic regression...")
print("using a selection threshold of {}".format(selection_threshold))
pipe = Pipeline(
[
(
"feature_selection",
RandomizedLogisticRegression(selection_threshold=selection_threshold),
),
("classification", LogisticRegression()),
]
)
pipe.fit(X_train, y_train)
print("training accuracy : {}".format(pipe.score(X_train, y_train)))
print("testing accuracy : {}".format(pipe.score(X_test, y_test)))
return pipe
示例13: test_experiment_sklearn_classifier
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import LogisticRegression [as 别名]
def test_experiment_sklearn_classifier(tmpdir_name):
X, y = make_classification_df(n_samples=1024, n_num_features=10, n_cat_features=0,
class_sep=0.98, random_state=0, id_column='user_id')
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=0)
params = {
'C': 0.1
}
result = run_experiment(params, X_train, y_train, X_test, tmpdir_name, eval_func=roc_auc_score,
algorithm_type=LogisticRegression, with_auto_prep=False)
assert len(np.unique(result.oof_prediction)) > 5 # making sure prediction is not binarized
assert len(np.unique(result.test_prediction)) > 5
assert roc_auc_score(y_train, result.oof_prediction) >= 0.8
assert roc_auc_score(y_test, result.test_prediction) >= 0.8
_check_file_exists(tmpdir_name)
示例14: test_regularization
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import LogisticRegression [as 别名]
def test_regularization():
"""Test for fitting the model."""
X = rnd.randn(10, 2)
y = np.hstack((-np.ones((5,)), np.ones((5,))))
Z = rnd.randn(10, 2) + 1
clf = ImportanceWeightedClassifier(loss_function='lr',
l2_regularization=None)
assert isinstance(clf.clf, LogisticRegressionCV)
clf = ImportanceWeightedClassifier(loss_function='lr',
l2_regularization=1.0)
assert isinstance(clf.clf, LogisticRegression)
示例15: test_not_none
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import LogisticRegression [as 别名]
def test_not_none(self):
self.assertIsNotNone(LogisticRegression)