标签:
Gradient Boosting for regression.
GB builds an additive model in a forward stage-wise fashion; it allows for the optimization of arbitrary differentiable loss functions. In each stage a regression tree is fit on the negative gradient of the given loss function.
Read more in the User Guide.
P a r a m e t e r s : |
loss : {‘ls’, ‘lad’, ‘huber’, ‘quantile’}, optional (default=’ls’)
learning_rate : float, optional (default=0.1)
n_estimators : int (default=100)
max_depth : integer, optional (default=3)
min_samples_split : integer, optional (default=2)
min_samples_leaf : integer, optional (default=1)
min_weight_fraction_leaf : float, optional (default=0.)
subsample : float, optional (default=1.0)
max_features : int, float, string or None, optional (default=None)
max_leaf_nodes : int or None, optional (default=None)
alpha : float (default=0.9)
init : BaseEstimator, None, optional (default=None)
verbose : int, default: 0
warm_start : bool, default: False
random_state : int, RandomState instance or None, optional (default=None)
|
---|---|
A t t r i b u t e s : |
feature_importances_ : array, shape = [n_features]
oob_improvement_ : array, shape = [n_estimators]
train_score_ : array, shape = [n_estimators]
loss_ : LossFunction
`init` : BaseEstimator
estimators_ : ndarray of DecisionTreeRegressor, shape = [n_estimators, 1]
|
See also
DecisionTreeRegressor, RandomForestRegressor
References
J. Friedman, Greedy Function Approximation: A Gradient Boosting Machine, The Annals of Statistics, Vol. 29, No. 5, 2001.
T. Hastie, R. Tibshirani and J. Friedman. Elements of Statistical Learning Ed. 2, Springer, 2009.
Methods
decision_function(*args, **kwargs) | DEPRECATED: and will be removed in 0.19 |
fit(X, y[, sample_weight, monitor]) | Fit the gradient boosting model. |
fit_transform(X[, y]) | Fit to data, then transform it. |
get_params([deep]) | Get parameters for this estimator. |
predict(X) | Predict regression target for X. |
score(X, y[, sample_weight]) | Returns the coefficient of determination R^2 of the prediction. |
set_params(**params) | Set the parameters of this estimator. |
staged_decision_function(*args, **kwargs) | DEPRECATED: and will be removed in 0.19 |
staged_predict(X) | Predict regression target at each stage for X. |
transform(X[, threshold]) | Reduce X to its most important features. |
DEPRECATED: and will be removed in 0.19
Compute the decision function of X.
Parameters: |
X : array-like of shape = [n_samples, n_features]
|
---|---|
Returns: |
score : array, shape = [n_samples, n_classes] or [n_samples]
|
Returns: | feature_importances_ : array, shape = [n_features] |
---|
Fit the gradient boosting model.
P a r a m e t e r s: |
X : array-like, shape = [n_samples, n_features]
y : array-like, shape = [n_samples]
sample_weight : array-like, shape = [n_samples] or None
monitor : callable, optional
|
---|---|
R e t u r n s: |
self : object
|
Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.
Parameters: |
X : numpy array of shape [n_samples, n_features]
y : numpy array of shape [n_samples]
|
---|---|
Returns: |
X_new : numpy array of shape [n_samples, n_features_new]
|
Get parameters for this estimator.
Parameters: |
deep: boolean, optional :
|
---|---|
Returns: |
params : mapping of string to any
|
Predict regression target for X.
Parameters: |
X : array-like of shape = [n_samples, n_features]
|
---|---|
Returns: |
y : array of shape = [n_samples]
|
Returns the coefficient of determination R^2 of the prediction.
The coefficient R^2 is defined as (1 - u/v), where u is the regression sum of squares ((y_true - y_pred) ** 2).sum() and v is the residual sum of squares ((y_true - y_true.mean()) ** 2).sum(). Best possible score is 1.0, lower values are worse.
Parameters: |
X : array-like, shape = (n_samples, n_features)
y : array-like, shape = (n_samples) or (n_samples, n_outputs)
sample_weight : array-like, shape = [n_samples], optional
|
---|---|
Returns: |
score : float
|
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as pipelines). The former have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Returns: | self : |
---|
DEPRECATED: and will be removed in 0.19
Compute decision function of X for each iteration.
This method allows monitoring (i.e. determine error on testing set) after each stage.
Parameters: |
X : array-like of shape = [n_samples, n_features]
|
---|---|
Returns: |
score : generator of array, shape = [n_samples, k]
|
Predict regression target at each stage for X.
This method allows monitoring (i.e. determine error on testing set) after each stage.
Parameters: |
X : array-like of shape = [n_samples, n_features]
|
---|---|
Returns: |
y : generator of array of shape = [n_samples]
|
Reduce X to its most important features.
Uses coef_ or feature_importances_ to determine the most important features. For models with a coef_ for each class, the absolute sum over the classes is used.
Parameters: |
X : array or scipy sparse matrix of shape [n_samples, n_features]
threshold : string, float or None, optional (default=None)
|
---|---|
Returns: |
X_r : array of shape [n_samples, n_selected_features]
|
Demonstrate Gradient Boosting on the Boston housing dataset.
This example fits a Gradient Boosting model with least squares loss and 500 regression trees of depth 4.
print(__doc__) # Author: Peter Prettenhofer <peter.prettenhofer@gmail.com> # # License: BSD 3 clause import numpy as np import matplotlib.pyplot as plt from sklearn import ensemble from sklearn import datasets from sklearn.utils import shuffle from sklearn.metrics import mean_squared_error ############################################################################### # Load data boston = datasets.load_boston() X, y = shuffle(boston.data, boston.target, random_state=13) X = X.astype(np.float32) offset = int(X.shape[0] * 0.9) X_train, y_train = X[:offset], y[:offset] X_test, y_test = X[offset:], y[offset:] ############################################################################### # Fit regression model params = {‘n_estimators‘: 500, ‘max_depth‘: 4, ‘min_samples_split‘: 1, ‘learning_rate‘: 0.01, ‘loss‘: ‘ls‘} clf = ensemble.GradientBoostingRegressor(**params) clf.fit(X_train, y_train) mse = mean_squared_error(y_test, clf.predict(X_test)) print("MSE: %.4f" % mse) ############################################################################### # Plot training deviance # compute test set deviance test_score = np.zeros((params[‘n_estimators‘],), dtype=np.float64) for i, y_pred in enumerate(clf.staged_decision_function(X_test)): test_score[i] = clf.loss_(y_test, y_pred) plt.figure(figsize=(12, 6)) plt.subplot(1, 2, 1) plt.title(‘Deviance‘) plt.plot(np.arange(params[‘n_estimators‘]) + 1, clf.train_score_, ‘b-‘, label=‘Training Set Deviance‘) plt.plot(np.arange(params[‘n_estimators‘]) + 1, test_score, ‘r-‘, label=‘Test Set Deviance‘) plt.legend(loc=‘upper right‘) plt.xlabel(‘Boosting Iterations‘) plt.ylabel(‘Deviance‘) ############################################################################### # Plot feature importance feature_importance = clf.feature_importances_ # make importances relative to max importance feature_importance = 100.0 * (feature_importance / feature_importance.max()) sorted_idx = np.argsort(feature_importance) pos = np.arange(sorted_idx.shape[0]) + .5 plt.subplot(1, 2, 2) plt.barh(pos, feature_importance[sorted_idx], align=‘center‘) plt.yticks(pos, boston.feature_names[sorted_idx]) plt.xlabel(‘Relative Importance‘) plt.title(‘Variable Importance‘) plt.show()
标签:
原文地址:http://www.cnblogs.com/chaofn/p/4681394.html