如何从python3释放python 2中的sklearn.tree.DescisionTreeRegressor

时间:2019-07-18 11:50:15

标签: python-3.x python-2.7 machine-learning scikit-learn data-science

如何从python3释放python 2中的sklearn.tree.DescisionTreeRegressor

  

在Python 3中保存Pickle文件

从joblib导入转储,加载

dump(rfc,'rfc.joblib',protocol = 2)

  

在python 2.7.14中加载Pickle文件

从joblib导入转储,加载

rfc = load('rfc.joblib')

enter image description here

尝试以下方法,它给了我类而不是模型

from lib2to3.fixes.fix_imports import MAPPING
REVERSE_MAPPING={}
for key,val in MAPPING.items():
    REVERSE_MAPPING[val]=key

class Python_3_Unpickler(pickle.Unpickler):
    """Class for pickling objects from Python 3"""
    def find_class(self,module,name):
        if module in REVERSE_MAPPING:
            module=REVERSE_MAPPING[module]
        import(module)
        mod = sys.modules[module]
        klass = getattr(mod, name)
        return klass

    def loads(str):
        file = pickle.StringIO(str)
        return Python_3_Unpickler(file).load()


with open('JULY_18_rfc_python_2.pkl', 'rb') as file_:
    rfc = Python_3_Unpickler(file_).load() 

0 个答案:

没有答案