spark1.2.0源码MLlib-线性回归

本文深入探讨Spark 1.2.0版本中MLlib库的线性回归实现。通过分析LinearRegressionWithSGD.train()方法的调用流程,揭示了其如何利用GeneralizedLinearAlgorithm.run()和GradientDescent.optimize()进行梯度下降优化以完成线性回归模型训练的过程。
摘要由CSDN通过智能技术生成

当调用LinearRegressionWithSGD.train() 时,代码执行如下:

  def train(
      input: RDD[LabeledPoint],
      numIterations: Int,
      stepSize: Double,
      miniBatchFraction: Double,
      initialWeights: Vector): LinearRegressionModel = {
    new LinearRegressionWithSGD(stepSize, numIterations, miniBatchFraction)
      .run(input, initialWeights)
  }

接着会调用LinearRegressionWithSGD的父类方法 --- GeneralizedLinearAlgorithm.run():

  /**
   * Run the algorithm with the configured parameters on an input RDD
   * of LabeledPoint entries starting from the initial weights provided.
   */
  def run(input: RDD[LabeledPoint], initialWeights: Vector): M = {

    if (input.getStorageLevel == StorageLevel.NONE) {
      logWarning("The input data is not directly cached, which may hurt performance if its"
        + " parent RDDs are also uncached.")
    }

    // Check the data properties before running the optimizer
    if (validateData && !validators.forall(func => func(input))) {
      throw new SparkException("Input validation failed.")
    }

    val scaler = if (useFeatureScaling) {
      (new StandardScaler).fit(input.map(x => x.features))  //对各个特征向量做标准化归一化处理,有助于收敛
    } else {
      null
    }

    // Prepend an extra variable consisting of all 1.0's for the intercept.
    val data = if (addIntercept) {  //是否对特征向量增加截距,默认为false
      if(useF
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值