0


机器学习_PySpark-3.0.3随机森林回归(RandomForestRegressor)实例

机器学习_PySpark-3.0.3随机森林回归(RandomForestRegressor)实例

随机森林回归 (Random Forest Regression):

任务类型: 随机森林回归主要用于回归任务。在回归任务中, 算法试图预测一个连续的数值输出, 而不是一个离散的类别。

输出: 随机森林回归的输出是一个连续的数值, 表示输入数据的预测结果。

算法原理: 随机森林回归同样基于决策树, 但在回归任务中, 每个决策树的输出是一个实数值。最终的预测结果是多个决策树输出的平均值或加权平均值。

在 PySpark-3.x.x 中构建随机森林回归主要使用 pyspark.ml 模块中的 RandomForestRegressor。

下面是一个简单的示例, 演示如何使用 PySpark-3.x.x 构建和训练随机森林回归模型。

实例数据

本实例是于 2023年12月30日 截取了 “Iris_Dataset (鸢尾花数据集)” 中的120条样本数据。

字段说明: SepalLength(花萼长度), SepalWidth(花萼宽度), PetalLength(花瓣长度), PetalWidth(花瓣宽度), Species(品种).

品种说明: Setosa(山鸢尾), Versicolor(变色鸢尾), Virginical(维吉尼亚鸢尾).

