首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在没有任何输入列的情况下创建自定义转换器?

如何在没有任何输入列的情况下创建自定义转换器?
EN

Stack Overflow用户
提问于 2019-02-18 11:50:42
回答 1查看 235关注 0票数 0

我们有一个需求,在这里我们想要生成模型的分数,其中包含0-1之间的一些随机值。

为了做到这一点,我们想要有一个自定义的转换器,它将生成随机数,而不需要任何输入字段。

那么,我们可以在mleap中生成一个没有输入字段的转换器吗?

就像我们通常创建的那样,如下所示:

代码语言:javascript
复制
import ml.combust.mleap.core.Model
import ml.combust.mleap.core.types._

case class RandomNumberModel() extends Model {
  private val rnd = scala.util.Random

  def apply(): Double = rnd.nextFloat

  override def inputSchema: StructType = StructType("input" -> ScalarType.String).get

  override def outputSchema: StructType = StructType("output" -> ScalarType.Double ).get

}

如何使其作为输入模式不需要放入?

EN

回答 1

Stack Overflow用户

发布于 2019-03-22 22:34:39

我从来没有尝试过,但是考虑到我是如何实现具有多个输入字段的自定义转换器的……

代码语言:javascript
复制
package org.apache.spark.ml.feature.mleap

import ml.combust.mleap.core.Model
import ml.combust.mleap.core.types._
import org.apache.spark.ml.linalg._

case class PropertyGroupAggregatorBaseModel (props: Array[String],
                                        aggFunc: String) extends Model {
  val outputSize = props.size

  //having multiple inputs, you will have apply with a parameter Seq[Any]
  def apply(features: Seq[Any]): Vector = {
    val properties = features(0).asInstanceOf[Seq[String]]
    val values = features(1).asInstanceOf[Seq[Double]]
    val mapping = properties.zip(values)
    val histogram = props.foldLeft(Array.empty[Double]){
      (acc, property) =>
        val newValues = mapping.filter(x => x._1 == property).map(x => x._2)
        val newAggregate = aggFunc match {
          case "sum" => newValues.sum.toDouble
          case "count" => newValues.size.toDouble
          case "avg" => (newValues.sum / Math.max(newValues.size, 1)).toDouble
        }
        acc :+ newAggregate
    }

    Vectors.dense(histogram)
  }

  override def inputSchema: StructType =  {
    //here you define the input 
    val inputFields = Seq(
      StructField("input1" -> ListType(BasicType.String)),
      StructField("input2" -> ListType(BasicType.Double))
    )
    StructType(inputFields).get
  }

  override def outputSchema: StructType = StructType(StructField("output" -> TensorType.Double(outputSize))).get
}

我的建议是,应用程序可能已经为您工作了。我猜如果您按如下方式定义inputSchema,它可能会起作用:

代码语言:javascript
复制
override def inputSchema: StructType =  {
    //here you define the input 
    val inputFields = Seq.empty[StructField]
    StructType(inputFields).get
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54740231

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档