在Scala类中设置参数。

huangapple go评论66阅读模式
英文:

Setting parameters in scala classes

问题

我正在尝试熟悉Scala(Spark)中的类,并编写了一个简单的XGBoostClassifier的包装类来进行实验。一旦包装类准备好了,我尝试设置一些参数,但是出现了错误,而且在网上找不到太多帮助。

这是我正在使用的类:

class trialXGBClassifier {
  var xgb_cls_object: XGBoostClassifier = _
}

我还编写了一个小的实用函数,将JSON转换为Map(字符串,字符串):

def jsonToMap(jsonString: String): Map[String, String] = {
  val jsonMap = JSON.parseFull(jsonString).get.asInstanceOf[Map[String, String]]
  return jsonMap
}

以下是导致错误的代码:

var params = """{"numRound": "10"}"""
var trial_xgb = new trialXGBClassifier
trial_xgb.xgb_cls_object.setSeed(jsonToMap(params)("numRound").toLong)

这是我收到的错误消息:

java.lang.NullPointerException
  ... 61 elided

我尝试使用实际的ml.dmlc类进行以下操作,它可以正常工作:

var xgb_orig = new XGBoostClassifier
xgb_orig.setSeed(jsonToMap(params)("numRound").toLong)
xgb_orig.getSeed #10

这个错误的原因是什么,我该如何解决?

英文:

I am trying to get familiar with classes in Scala(spark) and I wrote a simple wrapper class over XGBoostClassifier to experiment with. Once that wrapper class was ready, I tried setting some parameters but I am getting errors and can't find much help online

This is the class I am using

class trialXGBClassifier{
  var xgb_cls_object: XGBoostClassifier = _
}

I also wrote a small utility function to convert JSON to a Map (string, string)

def jsonToMap(jsonString: String): Map[String, String] = {
  val jsonMap = JSON.parseFull(jsonString).get.asInstanceOf[Map[String, String]]
  return jsonMap
  }

And this is the code that's giving me the error

var params = """{"numRound": "10"}"""
var trial_xgb = new trialXGBClassifier
trial_xgb.xgb_cls_object.setSeed(jsonToMap(params)("numRound").toLong)

This is the error message I get

java.lang.NullPointerException
  ... 61 elided

I tried doing the following using the actual ml.dmlc class and it works

var xgb_orig = new XGBoostClassifier
xgb_orig.setSeed(jsonToMap(params)("numRound").toLong)
xgb_orig.getSeed #10

Whats the reason behind this error and how do I fix this?

答案1

得分: 1

在Scala中,人们试图在所有可能的地方使用val而不是var。此外,他们尝试使用case classsealed trait而不仅仅是class
因此,有一种非常好的方法来避免这种问题,即使用case class

case class TrialXGBClassifier(xgb_cls_object: XGBoostClassifier)

然后,在只有需要包装时才使用你的包装器的最佳选项是:

val xgb_orig = new XGBoostClassifier
xgb_orig.setSeed(jsonToMap(params)("numRound").toLong)
val v = TrialXGBClassifier(xgb_orig)

甚至可以省略xgb_orig的声明:

val v = TrialXGBClassifier(new XGBoostClassifier)
v.xgb_cls_object.setSeed(jsonToMap(params)("numRound").toLong)
英文:

In Scala, people try to use val instead of var in all places where such usage possible. Also, they try to use case class or sealed trait instead of just class.
So, there's a very nice way to avoid such kind of issues using case class:

case class TrialXGBClassifier(xgb_cls_object: XGBoostClassifier)

And then the best option to use your wrapper only when you have something to wrap:

val xgb_orig = new XGBoostClassifier
xgb_orig.setSeed(jsonToMap(params)("numRound").toLong)
val v = TrialXGBClassifier(xgb_orig)

And you even can omit xgb_orig declaration:

val v = TrialXGBClassifier(new XGBoostClassifier)
v.xgb_cls_object.setSeed(jsonToMap(params)("numRound").toLong)

huangapple
  • 本文由 发表于 2020年1月6日 18:01:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610038.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定