小编P0i*_*MaN的帖子

将 sklearn.svm.SVR 模型保存为 JSON 而不是 pickling

我有一个训练有素的 SVR 模型,需要以JSON格式保存,而不是腌制。

JSON 化训练模型背后的想法是简单地捕获权重和其他“拟合”属性的状态。然后,我可以稍后设置这些属性来进行预测。这是我所做的一个实现:

    # assume SVR has been trained
    regressor = SVR()
    regressor.fit(x_train, y_train)

    # saving the regressor params in a JSON file for later retrieval
    with open(f'saved_regressor_params.json', 'w', encoding='utf-8') as outfile:
        json.dump(regressor.get_params(), outfile)

    # finding the fitted attributes of SVR()
    # if an attribute is trailed by '_', it's a fitted attribute
    attrs = [i for i in dir(regressor) if i.endswith('_') and not i.endswith('__')]
    remove_list = ['coef_', '_repr_html_', '_repr_mimebundle_'] # unnecessary attributes
    
    for attr in remove_list: …
Run Code Online (Sandbox Code Playgroud)

machine-learning svm scikit-learn

5
推荐指数
1
解决办法
748
查看次数

标签 统计

machine-learning ×1

scikit-learn ×1

svm ×1