Iris_Dataset_120_2023-12-30.csv

  1. SepalLength,SepalWidth,PetalLength,PetalWidth,Species
  2. 6.4,2.8,5.6,2.2,Virginical
  3. 5.0,2.3,3.3,1.0,Versicolor
  4. 4.9,2.5,4.5,1.7,Virginical
  5. 4.9,3.1,1.5,0.1,Setosa
  6. 5.7,3.8,1.7,0.3,Setosa
  7. 4.4,3.2,1.3,0.2,Setosa
  8. 5.4,3.4,1.5,0.4,Setosa
  9. 6.9,3.1,5.1,2.3,Virginical
  10. 6.7,3.1,4.4,1.4,Versicolor
  11. 5.1,3.7,1.5,0.4,Setosa
  12. 5.2,2.7,3.9,1.4,Versicolor
  13. 6.9,3.1,4.9,1.5,Versicolor
  14. 5.8,4.0,1.2,0.2,Setosa
  15. 5.4,3.9,1.7,0.4,Setosa
  16. 7.7,3.8,6.7,2.2,Virginical
  17. 6.3,3.3,4.7,1.6,Versicolor
  18. 6.8,3.2,5.9,2.3,Virginical
  19. 7.6,3.0,6.6,2.1,Virginical
  20. 6.4,3.2,5.3,2.3,Virginical
  21. 5.7,4.4,1.5,0.4,Setosa
  22. 6.7,3.3,5.7,2.1,Virginical
  23. 6.4,2.8,5.6,2.1,Virginical
  24. 5.4,3.9,1.3,0.4,Setosa
  25. 6.1,2.6,5.6,1.4,Virginical
  26. 7.2,3.0,5.8,1.6,Virginical
  27. 5.2,3.5,1.5,0.2,Setosa
  28. 5.8,2.6,4.0,1.2,Versicolor
  29. 5.9,3.0,5.1,1.8,Virginical
  30. 5.4,3.0,4.5,1.5,Versicolor
  31. 6.7,3.0,5.0,1.7,Versicolor
  32. 6.3,2.3,4.4,1.3,Versicolor
  33. 5.1,2.5,3.0,1.1,Versicolor
  34. 6.4,3.2,4.5,1.5,Versicolor
  35. 6.8,3.0,5.5,2.1,Virginical
  36. 6.2,2.8,4.8,1.8,Virginical
  37. 6.9,3.2,5.7,2.3,Virginical
  38. 6.5,3.2,5.1,2.0,Virginical
  39. 5.8,2.8,5.1,2.4,Virginical
  40. 5.1,3.8,1.5,0.3,Setosa
  41. 4.8,3.0,1.4,0.3,Setosa
  42. 7.9,3.8,6.4,2.0,Virginical
  43. 5.8,2.7,5.1,1.9,Virginical
  44. 6.7,3.0,5.2,2.3,Virginical
  45. 5.1,3.8,1.9,0.4,Setosa
  46. 4.7,3.2,1.6,0.2,Setosa
  47. 6.0,2.2,5.0,1.5,Virginical
  48. 4.8,3.4,1.6,0.2,Setosa
  49. 7.7,2.6,6.9,2.3,Virginical
  50. 4.6,3.6,1.0,0.2,Setosa
  51. 7.2,3.2,6.0,1.8,Virginical
  52. 5.0,3.3,1.4,0.2,Setosa
  53. 6.6,3.0,4.4,1.4,Versicolor
  54. 6.1,2.8,4.0,1.3,Versicolor
  55. 5.0,3.2,1.2,0.2,Setosa
  56. 7.0,3.2,4.7,1.4,Versicolor
  57. 6.0,3.0,4.8,1.8,Virginical
  58. 7.4,2.8,6.1,1.9,Virginical
  59. 5.8,2.7,5.1,1.9,Virginical
  60. 6.2,3.4,5.4,2.3,Virginical
  61. 5.0,2.0,3.5,1.0,Versicolor
  62. 5.6,2.5,3.9,1.1,Versicolor
  63. 6.7,3.1,5.6,2.4,Virginical
  64. 6.3,2.5,5.0,1.9,Virginical
  65. 6.4,3.1,5.5,1.8,Virginical
  66. 6.2,2.2,4.5,1.5,Versicolor
  67. 7.3,2.9,6.3,1.8,Virginical
  68. 4.4,3.0,1.3,0.2,Setosa
  69. 7.2,3.6,6.1,2.5,Virginical
  70. 6.5,3.0,5.5,1.8,Virginical
  71. 5.0,3.4,1.5,0.2,Setosa
  72. 4.7,3.2,1.3,0.2,Setosa
  73. 6.6,2.9,4.6,1.3,Versicolor
  74. 5.5,3.5,1.3,0.2,Setosa
  75. 7.7,3.0,6.1,2.3,Virginical
  76. 6.1,3.0,4.9,1.8,Virginical
  77. 4.9,3.1,1.5,0.1,Setosa
  78. 5.5,2.4,3.8,1.1,Versicolor
  79. 5.7,2.9,4.2,1.3,Versicolor
  80. 6.0,2.9,4.5,1.5,Versicolor
  81. 6.4,2.7,5.3,1.9,Virginical
  82. 5.4,3.7,1.5,0.2,Setosa
  83. 6.1,2.9,4.7,1.4,Versicolor
  84. 6.5,2.8,4.6,1.5,Versicolor
  85. 5.6,2.7,4.2,1.3,Versicolor
  86. 6.3,3.4,5.6,2.4,Virginical
  87. 4.9,3.1,1.5,0.1,Setosa
  88. 6.8,2.8,4.8,1.4,Versicolor
  89. 5.7,2.8,4.5,1.3,Versicolor
  90. 6.0,2.7,5.1,1.6,Versicolor
  91. 5.0,3.5,1.3,0.3,Setosa
  92. 6.5,3.0,5.2,2.0,Virginical
  93. 6.1,2.8,4.7,1.2,Versicolor
  94. 5.1,3.5,1.4,0.3,Setosa
  95. 4.6,3.1,1.5,0.2,Setosa
  96. 6.5,3.0,5.8,2.2,Virginical
  97. 4.6,3.4,1.4,0.3,Setosa
  98. 4.6,3.2,1.4,0.2,Setosa
  99. 7.7,2.8,6.7,2.0,Virginical
  100. 5.9,3.2,4.8,1.8,Versicolor
  101. 5.1,3.8,1.6,0.2,Setosa
  102. 4.9,3.0,1.4,0.2,Setosa
  103. 4.9,2.4,3.3,1.0,Versicolor
  104. 4.5,2.3,1.3,0.3,Setosa
  105. 5.8,2.7,4.1,1.0,Versicolor
  106. 5.0,3.4,1.6,0.4,Setosa
  107. 5.2,3.4,1.4,0.2,Setosa
  108. 5.3,3.7,1.5,0.2,Setosa
  109. 5.0,3.6,1.4,0.2,Setosa
  110. 5.6,2.9,3.6,1.3,Versicolor
  111. 4.8,3.1,1.6,0.2,Setosa
  112. 6.3,2.7,4.9,1.8,Virginical
  113. 5.7,2.8,4.1,1.3,Versicolor
  114. 5.0,3.0,1.6,0.2,Setosa
  115. 6.3,3.3,6.0,2.5,Virginical
  116. 5.0,3.5,1.6,0.6,Setosa
  117. 5.5,2.6,4.4,1.2,Versicolor
  118. 5.7,3.0,4.2,1.2,Versicolor
  119. 4.4,2.9,1.4,0.2,Setosa
  120. 4.8,3.0,1.4,0.1,Setosa
  121. 5.5,2.4,3.7,1.0,Versicolor

探索思路

