Spark-Pipeline

学习笔记。若涉及侵权,请告知删除。

Intro

构建在DataFrame之上。Mllib提供标准的机器学习算法API,能够方便的将不同的算法组合成一个独立的管道 Pipeline or workflow.

  • DataFrame: from Spark SQL as an ML dataset, which can hold a variety of data types, e.g. different columns storing text, feature vectors, true labels and predictions.
  • Estimator: is an algorithm which can be fit on a DataFrame to produce a Transformer. A learning algorithm is an Estimator which trains on a DataFrame and produces a model.
  • Transformer: is an algorithm which can transform one DataFrame into another DataFrame, e.g. an ML model is a Transformer which transforms a DataFrame with features into a DataFrame with prediction.
  • Pipeline: a pipeline chains multiple Transformers and Estimators together to specify an ML workflow.
  • Parameter: All Transformer and Estimators share a common API for specifying parameters.

Pipeline

A Pipeline is specified as a sequences of stages, and each stage is either a Transformer or an Estimator.
The input DataFrame is transformed as it passes through each stage.

For Transformer stages, the transform() method is called on the DataFrame.

For Estimator stages, the fit() method is called to produce a Transformer (which becomes a part of the PipelineModel, or fitted Pipeline)

Fit

fit

the figure is for the training time usage of a Pipeline.

The top row, three stages, the first two are Transformers and the third one is an Estimator.

The bottom row represents data flowing through the pipeline, where cylinders indicate DataFrames.

If the Pipeline has more stages after LogRegModel, it would call the model’s transform() method on the DataFrame before passing the DF to the next stage.

<think>好的,用户之前问过如何在Spark中使用Scala训练并保存模型,然后进行预测,我给出了基于Spark 3.0+的完整代码。现在用户的新问题是关于Spark 2.4.8的情况,需要我调整之前的回答以适应这个旧版本。 首先,我需要确认Spark 2.4.8和Spark 3.0+之间的主要差异,尤其是在ML库和API方面。例如,Spark 3.0引入了很多优化和新特性,但Spark 2.4.8可能在某些API或功能上有所不同。比如,LinearSVC在Spark 2.4中可能存在不同的参数或方法,或者Pipeline的保存和加载方式是否有变化? 接下来,我需要检查用户之前的代码示例中是否有不兼容的地方。例如,在Spark 2.4.8中,是否有某些类或方法已经被弃用或修改?例如,StandardScaler的setWithMean和setWithStd在旧版本中是否可用?或者VectorAssembler的输入输出列设置是否有变化? 另外,用户可能在使用旧版本时遇到依赖问题,比如需要不同的库版本。例如,Spark MLlib在2.4.x和3.x之间的包结构是否一致?需要确认用户是否使用正确的import语句。 还有,模型保存和加载的路径处理是否有变化?在Spark 2.4.8中,保存模型到本地是否需要额外的配置,比如Hadoop的依赖或者文件系统的权限? 然后,用户可能想知道如何在代码中进行调整,例如替换某些类或方法,或者调整参数设置。例如,在Spark 2.4.8中,LinearSVC是否属于不同的包,或者需要不同的参数名称? 另外,用户可能关心性能优化方面的差异,比如在Spark 2.4.8中是否缺少某些优化配置选项,或者需要不同的设置来提升训练速度。 可能还需要考虑示例数据的兼容性,比如CSV读取的选项是否有变化,处理缺失值的方法是否不同? 最后,用户可能需要关于依赖配置的建议,比如使用sbt或Maven时,正确的Spark版本和依赖库应该如何声明,避免版本冲突。 总结起来,我需要针对Spark 2.4.8调整之前的代码示例,确保所有API调用和依赖都是兼容的,并指出版本差异带来的变化,同时提供用户可能需要的配置建议和注意事项。</think>以下是针对 **Spark 2.4.8** 的完整代码适配说明和示例,包含与 Spark 3.0+ 的主要差异和关键修改点: --- ### Spark 2.4.8 与 Spark 3.0+ 的差异说明 1. **依赖库变化**: - Spark 2.4.8 的 `LinearSVC` 类位于 `org.apache.spark.ml.classification`,但某些高级特性可能受限(如缺少原生 GPU 加速) - 需要确保 Scala 版本兼容(通常 Spark 2.4.8 使用 Scala 2.11) 2. **API 兼容性**: - `StandardScaler` 的 `setWithMean` 在稀疏数据上可能表现不同 - 模型保存/加载的元数据格式与 Spark 3.0 不完全兼容(跨版本加载模型会失败) 3. **性能优化**: - Spark 2.4.8 缺少 Spark 3.0 的 Adaptive Query Execution (AQE) 优化 --- ### 适配后的完整代码(Spark 2.4.8) ```scala import org.apache.spark.sql.SparkSession import org.apache.spark.ml.feature.{VectorAssembler, StandardScaler} import org.apache.spark.ml.{Pipeline, PipelineModel} import org.apache.spark.ml.classification.LinearSVC // 1. 初始化SparkSession (注意版本标识) val spark = SparkSession.builder() .appName("SVM_Spark248") .master("local[*]") .config("spark.sql.legacy.allowUntypedScalaUDF", "true") // 兼容性配置 .getOrCreate() // 2. 数据加载(需显式指定数值类型) val rawData = spark.read .option("header", "true") .option("inferSchema", "true") // 2.4.8的类型推断可能更严格 .csv("pollution_data.csv") // 3. 数据清洗(Spark 2.4对空值处理更敏感) val cleanedData = rawData.na.drop("any") // 显式指定删除模式 // 4. 特征工程(与Spark 3.0相同,但需注意稀疏向量处理) val featureCols = Array("PM2.5", "PM10", "能见度", "O3浓度", "水质PH值") val assembler = new VectorAssembler() .setInputCols(featureCols) .setOutputCol("rawFeatures") val scaler = new StandardScaler() .setInputCol("rawFeatures") .setOutputCol("features") .setWithStd(true) .setWithMean(false) // Spark 2.4对中心化处理更保守 // 5. 模型定义(参数略有不同) val svm = new LinearSVC() .setLabelCol("结果") .setFeaturesCol("features") .setMaxIter(100) .setRegParam(0.01) .setStandardization(false) // 已手动标准化,需关闭 // 6. 构建Pipeline(与Spark 3.0相同) val pipeline = new Pipeline().setStages(Array(assembler, scaler, svm)) // 7. 训练并保存模型(注意Hadoop版本兼容性) val model = pipeline.fit(cleanedData) model.write.overwrite().save("hdfs:///path/to/model") // HDFS路径更稳定 // 8. 加载模型(需确保集群环境一致) val loadedModel = PipelineModel.load("hdfs:///path/to/model") // 9. 预测示例(数据需转换为精确类型) val testData = Seq( (40.0, 90.0, 4.5, 0.08, 6.8), (20.0, 50.0, 10.0, 0.03, 7.5) ).toDF("PM2.5", "PM10", "能见度", "O3浓度", "水质PH值") .selectExpr( "CAST(PM2.5 AS DOUBLE)", "CAST(PM10 AS DOUBLE)", "CAST(能见度 AS DOUBLE)", "CAST(O3浓度 AS DOUBLE)", "CAST(水质PH值 AS DOUBLE)" ) val predictions = loadedModel.transform(testData) predictions.show() ``` --- ### 关键适配要点 1. **依赖配置**(`build.sbt` 示例): ```scala name := "Spark248_SVM" version := "1.0" scalaVersion := "2.11.12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值