编程语言
首页 > 编程语言> > python – 在PySpark ML中创建自定义Transformer

python – 在PySpark ML中创建自定义Transformer

作者:互联网

我是Spark SQL DataFrames和ML的新手(PySpark).
如何创建服装标记器,例如删除停用词并使用中的某些库?我可以延长默认值吗?

谢谢.

解决方法:

Can I extend the default one?

并不是的.默认Tokenizer是pyspark.ml.wrapper.JavaTransformer的子类,与pyspark.ml.feature中的其他transfromers和估算器一样,将实际处理委托给其Scala对应项.由于您想使用Python,您应该直接扩展pyspark.ml.pipeline.Transformer.

import nltk

from pyspark import keyword_only  ## < 2.0 -> pyspark.ml.util.keyword_only
from pyspark.ml import Transformer
from pyspark.ml.param.shared import HasInputCol, HasOutputCol, Param
from pyspark.sql.functions import udf
from pyspark.sql.types import ArrayType, StringType

class NLTKWordPunctTokenizer(Transformer, HasInputCol, HasOutputCol):

    @keyword_only
    def __init__(self, inputCol=None, outputCol=None, stopwords=None):
        super(NLTKWordPunctTokenizer, self).__init__()
        self.stopwords = Param(self, "stopwords", "")
        self._setDefault(stopwords=set())
        kwargs = self._input_kwargs
        self.setParams(**kwargs)

    @keyword_only
    def setParams(self, inputCol=None, outputCol=None, stopwords=None):
        kwargs = self._input_kwargs
        return self._set(**kwargs)

    def setStopwords(self, value):
        self._paramMap[self.stopwords] = value
        return self

    def getStopwords(self):
        return self.getOrDefault(self.stopwords)

    def _transform(self, dataset):
        stopwords = self.getStopwords()

        def f(s):
            tokens = nltk.tokenize.wordpunct_tokenize(s)
            return [t for t in tokens if t.lower() not in stopwords]

        t = ArrayType(StringType())
        out_col = self.getOutputCol()
        in_col = dataset[self.getInputCol()]
        return dataset.withColumn(out_col, udf(f, t)(in_col))

示例用法(来自ML – Features的数据):

sentenceDataFrame = spark.createDataFrame([
  (0, "Hi I heard about Spark"),
  (0, "I wish Java could use case classes"),
  (1, "Logistic regression models are neat")
], ["label", "sentence"])

tokenizer = NLTKWordPunctTokenizer(
    inputCol="sentence", outputCol="words",  
    stopwords=set(nltk.corpus.stopwords.words('english')))

tokenizer.transform(sentenceDataFrame).show()

对于自定义Python Estimator,请参阅How to Roll a Custom Estimator in PySpark mllib

⚠此答案取决于内部API,并与Spark 2.0.3,2.1.1,2.2.0或更高版本(SPARK-19348)兼容.有关与以前Spark版本兼容的代码,请参阅revision 8.

标签:apache-spark-ml,python,apache-spark,pyspark,nltk,nltk
来源: https://codeday.me/bug/20190917/1809845.html