这里只是简单示例, 目的在于熟悉 Spark 中的随机森林回归使用方法。

目标:

通过

  1. SepalLength(花萼长度)

,

  1. SepalWidth(花萼宽度)

,

  1. PetalLength(花瓣长度)

,

  1. PetalWidth(花瓣宽度)

预测

  1. Iris(鸢尾花)

  1. Species(品种)

标签:

由于

  1. Iris(鸢尾花)

  1. Species(品种)

  1. 字符串(String)

的形式, 本例将使用

  1. pyspark.ml

  1. StringIndexer

模块将

  1. Iris(鸢尾花)

  1. Species(品种)

索引化。

导入 pyspark.sql 相关模块

Spark SQL 是用于结构化数据处理的 Spark 模块。它提供了一种成为 DataFrame 编程抽象, 是由 SchemaRDD 发展而来。

不同于 SchemaRDD 直接继承 RDD, DataFrame 自己实现了 RDD 的绝大多数功能。

  1. from pyspark.sql import Row, SparkSession
  2. from pyspark.sql.functions import col
  3. from pyspark.sql.types import StringType, DoubleType

导入 pyspark.ml 相关模块

Spark 在核心数据抽象 RDD 的基础上, 支持 4 大组件, 其中机器学习占其一。

进一步的, Spark 中实际上支持两个机器学习模块, MLlib 和 ML, 区别在于前者主要是基于 RDD 数据结构, 当前处于维护状态; 而后者则是 DataFrame 数据结构, 支持更多的算法, 后续将以此为主进行迭代。

所以, 在实际应用中优先使用 ML 子模块。

Spark 的 ML 库与 Python 中的另一大机器学习库 Sklearn 的关系是: Spark 的 ML 库支持大部分机器学习算法和接口功能, 虽远不如 Sklearn 功能全面, 但主要面向分布式训练, 针对大数据。

而 Sklearn 是单点机器学习算法库, 支持几乎所有主流的机器学习算法, 从样例数据, 特征选择, 模型选择和验证, 基础学习算法和集成学习算法, 提供了机器学习一站式解决方案, 但仅支持并行而不支持分布式。

  1. from pyspark.ml.feature import StringIndexer, VectorAssembler
  2. from pyspark.ml.regression import RandomForestRegressor
  3. from pyspark.ml.evaluation import RegressionEvaluator

创建 SparkSession 对象

Spark 2.0 以上版本的 spark-shell 在启动时会自动创建一个名为 spark 的 SparkSession 对象。

当需要手工创建时, SparkSession 可以由其伴生对象的 builder 方法创建出来。

  1. spark = SparkSession.builder.master("local[*]").appName("spark").getOrCreate()

使用 Spark 构建 DataFrame 数据 (Optional)

当数据量较小时, 可以使用该方法手工构建 DataFrame 数据。

构建数据行 Row (以前 5 行为例):

  1. Row(SepalLength=6.4, SepalWidth=2.8, PetalLength=5.6, PetalWidth=2.2, Species="Virginical")
  2. Row(SepalLength=5.0, SepalWidth=2.3, PetalLength=3.3, PetalWidth=1.0, Species="Versicolor")
  3. Row(SepalLength=4.9, SepalWidth=2.5, PetalLength=4.5, PetalWidth=1.7, Species="Virginical")
  4. Row(SepalLength=4.9, SepalWidth=3.1, PetalLength=1.5, PetalWidth=0.1, Species="Setosa")
  5. Row(SepalLength=5.7, SepalWidth=3.8, PetalLength=1.7, PetalWidth=0.3, Species="Setosa")

将构建好的数据行 Row 加入列表 (以前 5 行为例):

  1. Data_Rows =[
  2. Row(SepalLength=6.4, SepalWidth=2.8, PetalLength=5.6, PetalWidth=2.2, Species="Virginical"),
  3. Row(SepalLength=5.0, SepalWidth=2.3, PetalLength=3.3, PetalWidth=1.0, Species="Versicolor"),
  4. Row(SepalLength=4.9, SepalWidth=2.5, PetalLength=4.5, PetalWidth=1.7, Species="Virginical"),
  5. Row(SepalLength=4.9, SepalWidth=3.1, PetalLength=1.5, PetalWidth=0.1, Species="Setosa"),
  6. Row(SepalLength=5.7, SepalWidth=3.8, PetalLength=1.7, PetalWidth=0.3, Species="Setosa")]

生成 DataFrame 数据框 (以前 5 行为例):

  1. SDF = spark.createDataFrame(data=Data_Rows)

