2016-09-19 160 views
2

我遇到了一些違揹我理解的事情。我的理解是,對於活動對象,'this'不能爲空,但是,對於下面顯示的情況,我正在遇到類似的情況。斯卡拉 - 可以'這個'在斯卡拉爲空對象?

上下文 - 我在這種情況下使用XGBoost4J-Spark包。你可以看看源代碼here。更具體地說,我指的是XGBoostEstimator類。我有這個類的下面的定義,只有一個附加的打印語句。

package ml.dmlc.xgboost4j.scala.spark 

import ml.dmlc.xgboost4j.scala.{EvalTrait, ObjectiveTrait} 
import org.apache.spark.ml.{Predictor, Estimator} 
import org.apache.spark.ml.param.ParamMap 
import org.apache.spark.ml.util.Identifiable 
import org.apache.spark.mllib.linalg.{VectorUDT, Vector} 
import org.apache.spark.mllib.regression.LabeledPoint 
import org.apache.spark.sql.functions._ 
import org.apache.spark.sql.types.{NumericType, DoubleType, StructType} 
import org.apache.spark.sql.{DataFrame, TypedColumn, Dataset, Row} 

/** 
* the estimator wrapping XGBoost to produce a training model 
* 
* @param inputCol the name of input column 
* @param labelCol the name of label column 
* @param xgboostParams the parameters configuring XGBoost 
* @param round the number of iterations to train 
* @param nWorkers the total number of workers of xgboost 
* @param obj the customized objective function, default to be null and using the default in model 
* @param eval the customized eval function, default to be null and using the default in model 
* @param useExternalMemory whether to use external memory when training 
* @param missing the value taken as missing 
*/ 
class XGBoostEstimator(
    inputCol: String, labelCol: String, 
    xgboostParams: Map[String, Any], round: Int, nWorkers: Int, 
    obj: Option[ObjectiveTrait] = None, 
    eval: Option[EvalTrait] = None, useExternalMemory: Boolean = false, missing: Float = Float.NaN) 
    extends Estimator[XGBoostModel] { 

    println(s"This is ${this}") 
    override val uid: String = Identifiable.randomUID("XGBoostEstimator") 


    /** 
    * produce a XGBoostModel by fitting the given dataset 
    */ 
    def fit(trainingSet: Dataset[_]): XGBoostModel = { 
    val instances = trainingSet.select(
     col(inputCol), col(labelCol).cast(DoubleType)).rdd.map { 
     case Row(feature: Vector, label: Double) => 
     LabeledPoint(label, feature) 
    } 
    transformSchema(trainingSet.schema, logging = true) 
    val trainedModel = XGBoost.trainWithRDD(instances, xgboostParams, round, nWorkers, obj.get, 
     eval.get, useExternalMemory, missing).setParent(this) 
    copyValues(trainedModel) 
    } 

    override def copy(extra: ParamMap): Estimator[XGBoostModel] = { 
    defaultCopy(extra) 
    } 

    override def transformSchema(schema: StructType): StructType = { 
    // check input type, for now we only support vectorUDT as the input feature type 
    val inputType = schema(inputCol).dataType 
    require(inputType.equals(new VectorUDT), s"the type of input column $inputCol has to VectorUDT") 
    // check label Type, 
    val labelType = schema(labelCol).dataType 
    require(labelType.isInstanceOf[NumericType], s"the type of label column $labelCol has to" + 
     s" be NumericType") 
    schema 
    } 
} 

當我初始化通過Sprak殼牌相同的代碼(或以其他方式通過測試),以下是輸出我得到:

scala> import ml.dmlc.xgboost4j.scala.spark.XGBoostEstimator 
import ml.dmlc.xgboost4j.scala.spark.XGBoostEstimator 

scala> val xgb = new XGBoostEstimator("features", "label", Map.empty,10, 2) 
This is null 
xgb: ml.dmlc.xgboost4j.scala.spark.XGBoostEstimator = XGBoostEstimator_6cd31d495c8f 

scala> xgb.uid 
res1: String = XGBoostEstimator_6cd31d495c8f 

任何澄清,爲什麼當這個行爲是可能的會有幫助。

+0

你確定它不是'this.toString()'返回字符串' 「空」'?如果你用println(「null?」+(this eq null))'來打印呢? – sjrd

+0

@sjrd我忽略了toString在基類中被覆蓋。你是對的,這是導致問題的toString。 –

回答

6

您的toString()實現來自Identifiable,它只是返回uid集合。並且由於您在下一行中設置了uid,所以在打印時它未被初始化。

身份source

trait Identifiable { 

    /** 
    * An immutable unique ID for the object and its derivatives. 
    */ 
    val uid: String 

    override def toString: String = uid 
}