酒生产企业网站建设的目的,房地产新闻最新消息2022,wordpress怎么给栏目添加tdk,wordpress大前端主题免费试用随机森林是一种监督学习算法#xff0c;可用于分类和回归#xff0c;但是#xff0c;它主要用于分类问题#xff0c;众所周知#xff0c;森林由树木组成#xff0c;更多树木意味着更坚固的森林。同样#xff0c;随机森林算法在数据样本上创建决策树#xff0c;然后从每…
随机森林是一种监督学习算法可用于分类和回归但是它主要用于分类问题众所周知森林由树木组成更多树木意味着更坚固的森林。同样随机森林算法在数据样本上创建决策树然后从每个样本中获取预测最后通过投票选择最佳解决方案。它是一种集成方法比单个决策树要好因为它可以通过对输出求平均值来减少过度拟合。
随机森林算法
无涯教程可以通过以下步骤来了解随机森林算法的工作原理- 步骤1 - 首先从给定的数据集中选择随机样本。 步骤2 - 接下来该算法将为每个样本构造一个决策树。然后它将从每个决策树中获得预测输出。 步骤3 - 在此步骤中将对每个预测输出进行投票。 步骤4 - 最后选择投票最多的预测输出作为最终预测输出。
下图将说明其工作方式- 代码实现
首先从导入必要的Python包开始-
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
接下来如下所示从其网络链接下载iris数据集:
pathhttps://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data
接下来需要为数据集分配列名称如下所示:
headernames[sepal-length, sepal-width, petal-length, petal-width, Class]
现在需要将数据集读取为pandas数据框如下所示:
datasetpd.read_csv(path, namesheadernames)
dataset.head()
分隔长度分隔宽度花瓣长度花瓣宽度类 0 5.1 3.5 1.4 0.2 Iris-setosa 1 4.9 3.0 1.4 0.2 Iris-setosa 2 4.7 3.2 1.3 0.2 Iris-setosa 3 4.6 3.1 1.5 0.2 Iris-setosa 4 5.0 3.6 1.4 0.2 Iris-setosa
数据预处理将在以下脚本行的帮助下完成。
Xdataset.iloc[:, :-1].values
ydataset.iloc[:, 4].values
接下来无涯教程将数据分为训练和测试拆分。以下代码将数据集分为70的训练数据和30的测试数据-
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_testtrain_test_split(X, y, test_size0.30)
接下来借助sklearn的 RandomForestClassifier 类训练模型如下所示:
from sklearn.ensemble import RandomForestClassifier
classifierRandomForestClassifier(n_estimators50)
classifier.fit(X_train, y_train)
最后需要进行预测。可以在以下脚本的帮助下完成-
y_predclassifier.predict(X_test)
接下来按如下所示打印输出-
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
result confusion_matrix(y_test, y_pred)
print(Confusion Matrix:)
print(result)
result1 classification_report(y_test, y_pred)
print(Classification Report:,)
print (result1)
result2 accuracy_score(y_test,y_pred)
print(Accuracy:,result2)
运行上面代码输出
Confusion Matrix:
[[14 0 0][ 0 18 1][ 0 0 12]]
Classification Report:precision recall f1-score supportIris-setosa 1.00 1.00 1.00 14
Iris-versicolor 1.00 0.95 0.97 19Iris-virginica 0.92 1.00 0.96 12micro avg 0.98 0.98 0.98 45macro avg 0.97 0.98 0.98 45weighted avg 0.98 0.98 0.98 45Accuracy: 0.9777777777777777 分类算法 - 随机森林 - 无涯教程网无涯教程网提供随机森林是一种监督学习算法可用于分类和回归但是它主要用于分类问题众所周知...https://www.learnfk.com/python-machine-learning/machine-learning-with-python-classification-algorithms-random-forest.html