本文整理匯總了Python中sklearn.ensemble.GradientBoostingClassifier方法的典型用法代碼示例。如果您正苦於以下問題:Python ensemble.GradientBoostingClassifier方法的具體用法?Python ensemble.GradientBoostingClassifier怎麽用?Python ensemble.GradientBoostingClassifier使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sklearn.ensemble
的用法示例。
在下文中一共展示了ensemble.GradientBoostingClassifier方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: search_cv
# 需要導入模塊: from sklearn import ensemble [as 別名]
# 或者: from sklearn.ensemble import GradientBoostingClassifier [as 別名]
def search_cv(x_train, y_train, x_test, y_test, model=GradientBoostingClassifier(n_estimators=30)):
# grid search找到最好的參數
parameters = {'kernel': ('linear', 'rbf'), 'C': [1, 2, 4], 'gamma': [0.125, 0.25, 0.5, 1, 2, 4]}
clf = GridSearchCV(model, param_grid=parameters)
grid_search = clf.fit(x_train, y_train)
# 對結果打分
print("Best score: %0.3f" % grid_search.best_score_)
print(grid_search.best_estimator_)
# best prarams
print('best prarams:', clf.best_params_)
print('-----grid search end------------')
print('on all train set')
scores = cross_val_score(grid_search.best_estimator_, x_train, y_train, cv=3, scoring='accuracy')
print(scores.mean(), scores)
print('on test set')
scores = cross_val_score(grid_search.best_estimator_, x_test, y_test, cv=3, scoring='accuracy')
print(scores.mean(), scores)
示例2: Train
# 需要導入模塊: from sklearn import ensemble [as 別名]
# 或者: from sklearn.ensemble import GradientBoostingClassifier [as 別名]
def Train(data, modelcount, censhu, yanzhgdata):
model = GradientBoostingClassifier(loss='deviance', n_estimators=modelcount, max_depth=censhu, learning_rate=0.1, max_features='sqrt')
model.fit(data[:, :-1], data[:, -1])
# 給出訓練數據的預測值
train_out = model.predict(data[:, :-1])
# 計算MSE
train_mse = fmse(data[:, -1], train_out)[0]
# 給出驗證數據的預測值
add_yan = model.predict(yanzhgdata[:, :-1])
# 計算f1度量
add_mse = fmse(yanzhgdata[:, -1], add_yan)[0]
print(train_mse, add_mse)
return train_mse, add_mse
# 最終確定組合的函數
示例3: recspre
# 需要導入模塊: from sklearn import ensemble [as 別名]
# 或者: from sklearn.ensemble import GradientBoostingClassifier [as 別名]
def recspre(estrs, predata, datadict, zhe):
mo, ze = estrs.split('-')
model = GradientBoostingClassifier(loss='deviance', n_estimators=int(mo), max_depth=int(ze), learning_rate=0.1)
model.fit(datadict[zhe]['train'][:, :-1], datadict[zhe]['train'][:, -1])
# 預測
yucede = model.predict(predata[:, :-1])
# 計算混淆矩陣
print(ConfuseMatrix(predata[:, -1], yucede))
return fmse(predata[:, -1], yucede)
# 主函數
示例4: check_classification_synthetic
# 需要導入模塊: from sklearn import ensemble [as 別名]
# 或者: from sklearn.ensemble import GradientBoostingClassifier [as 別名]
def check_classification_synthetic(presort, loss):
# Test GradientBoostingClassifier on synthetic dataset used by
# Hastie et al. in ESLII Example 12.7.
X, y = datasets.make_hastie_10_2(n_samples=12000, random_state=1)
X_train, X_test = X[:2000], X[2000:]
y_train, y_test = y[:2000], y[2000:]
gbrt = GradientBoostingClassifier(n_estimators=100, min_samples_split=2,
max_depth=1, loss=loss,
learning_rate=1.0, random_state=0)
gbrt.fit(X_train, y_train)
error_rate = (1.0 - gbrt.score(X_test, y_test))
assert_less(error_rate, 0.09)
gbrt = GradientBoostingClassifier(n_estimators=200, min_samples_split=2,
max_depth=1, loss=loss,
learning_rate=1.0, subsample=0.5,
random_state=0,
presort=presort)
gbrt.fit(X_train, y_train)
error_rate = (1.0 - gbrt.score(X_test, y_test))
assert_less(error_rate, 0.08)
示例5: test_probability_log
# 需要導入模塊: from sklearn import ensemble [as 別名]
# 或者: from sklearn.ensemble import GradientBoostingClassifier [as 別名]
def test_probability_log():
# Predict probabilities.
clf = GradientBoostingClassifier(n_estimators=100, random_state=1)
assert_raises(ValueError, clf.predict_proba, T)
clf.fit(X, y)
assert_array_equal(clf.predict(T), true_result)
# check if probabilities are in [0, 1].
y_proba = clf.predict_proba(T)
assert np.all(y_proba >= 0.0)
assert np.all(y_proba <= 1.0)
# derive predictions from probabilities
y_pred = clf.classes_.take(y_proba.argmax(axis=1), axis=0)
assert_array_equal(y_pred, true_result)
示例6: test_check_inputs_predict_stages
# 需要導入模塊: from sklearn import ensemble [as 別名]
# 或者: from sklearn.ensemble import GradientBoostingClassifier [as 別名]
def test_check_inputs_predict_stages():
# check that predict_stages through an error if the type of X is not
# supported
x, y = datasets.make_hastie_10_2(n_samples=100, random_state=1)
x_sparse_csc = csc_matrix(x)
clf = GradientBoostingClassifier(n_estimators=100, random_state=1)
clf.fit(x, y)
score = np.zeros((y.shape)).reshape(-1, 1)
assert_raise_message(ValueError,
"When X is a sparse matrix, a CSR format is expected",
predict_stages, clf.estimators_, x_sparse_csc,
clf.learning_rate, score)
x_fortran = np.asfortranarray(x)
assert_raise_message(ValueError,
"X should be C-ordered np.ndarray",
predict_stages, clf.estimators_, x_fortran,
clf.learning_rate, score)
示例7: test_serialization
# 需要導入模塊: from sklearn import ensemble [as 別名]
# 或者: from sklearn.ensemble import GradientBoostingClassifier [as 別名]
def test_serialization():
# Check model serialization.
clf = GradientBoostingClassifier(n_estimators=100, random_state=1)
clf.fit(X, y)
assert_array_equal(clf.predict(T), true_result)
assert_equal(100, len(clf.estimators_))
try:
import cPickle as pickle
except ImportError:
import pickle
serialized_clf = pickle.dumps(clf, protocol=pickle.HIGHEST_PROTOCOL)
clf = None
clf = pickle.loads(serialized_clf)
assert_array_equal(clf.predict(T), true_result)
assert_equal(100, len(clf.estimators_))
示例8: test_verbose_output
# 需要導入模塊: from sklearn import ensemble [as 別名]
# 或者: from sklearn.ensemble import GradientBoostingClassifier [as 別名]
def test_verbose_output():
# Check verbose=1 does not cause error.
from io import StringIO
import sys
old_stdout = sys.stdout
sys.stdout = StringIO()
clf = GradientBoostingClassifier(n_estimators=100, random_state=1,
verbose=1, subsample=0.8)
clf.fit(X, y)
verbose_output = sys.stdout
sys.stdout = old_stdout
# check output
verbose_output.seek(0)
header = verbose_output.readline().rstrip()
# with OOB
true_header = ' '.join(['%10s'] + ['%16s'] * 3) % (
'Iter', 'Train Loss', 'OOB Improve', 'Remaining Time')
assert_equal(true_header, header)
n_lines = sum(1 for l in verbose_output.readlines())
# one for 1-10 and then 9 for 20-100
assert_equal(10 + 9, n_lines)
示例9: test_more_verbose_output
# 需要導入模塊: from sklearn import ensemble [as 別名]
# 或者: from sklearn.ensemble import GradientBoostingClassifier [as 別名]
def test_more_verbose_output():
# Check verbose=2 does not cause error.
from io import StringIO
import sys
old_stdout = sys.stdout
sys.stdout = StringIO()
clf = GradientBoostingClassifier(n_estimators=100, random_state=1,
verbose=2)
clf.fit(X, y)
verbose_output = sys.stdout
sys.stdout = old_stdout
# check output
verbose_output.seek(0)
header = verbose_output.readline().rstrip()
# no OOB
true_header = ' '.join(['%10s'] + ['%16s'] * 2) % (
'Iter', 'Train Loss', 'Remaining Time')
assert_equal(true_header, header)
n_lines = sum(1 for l in verbose_output.readlines())
# 100 lines for n_estimators==100
assert_equal(100, n_lines)
示例10: test_zero_estimator_clf
# 需要導入模塊: from sklearn import ensemble [as 別名]
# 或者: from sklearn.ensemble import GradientBoostingClassifier [as 別名]
def test_zero_estimator_clf():
# Test if init='zero' works for classification.
X = iris.data
y = np.array(iris.target)
est = GradientBoostingClassifier(n_estimators=20, max_depth=1,
random_state=1, init='zero')
est.fit(X, y)
assert_greater(est.score(X, y), 0.96)
# binary clf
mask = y != 0
y[mask] = 1
y[~mask] = 0
est = GradientBoostingClassifier(n_estimators=20, max_depth=1,
random_state=1, init='zero')
est.fit(X, y)
assert_greater(est.score(X, y), 0.96)
est = GradientBoostingClassifier(n_estimators=20, max_depth=1,
random_state=1, init='foobar')
assert_raises(ValueError, est.fit, X, y)
示例11: test_probability_exponential
# 需要導入模塊: from sklearn import ensemble [as 別名]
# 或者: from sklearn.ensemble import GradientBoostingClassifier [as 別名]
def test_probability_exponential():
# Predict probabilities.
clf = GradientBoostingClassifier(loss='exponential',
n_estimators=100, random_state=1)
assert_raises(ValueError, clf.predict_proba, T)
clf.fit(X, y)
assert_array_equal(clf.predict(T), true_result)
# check if probabilities are in [0, 1].
y_proba = clf.predict_proba(T)
assert np.all(y_proba >= 0.0)
assert np.all(y_proba <= 1.0)
score = clf.decision_function(T).ravel()
assert_array_almost_equal(y_proba[:, 1], expit(2 * score))
# derive predictions from probabilities
y_pred = clf.classes_.take(y_proba.argmax(axis=1), axis=0)
assert_array_equal(y_pred, true_result)
示例12: test_early_stopping_n_classes
# 需要導入模塊: from sklearn import ensemble [as 別名]
# 或者: from sklearn.ensemble import GradientBoostingClassifier [as 別名]
def test_early_stopping_n_classes():
# when doing early stopping (_, , y_train, _ = train_test_split(X, y))
# there might be classes in y that are missing in y_train. As the init
# estimator will be trained on y_train, we need to raise an error if this
# happens.
X = [[1]] * 10
y = [0, 0] + [1] * 8 # only 2 negative class over 10 samples
gb = GradientBoostingClassifier(n_iter_no_change=5, random_state=0,
validation_fraction=8)
with pytest.raises(
ValueError,
match='The training data after the early stopping split'):
gb.fit(X, y)
# No error if we let training data be big enough
gb = GradientBoostingClassifier(n_iter_no_change=5, random_state=0,
validation_fraction=4)
示例13: test_recursion_decision_function
# 需要導入模塊: from sklearn import ensemble [as 別名]
# 或者: from sklearn.ensemble import GradientBoostingClassifier [as 別名]
def test_recursion_decision_function(target_feature):
# Make sure the recursion method (implicitly uses decision_function) has
# the same result as using brute method with
# response_method=decision_function
X, y = make_classification(n_classes=2, n_clusters_per_class=1,
random_state=1)
assert np.mean(y) == .5 # make sure the init estimator predicts 0 anyway
est = GradientBoostingClassifier(random_state=0, loss='deviance')
est.fit(X, y)
preds_1, _ = partial_dependence(est, X, [target_feature],
response_method='decision_function',
method='recursion')
preds_2, _ = partial_dependence(est, X, [target_feature],
response_method='decision_function',
method='brute')
assert_allclose(preds_1, preds_2, atol=1e-7)
示例14: test_warning_recursion_non_constant_init
# 需要導入模塊: from sklearn import ensemble [as 別名]
# 或者: from sklearn.ensemble import GradientBoostingClassifier [as 別名]
def test_warning_recursion_non_constant_init():
# make sure that passing a non-constant init parameter to a GBDT and using
# recursion method yields a warning.
gbc = GradientBoostingClassifier(init=DummyClassifier(), random_state=0)
gbc.fit(X, y)
with pytest.warns(
UserWarning,
match='Using recursion method with a non-constant init predictor'):
partial_dependence(gbc, X, [0], method='recursion')
with pytest.warns(
UserWarning,
match='Using recursion method with a non-constant init predictor'):
partial_dependence(gbc, X, [0], method='recursion')
示例15: setUpClass
# 需要導入模塊: from sklearn import ensemble [as 別名]
# 或者: from sklearn.ensemble import GradientBoostingClassifier [as 別名]
def setUpClass(self):
"""
Set up the unit test by loading the dataset and training a model.
"""
from sklearn.datasets import load_boston
import numpy as np
scikit_data = load_boston()
scikit_model = GradientBoostingClassifier(random_state=1)
t = scikit_data.target
target = np.digitize(t, np.histogram(t)[1]) - 1
scikit_model.fit(scikit_data.data, target)
self.target = target
# Save the data and the model
self.scikit_data = scikit_data
self.scikit_model = scikit_model