Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pyspark管道中的用户定义转换器_Python_Apache Spark_Machine Learning_Pyspark_Spark Dataframe - Fatal编程技术网

Python Pyspark管道中的用户定义转换器

Python Pyspark管道中的用户定义转换器,python,apache-spark,machine-learning,pyspark,spark-dataframe,Python,Apache Spark,Machine Learning,Pyspark,Spark Dataframe,我正在尝试创建一个pyspark管道来运行分类模型。我的数据集有一个字符串列。因此,在管道中应用模型之前,我使用“StringIndexer”将其转换为数值 我的管道只包含两个阶段StringIndexer和ClassificationModel StringIndexer正在使用索引创建一个新列,但旧列也会保留。我想在管道中引入一个新的转换器来删除一个“字符串”列。这可能吗 是否有其他方法可以删除StringIndexer中的实际列 谢谢是的,您可以扩展抽象类转换器并创建您自己的转换器,从而删

我正在尝试创建一个pyspark管道来运行分类模型。我的数据集有一个字符串列。因此,在管道中应用模型之前,我使用“StringIndexer”将其转换为数值

我的管道只包含两个阶段StringIndexer和ClassificationModel

StringIndexer正在使用索引创建一个新列,但旧列也会保留。我想在管道中引入一个新的转换器来删除一个“字符串”列。这可能吗

是否有其他方法可以删除StringIndexer中的实际列


谢谢

是的,您可以扩展
抽象类转换器
并创建您自己的转换器,从而删除不必要的列

这应该如下所示:

import org.apache.spark.ml.Transformer
import org.apache.spark.ml.param.ParamMap
import org.apache.spark.ml.util.Identifiable
import org.apache.spark.sql.{DataFrame, Dataset}
import org.apache.spark.sql.types.{
  ArrayType,
  StringType,
  StructField,
  StructType
}
import org.apache.spark.sql.functions.collect_list

class Dropper(override val uid: String) extends Transformer {

  def this() = this(Identifiable.randomUID("dropper"))

  override def transform(dataset: Dataset[_]): DataFrame = {
    dataset.drop("your-column-name-here")
  }

  override def copy(extra: ParamMap): Transformer = defaultCopy(extra)

  override def transformSchema(schema: StructType): StructType = {
    //here you should right your result schema i.e. the schema without the dropped column
  }

}
我已经做了一段时间了,它对我来说很好

注意,您还可以扩展
抽象类估计器


希望能有帮助。致以最诚挚的问候

@Deva如果答案正确,请给出正确答案,否则请评论您的问题