有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将字典保存到。Scala中的txt文件

所以我正在训练一个超参数调优的xgboost。因此,我的代码片段如下所示:

val paramGrid = new ParamGridBuilder().
    addGrid(booster.minChildWeight, Array(0.3,0.6,0.7, 0.8)).
    addGrid(booster.eta, Array(0.1,0.2,0.4, 0.6)).
    build()


val cv = new CrossValidator().
    setEstimator(pipeline).
    setEvaluator(evaluator).
    setEstimatorParamMaps(paramGrid).
    setNumFolds(10)

val cvModel = cv.fit(df)

val bestModel = cvModel.bestModel.asInstanceOf[PipelineModel].stages(1).
    asInstanceOf[XGBoostClassificationModel]

现在,我想将参数映射保存为txt,并在以后对其进行解析。但是,当我尝试将其导出到文本文件时,使用以下内容:

bestModel.extractParamMap()

val file = new File("/home/hadoop/test/hyper_params.txt")
val bw = new BufferedWriter(new FileWriter(file))
bw.write(bestModel.extractParamMap())
bw.close()

我得到以下错误:

error: overloaded method value write with alternatives:
  (x$1: Int)Unit <and>
  (x$1: String)Unit <and>
  (x$1: Array[Char])Unit
 cannot be applied to (org.apache.spark.ml.param.ParamMap)
       bw.write(bestModel.extractParamMap())

我是scala的新手,还没有找到任何关于如何将参数映射保存到.txt文件的解决方案。这是我问题的第一步

接下来,我想创建一些变量,其中我想从.txt文件中读取保存的参数值

说这样的话:

val min_child_weight=('../param.txt){key value here}

那我怎么做呢?我已经浏览了一些帖子,比如thisthis,但是没有找到适合我的代码


共 (1) 个答案

  1. # 1 楼答案

    首先,您不需要使用常规的BufferedWriter将Spark中的内容保存到本地文件系统。通常,对于数据帧和RDD,您将使用Spark API并将路径前缀为"file:///",如图所示-How to save Spark RDD to local filesystem。另外,您将使用MLWriter来完成您正在做的事情,并且您将像这样保存整个管道-https://jaceklaskowski.gitbooks.io/mastering-apache-spark/spark-mllib/spark-mllib-pipelines-persistence.html

    更新:

    spark
     .sparkContext
     .parallelize(List(bestModel.extractParamMap().toString))
     .saveAsTextFile("file:///home/hadoop/test/hyper_params.txt")