本文整理汇总了Python中sklearn.linear_model.logistic.LogisticRegression.fit方法的典型用法代码示例。如果您正苦于以下问题:Python LogisticRegression.fit方法的具体用法?Python LogisticRegression.fit怎么用?Python LogisticRegression.fit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.linear_model.logistic.LogisticRegression
的用法示例。
在下文中一共展示了LogisticRegression.fit方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_warm_start
# 需要导入模块: from sklearn.linear_model.logistic import LogisticRegression [as 别名]
# 或者: from sklearn.linear_model.logistic.LogisticRegression import fit [as 别名]
def test_warm_start(solver, warm_start, fit_intercept, multi_class):
# A 1-iteration second fit on same data should give almost same result
# with warm starting, and quite different result without warm starting.
# Warm starting does not work with liblinear solver.
X, y = iris.data, iris.target
clf = LogisticRegression(tol=1e-4, multi_class=multi_class,
warm_start=warm_start,
solver=solver,
random_state=42, max_iter=100,
fit_intercept=fit_intercept)
with ignore_warnings(category=ConvergenceWarning):
clf.fit(X, y)
coef_1 = clf.coef_
clf.max_iter = 1
clf.fit(X, y)
cum_diff = np.sum(np.abs(coef_1 - clf.coef_))
msg = ("Warm starting issue with %s solver in %s mode "
"with fit_intercept=%s and warm_start=%s"
% (solver, multi_class, str(fit_intercept),
str(warm_start)))
if warm_start:
assert_greater(2.0, cum_diff, msg)
else:
assert_greater(cum_diff, 2.0, msg)
示例2: main
# 需要导入模块: from sklearn.linear_model.logistic import LogisticRegression [as 别名]
# 或者: from sklearn.linear_model.logistic.LogisticRegression import fit [as 别名]
def main():
classes = [
'chimp',
'corvette',
'tokyo',
'goldengatebridge'
]
images, labels = get_labels(classes)
std_features = get_standard_features(images)
k = 256
surf_features = get_visual_words(images, k)
tas_features = get_tas_features(images)
feature_dict = {
'Std': std_features,
'SURF': surf_features,
'TAS': tas_features
#'Zernike': zernike_features
}
best_features = log_classify(feature_dict, labels)
classifier = LogisticRegression()
classifier.fit(best_features, labels)
示例3: __init__
# 需要导入模块: from sklearn.linear_model.logistic import LogisticRegression [as 别名]
# 或者: from sklearn.linear_model.logistic.LogisticRegression import fit [as 别名]
class LogReg:
def __init__(self):
self.load_data()
self.clf = LogisticRegression(class_weight = 'balanced')
self.train()
self.predict()
def load_data(self):
train_csv = './data/train.csv'
test_csv = './data/test.csv'
df_train = pd.read_csv(train_csv, header=0)
df_test = pd.read_csv(test_csv, header=0)
arr_train = df_train.values
arr_test = df_test.values
self.train_X = arr_train[0::,1::]
self.train_Y = arr_train[0::, 0]
self.test_X = arr_test[0::, 1::]
self.test_ID = arr_test[0::,0]
def train(self):
self.clf.fit(self.train_X, self.train_Y)
def predict(self):
self.test_Y = self.clf.predict_proba(self.test_X)
def get_training_accuracy(self):
return (self.clf.score(self.train_X, self.train_Y))
def store_result(self):
df_out = pd.DataFrame()
df_out['Id'] = self.test_ID
df_out['Action'] = self.test_Y[0::,1]
df_out.to_csv('./data/results/c1_result.csv',index=False)
示例4: test_write_parameters
# 需要导入模块: from sklearn.linear_model.logistic import LogisticRegression [as 别名]
# 或者: from sklearn.linear_model.logistic.LogisticRegression import fit [as 别名]
def test_write_parameters():
# Test that we can write to coef_ and intercept_
clf = LogisticRegression(random_state=0)
clf.fit(X, Y1)
clf.coef_[:] = 0
clf.intercept_[:] = 0
assert_array_almost_equal(clf.decision_function(X), 0)
示例5: test_regularization_path
# 需要导入模块: from sklearn.linear_model.logistic import LogisticRegression [as 别名]
# 或者: from sklearn.linear_model.logistic.LogisticRegression import fit [as 别名]
def test_regularization_path(self):
# Check results using logistic path
num_samples = 10
num_feat = 5
X, y = make_classification(n_samples=num_samples, n_features=num_feat, n_informative=3,
n_classes=2, random_state=0, weights=[0.5, 0.5])
matrix = np.zeros((num_samples, num_feat + 2))
matrix[:,:-2] = X
matrix[:, -2] = np.ones(num_samples)
matrix[:, -1] = y
# Betas to test
logitfitL1 = LogisticRegressionL1()
lambda_grid = np.exp(-1 * np.linspace(1, 17, 200))
path = logitfitL1.fit(matrix, lambda_grid)
# Sklearn
cs = l1_min_c(X, y, loss='log') * np.logspace(0, 3)
# Computing regularization path using sklearn
clf = LogisticRegression(C=1.0, penalty='l1', tol=1e-6)
coefs_ = []
for c in cs:
clf.set_params(C=c)
clf.fit(X, y)
coefs_.append(clf.coef_.ravel().copy())
skbetas = np.append(clf.intercept_[0], clf.coef_)
np.testing.assert_almost_equal(skbetas, logitfitL1.coef_, 1)
示例6: predictWithThreshold
# 需要导入模块: from sklearn.linear_model.logistic import LogisticRegression [as 别名]
# 或者: from sklearn.linear_model.logistic.LogisticRegression import fit [as 别名]
def predictWithThreshold(datadir, threshold, penalty_type='l2'):
maxent = LogisticRegression(penalty=penalty_type)
scores = defaultdict(list)
for dir in sorted(os.listdir(datadir), reverse=True):
trainfeatures, trainlabels, vec = feats_and_classify.collect_features(datadir+dir+'/train.conll')
TrainIndices=np.array(range(len(trainfeatures)))
features, labels, vec = feats_and_classify.collect_features(datadir+dir+'/all.conll')
TestIndices=np.array(range(len(trainfeatures),len(features)))
# print('\r'+dir, end="")
# print(dir)
TrainX_i = features[TrainIndices]
Trainy_i = labels[TrainIndices]
TestX_i = features[TestIndices]
Testy_i = labels[TestIndices]
maxent.fit(TrainX_i,Trainy_i)
# print('Finished fitting')
ypred_i, score=pred_for_threshold(maxent,TestX_i,Testy_i, threshold)
# print('Predicting')
scores["F1"].append(score[0])
scores["Recall"].append(score[1])
scores["Accuracy"].append(score[2])
scores["Precision"].append(score[3])
#scores = cross_validation.cross_val_score(maxent, features, labels, cv=10)
print("\n--")
for key in sorted(scores.keys()):
currentmetric = np.array(scores[key])
print("%s : %0.2f (+/- %0.2f)" % (key,currentmetric.mean(), currentmetric.std()))
print("--")
示例7: mlogistic
# 需要导入模块: from sklearn.linear_model.logistic import LogisticRegression [as 别名]
# 或者: from sklearn.linear_model.logistic.LogisticRegression import fit [as 别名]
def mlogistic():
X = []
# 前三行作为输入样本
X.append("fuck you")
X.append("fuck you all")
X.append("hello everyone")
# 后两句作为测试样本
X.append("fuck me")
X.append("hello boy")
# y为样本标注
y = [1,1,0]
vectorizer = TfidfVectorizer()
# 取X的前三句作为输入做tfidf转换
X_train = vectorizer.fit_transform(X[:-2])
print X_train
# 取X的后两句用“上句生成”的tfidf做转换
X_test = vectorizer.transform(X[-2:])
print X_test
# 用逻辑回归模型做训练
classifier = LogisticRegression()
classifier.fit(X_train, y)
# 做测试样例的预测
predictions = classifier.predict(X_test)
print predictions
示例8: test_warm_start
# 需要导入模块: from sklearn.linear_model.logistic import LogisticRegression [as 别名]
# 或者: from sklearn.linear_model.logistic.LogisticRegression import fit [as 别名]
def test_warm_start():
# A 1-iteration second fit on same data should give almost same result
# with warm starting, and quite different result without warm starting.
# Warm starting does not work with liblinear solver.
X, y = iris.data, iris.target
solvers = ['newton-cg', 'sag']
# old scipy doesn't have maxiter
if sp_version >= (0, 12):
solvers.append('lbfgs')
for warm_start in [True, False]:
for fit_intercept in [True, False]:
for solver in solvers:
for multi_class in ['ovr', 'multinomial']:
clf = LogisticRegression(tol=1e-4, multi_class=multi_class,
warm_start=warm_start,
solver=solver,
random_state=42, max_iter=100,
fit_intercept=fit_intercept)
clf.fit(X, y)
coef_1 = clf.coef_
clf.max_iter = 1
with ignore_warnings():
clf.fit(X, y)
cum_diff = np.sum(np.abs(coef_1 - clf.coef_))
msg = ("Warm starting issue with %s solver in %s mode "
"with fit_intercept=%s and warm_start=%s"
% (solver, multi_class, str(fit_intercept),
str(warm_start)))
if warm_start:
assert_greater(2.0, cum_diff, msg)
else:
assert_greater(cum_diff, 2.0, msg)
示例9: test_logreg_l1
# 需要导入模块: from sklearn.linear_model.logistic import LogisticRegression [as 别名]
# 或者: from sklearn.linear_model.logistic.LogisticRegression import fit [as 别名]
def test_logreg_l1():
# Because liblinear penalizes the intercept and saga does not, we do not
# fit the intercept to make it possible to compare the coefficients of
# the two models at convergence.
rng = np.random.RandomState(42)
n_samples = 50
X, y = make_classification(n_samples=n_samples, n_features=20,
random_state=0)
X_noise = rng.normal(size=(n_samples, 3))
X_constant = np.ones(shape=(n_samples, 2))
X = np.concatenate((X, X_noise, X_constant), axis=1)
lr_liblinear = LogisticRegression(penalty="l1", C=1.0, solver='liblinear',
fit_intercept=False,
tol=1e-10)
lr_liblinear.fit(X, y)
lr_saga = LogisticRegression(penalty="l1", C=1.0, solver='saga',
fit_intercept=False,
max_iter=1000, tol=1e-10)
lr_saga.fit(X, y)
assert_array_almost_equal(lr_saga.coef_, lr_liblinear.coef_)
# Noise and constant features should be regularized to zero by the l1
# penalty
assert_array_almost_equal(lr_liblinear.coef_[0, -5:], np.zeros(5))
assert_array_almost_equal(lr_saga.coef_[0, -5:], np.zeros(5))
示例10: test_logistic_regression_solvers_multiclass
# 需要导入模块: from sklearn.linear_model.logistic import LogisticRegression [as 别名]
# 或者: from sklearn.linear_model.logistic.LogisticRegression import fit [as 别名]
def test_logistic_regression_solvers_multiclass():
X, y = make_classification(n_samples=20, n_features=20, n_informative=10,
n_classes=3, random_state=0)
tol = 1e-7
ncg = LogisticRegression(solver='newton-cg', fit_intercept=False, tol=tol)
lbf = LogisticRegression(solver='lbfgs', fit_intercept=False, tol=tol)
lib = LogisticRegression(fit_intercept=False, tol=tol)
sag = LogisticRegression(solver='sag', fit_intercept=False, tol=tol,
max_iter=1000, random_state=42)
saga = LogisticRegression(solver='saga', fit_intercept=False, tol=tol,
max_iter=10000, random_state=42)
ncg.fit(X, y)
lbf.fit(X, y)
sag.fit(X, y)
saga.fit(X, y)
lib.fit(X, y)
assert_array_almost_equal(ncg.coef_, lib.coef_, decimal=4)
assert_array_almost_equal(lib.coef_, lbf.coef_, decimal=4)
assert_array_almost_equal(ncg.coef_, lbf.coef_, decimal=4)
assert_array_almost_equal(sag.coef_, lib.coef_, decimal=4)
assert_array_almost_equal(sag.coef_, ncg.coef_, decimal=4)
assert_array_almost_equal(sag.coef_, lbf.coef_, decimal=4)
assert_array_almost_equal(saga.coef_, sag.coef_, decimal=4)
assert_array_almost_equal(saga.coef_, lbf.coef_, decimal=4)
assert_array_almost_equal(saga.coef_, ncg.coef_, decimal=4)
assert_array_almost_equal(saga.coef_, lib.coef_, decimal=4)
示例11: test_consistency_path
# 需要导入模块: from sklearn.linear_model.logistic import LogisticRegression [as 别名]
# 或者: from sklearn.linear_model.logistic.LogisticRegression import fit [as 别名]
def test_consistency_path():
# Test that the path algorithm is consistent
rng = np.random.RandomState(0)
X = np.concatenate((rng.randn(100, 2) + [1, 1], rng.randn(100, 2)))
y = [1] * 100 + [-1] * 100
Cs = np.logspace(0, 4, 10)
f = ignore_warnings
# can't test with fit_intercept=True since LIBLINEAR
# penalizes the intercept
for solver in ("lbfgs", "newton-cg", "liblinear", "sag"):
coefs, Cs, _ = f(logistic_regression_path)(
X, y, Cs=Cs, fit_intercept=False, tol=1e-5, solver=solver, random_state=0
)
for i, C in enumerate(Cs):
lr = LogisticRegression(C=C, fit_intercept=False, tol=1e-5, random_state=0)
lr.fit(X, y)
lr_coef = lr.coef_.ravel()
assert_array_almost_equal(lr_coef, coefs[i], decimal=4, err_msg="with solver = %s" % solver)
# test for fit_intercept=True
for solver in ("lbfgs", "newton-cg", "liblinear", "sag"):
Cs = [1e3]
coefs, Cs, _ = f(logistic_regression_path)(
X, y, Cs=Cs, fit_intercept=True, tol=1e-6, solver=solver, intercept_scaling=10000.0, random_state=0
)
lr = LogisticRegression(C=Cs[0], fit_intercept=True, tol=1e-4, intercept_scaling=10000.0, random_state=0)
lr.fit(X, y)
lr_coef = np.concatenate([lr.coef_.ravel(), lr.intercept_])
assert_array_almost_equal(lr_coef, coefs[0], decimal=4, err_msg="with solver = %s" % solver)
示例12: cvWithThreshold
# 需要导入模块: from sklearn.linear_model.logistic import LogisticRegression [as 别名]
# 或者: from sklearn.linear_model.logistic.LogisticRegression import fit [as 别名]
def cvWithThreshold(X, y_current_tr, y_current_te, threshold, regularization='l2'):
out_dict = {}
scores = defaultdict(list)
fold=1
maxent = LogisticRegression(penalty=regularization)
for TrainIndices, TestIndices in cross_validation.StratifiedKFold(y_current_tr, n_folds=10, shuffle=False, random_state=None):
print('\r'+str(fold), end="")
fold+=1
TrainX_i = X[TrainIndices]
Trainy_i = y_current_tr[TrainIndices]
TestX_i = X[TestIndices]
Testy_i = y_current_te[TestIndices]
maxent.fit(TrainX_i,Trainy_i)
ypred_i, score=pred_for_threshold(maxent,TestX_i,Testy_i, threshold)
scores["F1"].append(score[0])
scores["Recall"].append(score[1])
scores["Accuracy"].append(score[2])
scores["Precision"].append(score[3])
#scores = cross_validation.cross_val_score(maxent, features, labels, cv=10)
print("\n--")
for key in sorted(scores.keys()):
currentmetric = np.array(scores[key])
out_dict[key] = (currentmetric.mean(),currentmetric.std())
print("%s : %0.2f (+/- %0.2f)" % (key,currentmetric.mean(), currentmetric.std()))
print("--")
return out_dict
示例13: main
# 需要导入模块: from sklearn.linear_model.logistic import LogisticRegression [as 别名]
# 或者: from sklearn.linear_model.logistic.LogisticRegression import fit [as 别名]
def main():
scriptdir = os.path.dirname(os.path.realpath(__file__))
parser = argparse.ArgumentParser(description="Skeleton for features and classifier for CWI-2016--optimisation of threshhold")
parser.add_argument('--threshold',type=float,default=0.5)
parser.add_argument('--annotator',type=str,default="03")
parser.add_argument('--penalty',type=str,choices=["l1","l2"],default="l1")
args = parser.parse_args()
current_single_ann = scriptdir+"/../data/cwi_training/cwi_training_"+args.annotator+".lbl.conll"
testfile = scriptdir+"/../data/cwi_testing/cwi_testing.txt.lbl.conll"
X__dict_train, y_train, v_train = feats_and_classify.collect_features(current_single_ann,vectorize=False)
X_dict_test, y_test, v_test = feats_and_classify.collect_features(testfile,vectorize=False)
featdicts = list([x for x in X__dict_train + X_dict_test])
vect = DictVectorizer()
X = vect.fit_transform(featdicts).toarray()
X_train=X[:len(y_train)]
X_test=X[len(y_train):]
maxent = LogisticRegression(penalty=args.penalty)
maxent.fit(X_train,y_train)
y_pred_proba = maxent.predict_proba(X_test)
ypred_i=["1" if pair[1]>=args.threshold else "0" for pair in y_pred_proba]
fout = open(args.annotator+".pred",mode="w")
print("\n".join(ypred_i),file=fout)
fout.close()
sys.exit(0)
示例14: test_liblinear_random_state
# 需要导入模块: from sklearn.linear_model.logistic import LogisticRegression [as 别名]
# 或者: from sklearn.linear_model.logistic.LogisticRegression import fit [as 别名]
def test_liblinear_random_state():
X, y = make_classification(n_samples=20)
lr1 = LogisticRegression(random_state=0)
lr1.fit(X, y)
lr2 = LogisticRegression(random_state=0)
lr2.fit(X, y)
assert_array_almost_equal(lr1.coef_, lr2.coef_)
示例15: test_logistic_regression_solvers
# 需要导入模块: from sklearn.linear_model.logistic import LogisticRegression [as 别名]
# 或者: from sklearn.linear_model.logistic.LogisticRegression import fit [as 别名]
def test_logistic_regression_solvers():
X, y = make_classification(n_features=10, n_informative=5, random_state=0)
ncg = LogisticRegression(solver='newton-cg', fit_intercept=False)
lbf = LogisticRegression(solver='lbfgs', fit_intercept=False)
lib = LogisticRegression(fit_intercept=False)
sag = LogisticRegression(solver='sag', fit_intercept=False,
random_state=42)
saga = LogisticRegression(solver='saga', fit_intercept=False,
random_state=42)
ncg.fit(X, y)
lbf.fit(X, y)
sag.fit(X, y)
saga.fit(X, y)
lib.fit(X, y)
assert_array_almost_equal(ncg.coef_, lib.coef_, decimal=3)
assert_array_almost_equal(lib.coef_, lbf.coef_, decimal=3)
assert_array_almost_equal(ncg.coef_, lbf.coef_, decimal=3)
assert_array_almost_equal(sag.coef_, lib.coef_, decimal=3)
assert_array_almost_equal(sag.coef_, ncg.coef_, decimal=3)
assert_array_almost_equal(sag.coef_, lbf.coef_, decimal=3)
assert_array_almost_equal(saga.coef_, sag.coef_, decimal=3)
assert_array_almost_equal(saga.coef_, lbf.coef_, decimal=3)
assert_array_almost_equal(saga.coef_, ncg.coef_, decimal=3)
assert_array_almost_equal(saga.coef_, lib.coef_, decimal=3)