Save custom transformers in pyspark

前端 未结 1 1588
走了就别回头了
走了就别回头了 2021-01-15 11:27

When I implement this part of this python code in Azure Databricks:

class clustomTransformations(Transformer):
    

custom_transformer = customT         


        
1条回答
  •  被撕碎了的回忆
    2021-01-15 11:44

    It seems like there is no easy workaround but to try and implement the _to_java method, as is suggested here for StopWordsRemover: Serialize a custom transformer using python to be used within a Pyspark ML pipeline

    def _to_java(self):
        """
        Convert this instance to a dill dump, then to a list of strings with the unicode integer values of each character.
        Use this list as a set of dumby stopwords and store in a StopWordsRemover instance
        :return: Java object equivalent to this instance.
        """
        dmp = dill.dumps(self)
        pylist = [str(ord(d)) for d in dmp] # convert byes to string integer list
        pylist.append(PysparkObjId._getPyObjId()) # add our id so PysparkPipelineWrapper can id us.
        sc = SparkContext._active_spark_context
        java_class = sc._gateway.jvm.java.lang.String
        java_array = sc._gateway.new_array(java_class, len(pylist))
        for i in xrange(len(pylist)):
            java_array[i] = pylist[i]
        _java_obj = JavaParams._new_java_obj(PysparkObjId._getCarrierClass(javaName=True), self.uid)
        _java_obj.setStopWords(java_array)
        return _java_obj
    

    0 讨论(0)
提交回复
热议问题