输出 DataFrame 数据框 (以前 5 行为例):

  1. print("[Message] Builded Spark DataFrame:")
  2. SDF.show(3)

输出:

  1. [Message] Builded Spark DataFrame:
  2. +-----------+----------+-----------+----------+----------+
  3. |SepalLength|SepalWidth|PetalLength|PetalWidth| Species|
  4. +-----------+----------+-----------+----------+----------+
  5. | 6.4| 2.8| 5.6| 2.2|Virginical|
  6. | 5.0| 2.3| 3.3| 1.0|Versicolor|
  7. | 4.9| 2.5| 4.5| 1.7|Virginical|
  8. | 4.9| 3.1| 1.5| 0.1| Setosa|
  9. | 5.7| 3.8| 1.7| 0.3| Setosa|
  10. +-----------+----------+-----------+----------+----------+
  11. only showing top 3 rows

使用 Spark 读取 CSV 数据

调用 SparkSession 的 .read 方法读取 CSV 数据:

其中 .option 是读取文件时的选项, 左边是 “键(Key)”, 右边是 “值(Value)”, 例如 .option(“header”, “true”) 与 {header = “true”} 类同。

  1. SDF = spark.read.option("header","true").option("encoding","utf-8").csv("file:///D:\\Iris_Dataset_120_2023-12-30.csv")

输出 DataFrame 数据框:

  1. print("[Message] Readed CSV File: D:\\Iris_Dataset_120_2023-12-30.csv")
  2. SDF.show()

输出:

  1. [Message] Readed CSV File: D:\Iris_Dataset_120_2023-12-30.csv
  2. +-----------+----------+-----------+----------+----------+
  3. |SepalLength|SepalWidth|PetalLength|PetalWidth| Species|
  4. +-----------+----------+-----------+----------+----------+
  5. | 6.4| 2.8| 5.6| 2.2|Virginical|
  6. | 5.0| 2.3| 3.3| 1.0|Versicolor|
  7. | 4.9| 2.5| 4.5| 1.7|Virginical|
  8. | 4.9| 3.1| 1.5| 0.1| Setosa|
  9. | 5.7| 3.8| 1.7| 0.3| Setosa|
  10. | 4.4| 3.2| 1.3| 0.2| Setosa|
  11. | 5.4| 3.4| 1.5| 0.4| Setosa|
  12. | 6.9| 3.1| 5.1| 2.3|Virginical|
  13. | 6.7| 3.1| 4.4| 1.4|Versicolor|
  14. | 5.1| 3.7| 1.5| 0.4| Setosa|
  15. | 5.2| 2.7| 3.9| 1.4|Versicolor|
  16. | 6.9| 3.1| 4.9| 1.5|Versicolor|
  17. | 5.8| 4.0| 1.2| 0.2| Setosa|
  18. | 5.4| 3.9| 1.7| 0.4| Setosa|
  19. | 7.7| 3.8| 6.7| 2.2|Virginical|
  20. | 6.3| 3.3| 4.7| 1.6|Versicolor|
  21. | 6.8| 3.2| 5.9| 2.3|Virginical|
  22. | 7.6| 3.0| 6.6| 2.1|Virginical|
  23. | 6.4| 3.2| 5.3| 2.3|Virginical|
  24. | 5.7| 4.4| 1.5| 0.4| Setosa|
  25. +-----------+----------+-----------+----------+----------+
  26. only showing top 20 rows

转换 Spark 中 DateFrame 各列数据类型

通常情况下, 为了避免计算出现数据类型的错误, 都需要重新转换一下数据类型。

  1. # 转换 Spark 中 DateFrame 数据类型。
  2. SDF = SDF.withColumn("SepalLength", col("SepalLength").cast(DoubleType()))
  3. SDF = SDF.withColumn("SepalWidth", col("SepalWidth").cast(DoubleType()))
  4. SDF = SDF.withColumn("PetalLength", col("PetalLength").cast(DoubleType()))
  5. SDF = SDF.withColumn("PetalWidth", col("PetalWidth").cast(DoubleType()))
  6. SDF = SDF.withColumn("Species", col("Species").cast(StringType()))# 输出 Spark DataFrame 字段和数据类型。print("[Message] Changed Spark DataFrame Data Type:")
  7. SDF.printSchema()

输出:

  1. [Message] Changed Spark DataFrame Data Type:
  2. root
  3. |-- SepalLength: double (nullable = true)
  4. |-- SepalWidth: double (nullable = true)
  5. |-- PetalLength: double (nullable = true)
  6. |-- PetalWidth: double (nullable = true)
  7. |-- Species: string (nullable = true)

