pyspark - 在 Spark 中使用逻辑回归计算估计值、Wald-Chi 平方统计量、p 值的标准误差

标签 pyspark logistic-regression apache-spark-mllib standard-error

我试图在样本数据上构建逻辑回归模型。

我们可以得到的模型输出是用于构建模型的特征的权重。

我找不到用于估计标准误差、Wald-Chi Square 统计量、p 值等的 Spark API。

我在下面粘贴我的代码作为示例

import org.apache.spark.mllib.classification.LogisticRegressionWithLBFGS
import org.apache.spark.mllib.evaluation.{BinaryClassificationMetrics, MulticlassMetrics}
import org.apache.spark.mllib.linalg.Vectors
import org.apache.spark.mllib.regression.LabeledPoint
import org.apache.spark.mllib.tree.RandomForest
import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}


    val sc = new SparkContext(new SparkConf().setAppName("SparkTest").setMaster("local[*]"))

    val sqlContext = new org.apache.spark.sql.SQLContext(sc);

    val data: RDD[String] = sc.textFile("C:/Users/user/Documents/spark-1.5.1-bin-hadoop2.4/data/mllib/credit_approval_2_attr.csv")


    val parsedData = data.map { line =>
      val parts = line.split(',').map(_.toDouble)
      LabeledPoint(parts(0), Vectors.dense(parts.tail))
    }

    //Splitting the data
    val splits: Array[RDD[LabeledPoint]] = parsedData.randomSplit(Array(0.7, 0.3), seed = 11L)
    val training: RDD[LabeledPoint] = splits(0).cache()
    val test: RDD[LabeledPoint] = splits(1)



    // Run training algorithm to build the model
    val model = new LogisticRegressionWithLBFGS()
      .setNumClasses(2)
      .run(training)
    // Clear the prediction threshold so the model will return probabilities
    model.clearThreshold
    print(model.weights)

模型权重输出为
[-0.03335987643613915,0.025215092730373874,0.22617842810253946,0.29415985532104943,-0.0025559467210279694,4.5242237280512646E-4]

只是一组权重。

虽然我能够计算 Precision、Recall、Accuracy、Sensitivity 和其他模型诊断。

有没有一种方法可以计算估计的标准误差、Wald-Chi Square 统计量、Spark 中的 p 值?

我很担心,因为 R 或 SAS 中有标准输出。

这是否与我们在 Spark 中使用的优化方法有关?

这里我们使用 L-BFGS 或 SGD。

可能是我不知道评估方法。

任何建议将不胜感激。

最佳答案

以下方法将提供卡方检验的详细信息 -

Statistics.chiSqTest(data)

输入数据

val obs: RDD[LabeledPoint] =
      sc.parallelize(
        Seq(
          LabeledPoint(1.0, Vectors.dense(1.0, 0.0, 3.0)),
          LabeledPoint(1.0, Vectors.dense(1.0, 2.0, 0.0)),
          LabeledPoint(-1.0, Vectors.dense(-1.0, 0.0, -0.5)
          )
        )
      )
val featureTestResults: Array[ChiSqTestResult] = Statistics.chiSqTest(obs)

返回一个数组,其中包含针对标签的每个特征的 ChiSquaredTestResult。

检验的摘要,包括 p 值、自由度、检验统计量、使用的方法和原假设。

关于pyspark - 在 Spark 中使用逻辑回归计算估计值、Wald-Chi 平方统计量、p 值的标准误差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37816701/

相关文章:

scala - 线性回归中的日期使用以及使用 Spark mllib 将日期转换为数字

amazon-web-services - 如果 EMR 中的总集群资源内存小于从 AWS S3 读取的数据集,Spark 如何读取和处理 AWS EMR 中的数据

python - Pyspark - 将 json 字符串转换为 DataFrame

python - 针对大数据部分训练逻辑回归模型

python - 如何在Tensorflow中用Logistic层替换Softmax输出层?

python - PySpark - 在 Dataframe 上使用 randomSplit 时出错

apache-spark - 如何将列添加到 pyspark 数据框中的嵌套结构中?

python - 收集 PySpark 中 KeyVal RDD 中每个键的前 N ​​个条目

machine-learning - 编写 sigmoid 函数,输入为 (X * theta)

scala - 将元数据附加到 Spark 中的向量列