机器学习中的 Random Forest 算法
Random Forest 是一种机器学习算法,它使用决策树集成来做出预测。该算法最早由 Leo Breiman 在 2001 年提出。该算法的核心思想是创建大量决策树,每棵树都在数据的一个不同子集上进行训练。然后,将这些单个树的预测结果组合起来,产生最终预测。
Random Forest 算法的工作原理
我们可以通过以下步骤来理解 Random Forest 算法的工作原理 —
步骤 1 − 首先,从给定的数据集选择随机样本。
步骤 2 − 接下来,该算法为每个样本构建一棵决策树。然后从每棵决策树获取预测结果。
步骤 3 − 在这一步,对每个预测结果进行投票。
步骤 4 − 最后,选择得票最多的预测结果作为最终预测结果。
下图展示了 Random Forest 算法的工作原理 —
Random Forest 是一种灵活的算法,可用于分类和回归任务。在分类任务中,该算法使用单个树预测结果的众数来做出最终预测。在回归任务中,该算法使用单个树预测结果的均值。
Random Forest 算法的优势
Random Forest 算法相较于其他机器学习算法具有多项优势。一些关键优势包括 —
对过拟合的鲁棒性 − Random Forest 算法以其对过拟合的鲁棒性而闻名。这是因为该算法使用决策树集成,有助于减少数据中异常值和噪声的影响。
高准确性 − Random Forest 算法以其高准确性而闻名。这是因为该算法结合了多个决策树的预测结果,有助于减少单个决策树可能存在的偏差或不准确的影响。
处理缺失数据 − Random Forest 算法可以在无需插补的情况下处理缺失数据。这是因为该算法仅考虑每个数据点可用的特征,并不要求所有数据点都具备所有特征。
非线性关系 − Random Forest 算法能够处理特征与目标变量之间的非线性关系。这是因为该算法使用决策树,后者能够建模非线性关系。
特征重要性 − Random Forest 算法可以提供模型中每个特征的重要性信息。此信息可用于识别数据中最重要特征,并可用于特征选择和特征工程。
Python 中 Random Forest 算法的实现
让我们来看看 Python 中 Random Forest 算法的实现。我们将使用 scikit-learn 库来实现该算法。scikit-learn 库是一个流行的机器学习库,提供了广泛的机器学习算法和工具。
步骤 1 − 导入库
我们将首先导入必要的库。我们将使用 pandas 库进行数据操作,以及 scikit-learn 库来实现 Random Forest 算法。
import pandas as pd from sklearn.ensemble import RandomForestClassifier
步骤 2 − 加载数据
接下来,我们将数据加载到 pandas dataframe 中。在本教程中,我们将使用著名的 Iris 数据集,这是一个经典的分类任务数据集。
# 加载 iris 数据集
iris = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learningdatabases/iris/iris.data', header=None)
iris.columns = ['sepal_length', 'sepal_width', 'petal_length','petal_width', 'species']
步骤 3 − 数据预处理
在用数据训练模型之前,我们需要对其进行预处理。这包括分离特征和目标变量,并将数据分割为训练集和测试集。
# 分离特征和目标变量 X = iris.iloc[:, :-1] y = iris.iloc[:, -1] # 将数据分割为训练集和测试集 from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.35, random_state=42)
步骤 4 − 训练模型
接下来,我们将在训练数据上训练 Random Forest 分类器。
# 创建 Random Forest 分类器对象 rfc = RandomForestClassifier(n_estimators=100) # 在训练数据上训练模型 rfc.fit(X_train, y_train)
步骤 5 − 进行预测
训练好模型后,我们可以使用它对测试数据进行预测。
# 对测试数据进行预测 y_pred = rfc.predict(X_test)
步骤 6 − 评估模型
最后,我们将使用各种指标如 accuracy、precision、recall 和 F1-score 来评估模型的性能。
# 导入 metrics 库
from sklearn.metrics import accuracy_score, precision_score,
recall_score, f1_score
# 计算 accuracy、precision、recall 和 F1-score
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred, average='weighted')
recall = recall_score(y_test, y_pred, average='weighted')
f1 = f1_score(y_test, y_pred, average='weighted')
print("Accuracy:", accuracy)
print("Precision:", precision)
print("Recall:", recall)
print("F1-score:", f1)
完整实现示例
下面是使用 iris 数据集在 Python 中 Random Forest 算法的完整实现示例 −
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
# 加载 iris 数据集
iris = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learningdatabases/iris/iris.data', header=None)
iris.columns = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species']
# 分离特征和目标变量
X = iris.iloc[:, :-1]
y = iris.iloc[:, -1]
# 将数据分割为训练集和测试集
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.35, random_state=42)
# 创建 Random Forest 分类器对象
rfc = RandomForestClassifier(n_estimators=100)
# 在训练数据上训练模型
rfc.fit(X_train, y_train)
# 对测试数据进行预测
y_pred = rfc.predict(X_test)
# 导入 metrics 库
from sklearn.metrics import accuracy_score, precision_score,
recall_score, f1_score
# 计算 accuracy、precision、recall 和 F1-score
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred, average='weighted')
recall = recall_score(y_test, y_pred, average='weighted')
f1 = f1_score(y_test, y_pred, average='weighted')
print("Accuracy:", accuracy)
print("Precision:", precision)
print("Recall:", recall)
print("F1-score:", f1)
输出
这将给出我们 Random Forest 分类器的性能指标,如下所示 −
Accuracy: 0.9811320754716981 Precision: 0.9821802935010483 Recall: 0.9811320754716981 F1-score: 0.9811157396063056
Random Forest 的优缺点
优点
以下是 Random Forest 算法的优势 −
它通过平均或组合不同决策树的结果来克服过拟合问题。
Random forest 对于大量数据项的表现比单个决策树更好。
Random forest 的方差比单个决策树小。
Random forest 非常灵活,并且具有很高的准确性。
Random forest 算法不需要对数据进行缩放。即使提供未缩放的数据,它也能保持良好的准确性。
Random forest 算法不需要对数据进行缩放。即使提供未缩放的数据,它也能保持良好的准确性。
缺点
以下是 Random Forest 算法的缺点 −
复杂度是 Random forest 算法的主要缺点。
构建 Random forest 比决策树更困难且耗时。
实现 Random Forest 算法需要更多的计算资源。
当我们有大量决策树集合时,它的可解释性较差。
使用 random forests 进行预测的过程与其他算法相比非常耗时。