字符串索引化 (StringIndexer) 转换 Species 列

StringIndexer (字符串-索引变换) 是一个估计器, 是将字符串列编码为标签索引列。索引位于

  1. [0, numLabels)

, 按标签频率排序, 频率最高的排 0, 依次类推, 因此最常见的标签获取索引是 0。

  1. # 使用 StringIndexer 转换 Species 列。
  2. MyStringIndexer = StringIndexer(inputCol="Species", outputCol="SpeciesIdx")# 拟合并转换数据。
  3. IndexedSDF = MyStringIndexer.fit(SDF).transform(SDF)# 输出 StringIndexer 的转换效果。print("[Message] The Effect of StringIndexer:")
  4. IndexedSDF.show()

输出:

  1. [Message] The Effect of StringIndexer:
  2. +-----------+----------+-----------+----------+----------+----------+
  3. |SepalLength|SepalWidth|PetalLength|PetalWidth| Species|SpeciesIdx|
  4. +-----------+----------+-----------+----------+----------+----------+
  5. | 6.4| 2.8| 5.6| 2.2|Virginical| 1.0|
  6. | 5.0| 2.3| 3.3| 1.0|Versicolor| 2.0|
  7. | 4.9| 2.5| 4.5| 1.7|Virginical| 1.0|
  8. | 4.9| 3.1| 1.5| 0.1| Setosa| 0.0|
  9. | 5.7| 3.8| 1.7| 0.3| Setosa| 0.0|
  10. | 4.4| 3.2| 1.3| 0.2| Setosa| 0.0|
  11. | 5.4| 3.4| 1.5| 0.4| Setosa| 0.0|
  12. | 6.9| 3.1| 5.1| 2.3|Virginical| 1.0|
  13. | 6.7| 3.1| 4.4| 1.4|Versicolor| 2.0|
  14. | 5.1| 3.7| 1.5| 0.4| Setosa| 0.0|
  15. | 5.2| 2.7| 3.9| 1.4|Versicolor| 2.0|
  16. | 6.9| 3.1| 4.9| 1.5|Versicolor| 2.0|
  17. | 5.8| 4.0| 1.2| 0.2| Setosa| 0.0|
  18. | 5.4| 3.9| 1.7| 0.4| Setosa| 0.0|
  19. | 7.7| 3.8| 6.7| 2.2|Virginical| 1.0|
  20. | 6.3| 3.3| 4.7| 1.6|Versicolor| 2.0|
  21. | 6.8| 3.2| 5.9| 2.3|Virginical| 1.0|
  22. | 7.6| 3.0| 6.6| 2.1|Virginical| 1.0|
  23. | 6.4| 3.2| 5.3| 2.3|Virginical| 1.0|
  24. | 5.7| 4.4| 1.5| 0.4| Setosa| 0.0|
  25. +-----------+----------+-----------+----------+----------+----------+
  26. only showing top 20 rows

提取 标签(Label)列 和 特征向量(Features)列

在创建特征向量(Features)列时, 将会用到 VectorAssembler 模块, VectorAssembler 将多个特征合并为一个特征向量。

提取 标签(Label) 列:

  1. # 将 SpeciesIdx 列复制为 Label 列。
  2. NewSDF = IndexedSDF.withColumn("Label", col("SpeciesIdx"))

创建 特征向量(Features) 列:

  1. # VectorAssembler 将多个特征合并为一个特征向量。
  2. FeaColsName:list=["SepalLength","SepalWidth","PetalLength","PetalWidth"]
  3. MyAssembler = VectorAssembler(inputCols=FeaColsName, outputCol="Features")# 创建 特征向量(Features) 列: 拟合数据 (可选, 如果在模型训练时使用 Pipeline, 则无需在此步骤拟合数据, 当然也就无法在此步骤预览数据)。
  4. AssembledSDF = MyAssembler.transform(NewSDF)

输出预览:

  1. print("[Message] Assembled Label and Features for RandomForestRegressor:")
  2. AssembledSDF.show()

