sklearn学习笔记 三 集成方法AdaBoost
官方英文文档手册http://scikit-learn.org/stable/modules/generated/sklearn.ensemble.AdaBoostClassifier.html
sklearn.ensemble模块提供了很多集成方法,AdaBoost、Bagging、随机森林等。本文使用的是AdaBoostClassifier。
AdaBoostClassifier这个函数,一共有5个参数:
参数说明如下:
- base_estimator:可选参数,默认为DecisionTreeClassifier。理论上可以选择任何一个分类或者回归学习器,不过需要支持样本权重。我们常用的一般是CART决策树或者神经网络MLP。默认是决策树,即AdaBoostClassifier默认使用CART分类树DecisionTreeClassifier,而AdaBoostRegressor默认使用CART回归树DecisionTreeRegressor。另外有一个要注意的点是,如果我们选择的AdaBoostClassifier算法是SAMME.R,则我们的弱分类学习器还需要支持概率预测,也就是在scikit-learn中弱分类学习器对应的预测方法除了predict还需要有predict_proba。
- algorithm:可选参数,默认为SAMME.R。scikit-learn实现了两种Adaboost分类算法,SAMME和SAMME.R。两者的主要区别是弱学习器权重的度量,SAMME使用对样本集分类效果作为弱学习器权重,而SAMME.R使用了对样本集分类的预测概率大小来作为弱学习器权重。由于SAMME.R使用了概率度量的连续值,迭代一般比SAMME快,因此AdaBoostClassifier的默认算法algorithm的值也是SAMME.R。我们一般使用默认的SAMME.R就够了,但是要注意的是使用了SAMME.R, 则弱分类学习器参数base_estimator必须限制使用支持概率预测的分类器。SAMME算法则没有这个限制。
- n_estimators:整数型,可选参数,默认为50。弱学习器的最大迭代次数,或者说最大的弱学习器的个数。一般来说n_estimators太小,容易欠拟合,n_estimators太大,又容易过拟合,一般选择一个适中的数值。默认是50。在实际调参的过程中,我们常常将n_estimators和下面介绍的参数learning_rate一起考虑。
- learning_rate:浮点型,可选参数,默认为1.0。每个弱学习器的权重缩减系数,取值范围为0到1,对于同样的训练集拟合效果,较小的v意味着我们需要更多的弱学习器的迭代次数。通常我们用步长和迭代最大次数一起来决定算法的拟合效果。所以这两个参数n_estimators和learning_rate要一起调参。一般来说,可以从一个小一点的v开始调参,默认是1。
- random_state:整数型,可选参数,默认为None。如果RandomState的实例,random_state是随机数生成器; 如果None,则随机数生成器是由np.random使用的RandomState实例
Methods
decision_function (X) |
Compute the decision function of X . |
fit (X, y[, sample_weight]) |
Build a boosted classifier from the training set (X, y). |
get_params ([deep]) |
Get parameters for this estimator. |
predict (X) |
Predict classes for X. |
predict_log_proba (X) |
Predict class log-probabilities for X. |
predict_proba (X) |
Predict class probabilities for X. |
score (X, y[, sample_weight]) |
Returns the mean accuracy on the given test data and labels. |
set_params (**params) |
Set the parameters of this estimator. |
staged_decision_function (X) |
Compute decision function of X for each boosting iteration. |
staged_predict (X) |
Return staged predictions for X. |
staged_predict_proba (X) |
Predict class probabilities for X. |
staged_score (X, y[, sample_weight]) |
Return staged scores for X, y. |
decision_function
(X)-
Compute the decision function of
X
.Parameters: X : {array-like, sparse matrix} of shape = [n_samples, n_features]
The training input samples. Sparse matrix can be CSC, CSR, COO,DOK, or LIL. DOK and LIL are converted to CSR.
Returns: score : array, shape = [n_samples, k]
The decision function of the input samples. The order ofoutputs is the same of that of the classes_ attribute.Binary classification is a special cases with
k == 1
,otherwisek==n_classes
. For binary classification,values closer to -1 or 1 mean more like the first or secondclass inclasses_
, respectively.
计算X的决定函数。
参数:
X:shape = [n_samples,n_features]的{阵列状,稀疏矩阵}
训练输入样本。稀疏矩阵可以是CSC,CSR,COO,DOK或LIL。 DOK和LIL被转换为CSR。
返回:
score:array,shape = [n_samples,k]
输入样本的决策功能。输出的顺序与classes_属性的顺序相同。二元分类是k == 1的特殊情况,否则是k == n_classes。对于二元分类,接近-1或1的值分别意味着更类似于classes_中的第一个或第二个类。
(2)fit
(X, y, sample_weight=None)
-
Build a boosted classifier from the training set (X, y).
Parameters: X : {array-like, sparse matrix} of shape = [n_samples, n_features]
The training input samples. Sparse matrix can be CSC, CSR, COO,DOK, or LIL. DOK and LIL are converted to CSR.
y : array-like of shape = [n_samples]
The target values (class labels).
sample_weight : array-like of shape = [n_samples], optional
Sample weights. If None, the sample weights are initialized to
1 / n_samples
.Returns: self : object
Returns self.
从训练集(X,y)中构建一个助推分类器。
参数:
X:shape = [n_samples,n_features]的{阵列状,稀疏矩阵}
训练输入样本。稀疏矩阵可以是CSC,CSR,COO,DOK或LIL。 DOK和LIL被转换为CSR。
y:数组 - 像shape = [n_samples]
目标值(类标签)。
sample_weight:类似于shape = [n_samples]的数组,可选
样品重量。如果无,则样本权重将初始化为1 / n_samples。
返回:
自我:对象
返回自我。
(3) predict
(X)
-
Predict classes for X.
The predicted class of an input sample is computed as the weighted meanprediction of the classifiers in the ensemble.
Parameters: X : {array-like, sparse matrix} of shape = [n_samples, n_features]
The training input samples. Sparse matrix can be CSC, CSR, COO,DOK, or LIL. DOK and LIL are converted to CSR.
Returns: y : array of shape = [n_samples]
The predicted classes.
预测X的类。
输入样本的预测类别被计算为集合中分类器的加权平均预测。
参数:
X:shape = [n_samples,n_features]的{阵列状,稀疏矩阵}
训练输入样本。稀疏矩阵可以是CSC,CSR,COO,DOK或LIL。 DOK和LIL被转换为CSR。
返回:
y:形状数组= [n_samples]
预测的类。