【问题标题】:Proper way to customize Spark ML estimator (e.g. GaussianMixture) by modified its private method?通过修改其私有方法来自定义 Spark ML 估计器(例如 GaussianMixture)的正确方法?
【发布时间】:2021-10-23 15:10:47
【问题描述】:
我的代码使用apache.ml.clustering.GaussianMixture,但是它的init方法private def initRandom(...)效果不好,所以我想自定义一个新的init方法。
起初我想“扩展”class GuassianMixture,但initRandom 是一个私有方法。
然后我尝试了另一种方法,即设置初始 GMM,但遗憾的是源代码显示 TODO: SPARK-15785 支持用户提供了初始 GMM。
我也尝试为我的自定义类复制class GuassianMixture 的代码,但是附加的东西太多了。 GaussianMixture.scala 带有一些类和特征,其中一些只能在 ML 包中访问。
【问题讨论】:
标签:
scala
apache-spark
extends
apache-spark-ml
【解决方案1】:
我自己解决了。这是我的解决方案。
我创建了类CustomGaussianMixture,它从官方包org.apache.spark.ml.clustering扩展GaussianMixture。
在我的项目中,我创建了一个新包,也称为org.apache.spark.ml.clustering(以防止处理org.apache.spark.ml.clustering 中的复杂类/特征/对象的范围)。并将我的自定义类放入其中。
接下来就是重写方法(fit)调用initRandom,一个非私有的方法,所以我可以重写它。具体来说,只需在CustomGaussianMixture类中编写我的新init方法,并将GaussianMixture.scala中的官方源代码中的fit方法复制到CustomGaussianMixture类中,记得修改CustomGaussianMixture.fit()中的代码以调用我自定义的init方法。
最后,只需在需要时使用CustomGaussianMixture 而不是GaussianMixture。