预览:

  1. [Message] Assembled for RandomForestRegressor:
  2. +-----------+----------+-----------+----------+----------+----------+-----+-----------------+
  3. |SepalLength|SepalWidth|PetalLength|PetalWidth| Species|SpeciesIdx|Label| Features|
  4. +-----------+----------+-----------+----------+----------+----------+-----+-----------------+
  5. | 6.4| 2.8| 5.6| 2.2|Virginical| 1.0| 1.0|[6.4,2.8,5.6,2.2]|
  6. | 5.0| 2.3| 3.3| 1.0|Versicolor| 2.0| 2.0|[5.0,2.3,3.3,1.0]|
  7. | 4.9| 2.5| 4.5| 1.7|Virginical| 1.0| 1.0|[4.9,2.5,4.5,1.7]|
  8. | 4.9| 3.1| 1.5| 0.1| Setosa| 0.0| 0.0|[4.9,3.1,1.5,0.1]|
  9. | 5.7| 3.8| 1.7| 0.3| Setosa| 0.0| 0.0|[5.7,3.8,1.7,0.3]|
  10. | 4.4| 3.2| 1.3| 0.2| Setosa| 0.0| 0.0|[4.4,3.2,1.3,0.2]|
  11. | 5.4| 3.4| 1.5| 0.4| Setosa| 0.0| 0.0|[5.4,3.4,1.5,0.4]|
  12. | 6.9| 3.1| 5.1| 2.3|Virginical| 1.0| 1.0|[6.9,3.1,5.1,2.3]|
  13. | 6.7| 3.1| 4.4| 1.4|Versicolor| 2.0| 2.0|[6.7,3.1,4.4,1.4]|
  14. | 5.1| 3.7| 1.5| 0.4| Setosa| 0.0| 0.0|[5.1,3.7,1.5,0.4]|
  15. | 5.2| 2.7| 3.9| 1.4|Versicolor| 2.0| 2.0|[5.2,2.7,3.9,1.4]|
  16. | 6.9| 3.1| 4.9| 1.5|Versicolor| 2.0| 2.0|[6.9,3.1,4.9,1.5]|
  17. | 5.8| 4.0| 1.2| 0.2| Setosa| 0.0| 0.0|[5.8,4.0,1.2,0.2]|
  18. | 5.4| 3.9| 1.7| 0.4| Setosa| 0.0| 0.0|[5.4,3.9,1.7,0.4]|
  19. | 7.7| 3.8| 6.7| 2.2|Virginical| 1.0| 1.0|[7.7,3.8,6.7,2.2]|
  20. | 6.3| 3.3| 4.7| 1.6|Versicolor| 2.0| 2.0|[6.3,3.3,4.7,1.6]|
  21. | 6.8| 3.2| 5.9| 2.3|Virginical| 1.0| 1.0|[6.8,3.2,5.9,2.3]|
  22. | 7.6| 3.0| 6.6| 2.1|Virginical| 1.0| 1.0|[7.6,3.0,6.6,2.1]|
  23. | 6.4| 3.2| 5.3| 2.3|Virginical| 1.0| 1.0|[6.4,3.2,5.3,2.3]|
  24. | 5.7| 4.4| 1.5| 0.4| Setosa| 0.0| 0.0|[5.7,4.4,1.5,0.4]|
  25. +-----------+----------+-----------+----------+----------+----------+-----+-----------------+
  26. only showing top 20 rows

训练 随机森林回归(RandomForestRegressor) 模型

将数据集划分为 “训练集” 和 “测试集”:

  1. (TrainingData, TestData)= AssembledSDF.randomSplit([0.8,0.2], seed=42)

创建 随机森林回归(RandomForestRegressor):

  1. RFR = RandomForestRegressor(featuresCol="Features", labelCol="Label")

创建 Pipeline (可选):

  1. # 创建 Pipeline, 将特征向量转换和随机森林回归模型组合在一起# 注意: 如果要使用 Pipeline, 则在创建 特征向量(Features)列 的时候不需要拟合数据, 否则会报 "Output column Features already exists." 的错误。
  2. MyPipeline = Pipeline(stages=[MyAssembler, RFR])

训练 随机森林回归(RandomForestRegressor) 模型:

如果在创建 特征向量(Features)列 的时候已经拟合数据:

  1. # 训练模型 (普通模式)。
  2. Model = RFR.fit(TrainingData)

如果在创建 特征向量(Features)列 的时候没有拟合数据:

  1. # 训练模型 (Pipeline 模式)。
  2. Model = MyPipeline.fit(TrainingData)

使用 随机森林回归(RandomForestRegressor) 模型预测数据

  1. # 在测试集上进行预测。
  2. Predictions = Model.transform(TestData)print("[Message] Prediction Results on The Test Data Set for RandomForestRegressor:")
  3. Predictions.show()

