我有一个训练有素的 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)