详解 Scikit-learn 的 ensemble.GradientBoostingClassifier函数:梯度提升分类器

  • Post category:Python

sklearn.ensemble.GradientBoostingClassifier函数是Scikit-learn库中的一个集成算法模型,实现了一种叫做梯度提升(Gradient Boosting)的算法,用于解决分类问题。该函数通过一系列决策树模型的加权组合来进行预测,并在每一步迭代中对前一步的拟合结果进行修正。以下是该函数的详细使用指南:

函数参数

sklearn.ensemble.GradientBoostingClassifier(loss='deviance', learning_rate=0.1, n_estimators=100, subsample=1.0, criterion='friedman_mse', min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_depth=3, min_impurity_decrease=0.0, min_impurity_split=None, init=None, random_state=None, max_features=None, verbose=0, max_leaf_nodes=None, warm_start=False, presort='deprecated', validation_fraction=0.1, n_iter_no_change=None, tol=0.0001, ccp_alpha=0.0)

其中,常用的参数包括:

  • loss: 指定损失函数的类型,可以是’deviance'(默认)或’exponential’
  • learning_rate: 单个决策树的权重缩小率,默认为0.1,不建议设置得过小。
  • n_estimators: 决策树的数量(迭代次数),默认为100,建议适当调整。
  • max_depth: 决策树的最大深度,默认为3,建议根据数据量和特征数量适当调节。
  • min_samples_split: 节点分裂所需的最小样本数,默认为2,建议适当调节。

示例

下面我们用两个具体的实例来演示如何使用sklearn.ensemble.GradientBoostingClassifier函数。

实例1:泰坦尼克号幸存者预测

我们用Titanic数据集中的幸存者预测任务作为例子来演示如何使用GradientBoostingClassifier函数。首先导入数据并进行预处理:

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder

# 读取数据
data = pd.read_csv('titanic.csv')

# 去除不必要的列
data.drop(['PassengerId', 'Name', 'Ticket', 'Cabin'], axis=1, inplace=True)

# 去除含有缺失值的样本
data.dropna(inplace=True)

# 编码字符串属性
for col in ['Sex', 'Embarked']:
    encoder = LabelEncoder()
    data[col] = encoder.fit_transform(data[col])

# 分离X和y
X = data.drop('Survived', axis=1)
y = data['Survived']

# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

接下来我们创建GradientBoostingClassifier对象,并将其拟合到训练数据上:

from sklearn.ensemble import GradientBoostingClassifier

# 创建分类器并训练
gbc = GradientBoostingClassifier(n_estimators=100, max_depth=3, random_state=42)
gbc.fit(X_train, y_train)

最后我们可以用训练好的分类器来预测:

# 预测
y_pred = gbc.predict(X_test)
实例2:Iris数据集分类

我们用Iris数据集中的花卉分类任务作为例子来演示如何使用GradientBoostingClassifier函数。首先导入数据并进行预处理:

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

# 导入数据
iris = load_iris()

# 标准化
X = StandardScaler().fit_transform(iris.data)

# 分离X和y
y = iris.target

# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

接下来我们创建GradientBoostingClassifier对象,并将其拟合到训练数据上:

from sklearn.ensemble import GradientBoostingClassifier

# 创建分类器并训练
gbc = GradientBoostingClassifier(n_estimators=100, max_depth=3, random_state=42)
gbc.fit(X_train, y_train)

最后我们可以用训练好的分类器来预测:

# 预测
y_pred = gbc.predict(X_test)

以上就是使用sklearn.ensemble.GradientBoostingClassifier函数的完整攻略与两个实例的具体操作。