输出:

  1. [Message] Prediction Results on The Test Data Set for RandomForestRegressor:
  2. +-----------+----------+-----------+----------+----------+----------+-----+-----------------+------------------+
  3. |SepalLength|SepalWidth|PetalLength|PetalWidth| Species|SpeciesIdx|Label| Features| prediction|
  4. +-----------+----------+-----------+----------+----------+----------+-----+-----------------+------------------+
  5. | 4.4| 3.2| 1.3| 0.2| Setosa| 0.0| 0.0|[4.4,3.2,1.3,0.2]| 0.0|
  6. | 4.6| 3.4| 1.4| 0.3| Setosa| 0.0| 0.0|[4.6,3.4,1.4,0.3]| 0.0|
  7. | 4.7| 3.2| 1.3| 0.2| Setosa| 0.0| 0.0|[4.7,3.2,1.3,0.2]| 0.0|
  8. | 4.8| 3.4| 1.6| 0.2| Setosa| 0.0| 0.0|[4.8,3.4,1.6,0.2]| 0.0|
  9. | 4.9| 3.1| 1.5| 0.1| Setosa| 0.0| 0.0|[4.9,3.1,1.5,0.1]| 0.0|
  10. | 5.0| 3.2| 1.2| 0.2| Setosa| 0.0| 0.0|[5.0,3.2,1.2,0.2]| 0.0|
  11. | 5.0| 3.6| 1.4| 0.2| Setosa| 0.0| 0.0|[5.0,3.6,1.4,0.2]| 0.0|
  12. | 5.1| 3.8| 1.9| 0.4| Setosa| 0.0| 0.0|[5.1,3.8,1.9,0.4]| 0.2|
  13. | 5.5| 2.4| 3.7| 1.0|Versicolor| 2.0| 2.0|[5.5,2.4,3.7,1.0]| 2.0|
  14. | 5.5| 2.4| 3.8| 1.1|Versicolor| 2.0| 2.0|[5.5,2.4,3.8,1.1]| 2.0|
  15. | 5.5| 2.6| 4.4| 1.2|Versicolor| 2.0| 2.0|[5.5,2.6,4.4,1.2]| 2.0|
  16. | 5.6| 2.5| 3.9| 1.1|Versicolor| 2.0| 2.0|[5.6,2.5,3.9,1.1]| 2.0|
  17. | 5.6| 2.9| 3.6| 1.3|Versicolor| 2.0| 2.0|[5.6,2.9,3.6,1.3]| 2.0|
  18. | 5.7| 3.0| 4.2| 1.2|Versicolor| 2.0| 2.0|[5.7,3.0,4.2,1.2]| 2.0|
  19. | 5.8| 2.8| 5.1| 2.4|Virginical| 1.0| 1.0|[5.8,2.8,5.1,2.4]| 1.0|
  20. | 6.0| 3.0| 4.8| 1.8|Virginical| 1.0| 1.0|[6.0,3.0,4.8,1.8]|1.3833333333333333|
  21. | 6.2| 3.4| 5.4| 2.3|Virginical| 1.0| 1.0|[6.2,3.4,5.4,2.3]| 1.0|
  22. | 6.7| 3.1| 5.6| 2.4|Virginical| 1.0| 1.0|[6.7,3.1,5.6,2.4]| 1.0|
  23. | 7.3| 2.9| 6.3| 1.8|Virginical| 1.0| 1.0|[7.3,2.9,6.3,1.8]| 1.0|
  24. | 7.7| 2.8| 6.7| 2.0|Virginical| 1.0| 1.0|[7.7,2.8,6.7,2.0]| 1.0|
  25. +-----------+----------+-----------+----------+----------+----------+-----+-----------------+------------------+
  26. only showing top 20 rows

使用 RegressionEvaluator 评估模型性能

  1. # 使用 RegressionEvaluator 评估模型性能。
  2. MyEvaluator = RegressionEvaluator(labelCol="Label", predictionCol="prediction", metricName="mse")
  3. mse = MyEvaluator.evaluate(Predictions)print("均方误差(MSE): %f"% mse)

输出:

  1. 均方误差(MSE): 0.008497

