Save custom transformers in pyspark

血红的双手。 提交于 2019-12-01 11:55:36
dportman

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!