完整代码

  1. #!/usr/bin/python3# Create By GF 2023-12-30# 请确保你的 DataFrame 包含一个名为 Label 的列, 这是 Species(品种) 的列。# 如果 label 是字符串类型的分类特征, 你可能需要使用 StringIndexer 进行索引。from pyspark.sql import Row, SparkSession
  2. from pyspark.sql.functions import col
  3. from pyspark.sql.types import StringType, DoubleType
  4. # --------------------------------------------------from pyspark.ml.feature import StringIndexer, VectorAssembler
  5. from pyspark.ml.regression import RandomForestRegressor
  6. from pyspark.ml.evaluation import RegressionEvaluator
  7. # Spark 2.0 以上版本的 spark-shell 在启动时会自动创建一个名为 spark 的 SparkSession 对象。# 当需要手工创建时, SparkSession 可以由其伴生对象的 builder 方法创建出来。
  8. spark = SparkSession.builder.master("local[*]").appName("spark").getOrCreate()# 调用 SparkSession .read 方法读取 CSV 数据:# 其中 .option 是读取文件时的选项, 左边是 "键(Key)", 右边是 "值(Value)", 例如 .option("header", "true") {header = "true"} 类同。
  9. SDF = spark.read.option("header","true").option("encoding","utf-8").csv("file:///D:\\Iris_Dataset_120_2023-12-30.csv")print("[Message] Readed CSV File: D:\\Iris_Dataset_120_2023-12-30.csv")
  10. SDF.show()# 转换 Spark DateFrame 数据类型。
  11. SDF = SDF.withColumn("SepalLength", col("SepalLength").cast(DoubleType()))
  12. SDF = SDF.withColumn("SepalWidth", col("SepalWidth").cast(DoubleType()))
  13. SDF = SDF.withColumn("PetalLength", col("PetalLength").cast(DoubleType()))
  14. SDF = SDF.withColumn("PetalWidth", col("PetalWidth").cast(DoubleType()))
  15. SDF = SDF.withColumn("Species", col("Species").cast(StringType()))# 输出 Spark DataFrame 字段和数据类型。print("[Message] Changed Spark DataFrame Data Type:")
  16. SDF.printSchema()# 使用 StringIndexer 转换 Species 列。
  17. MyStringIndexer = StringIndexer(inputCol="Species", outputCol="SpeciesIdx")# 拟合并转换数据。
  18. IndexedSDF = MyStringIndexer.fit(SDF).transform(SDF)# 输出 StringIndexer 的转换效果。print("[Message] The Effect of StringIndexer:")
  19. IndexedSDF.show()# SpeciesIdx 列复制为 Label 列。
  20. NewSDF = IndexedSDF.withColumn("Label", col("SpeciesIdx"))# VectorAssembler 将多个特征合并为一个特征向量。
  21. FeaColsName:list=["SepalLength","SepalWidth","PetalLength","PetalWidth"]
  22. MyAssembler = VectorAssembler(inputCols=FeaColsName, outputCol="Features")# 创建 特征向量(Features) 列: 拟合数据 (可选, 如果在模型训练时使用 Pipeline, 则无需在此步骤拟合数据, 当然也就无法在此步骤预览数据)。
  23. AssembledSDF = MyAssembler.transform(NewSDF)print("[Message] Assembled Label and Features for RandomForestRegressor:")
  24. AssembledSDF.show()# 将数据集划分为 "训练集" "测试集"。(TrainingData, TestData)= AssembledSDF.randomSplit([0.8,0.2], seed=42)# 创建 随机森林回归(RandomForestRegressor)。
  25. RFR = RandomForestRegressor(featuresCol="Features", labelCol="Label")# 创建 Pipeline (可选): 将特征向量转换和随机森林回归模型组合在一起# 注意: 如果要使用 Pipeline, 则在创建 特征向量(Features)列 的时候不需要拟合数据, 否则会报 "Output column Features already exists." 的错误。#MyPipeline = Pipeline(stages=[MyAssembler, RFR])# 训练模型 (普通模式)。
  26. Model = RFR.fit(TrainingData)# 训练模型 (Pipeline 模式)。#Model = MyPipeline.fit(TrainingData)# 在测试集上进行预测。
  27. Predictions = Model.transform(TestData)print("[Message] Prediction Results on The Test Data Set for RandomForestRegressor:")
  28. Predictions.show()# 使用 RegressionEvaluator 评估模型性能。
  29. MyEvaluator = RegressionEvaluator(labelCol="Label", predictionCol="prediction", metricName="mse")
  30. mse = MyEvaluator.evaluate(Predictions)print("均方误差(MSE): %f"% mse)

其它

请确保你的 DataFrame 包含一个名为 Label 的列, 这是 Species(品种) 的列。

如果 label 是字符串类型的分类特征, 你可能需要使用 StringIndexer 进行索引。

总结

以上就是关于 机器学习 PySpark-3.0.3随机森林回归(RandomForestRegressor)实例 的全部内容。

更多内容可以访问我的代码仓库:

https://gitee.com/goufeng928/public

https://github.com/goufeng928/public


本文转载自: https://blog.csdn.net/goufeng93/article/details/137404155
版权归原作者 Mostcow 所有, 如有侵权,请联系我们删除。

“机器学习_PySpark-3.0.3随机森林回归(RandomForestRegressor)实例”的评论:

还没有评论