gpt4 book ai didi

python - 更改已保存 pickle 的导入库

转载 作者:行者123 更新时间:2023-12-05 04:23:52 27 4
gpt4 key购买 nike

前段时间我将一个类函数保存到 pickle 文件中。

<library.module.Class at 0x1c926b2e520>

该类导入了两个同时更改了名称的库。

是否可以编辑 pickle 文件,以便我可以更新这 2 个新导入而无需重新生成 pickle?

谢谢!问候

编辑:

这是我加载 pickle 的方式:

import pickle
model_path = os.getenv('MODELS_FOLDER') + 'model_20210130.pkl'
model = pickle.load(open(model, 'rb'))

这是pickle 类的内容。我要更新的两个库已精确定位。

**import socceraction.spadl.config** as spadlconfig
**from socceraction.spadl.base import** SPADLSchema

class ExpectedThreat:
"""An implementation of the model.

"""

def __init__(self):
...


def __solve(
self) -> None:


def fit(self, actions: DataFrame[SPADLSchema]) -> 'ExpectedThreat':
"""Fits the xT model with the given actions."""



def predict(
self, actions: DataFrame[SPADLSchema], use_interpolation: bool = False
) -> np.ndarray:
"""Predicts the model values for the given actions.


最佳答案

我认为你做不到。

Pickled 对象是序列化的内容,为了被修改,必须适当反序列化。

为什么不能直接加载、更改和覆盖它?

您仍然可以用新函数覆盖属性等函数:


# import new libraries
import new_socceraction.spadl.config as new_spadlconfig
from new_socceraction.spadl.base import new_SPADLSchema

import pickle

model_path = os.getenv('MODELS_FOLDER') + 'model_20210130.pkl'

with open(model_path, 'rb') as file:
model = pickle.load(file)

# define new methods with different libraries

def fit(self, actions: DataFrame[new_SPADLSchema]) -> 'ExpectedThreat':
"""Fits the xT model with the given actions."""
pass # your new implementation

def predict(
self, actions: DataFrame[new_SPADLSchema], use_interpolation: bool = False
) -> np.ndarray:
"""Predicts the model values for the given actions."""
pass # your new implementation


# overwrite new methods
model.fit = fit
model.predict = predict

# save it again
with open(model_path, 'wb') as file:
pickle.dump(file)
...

关于python - 更改已保存 pickle 的导入库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73635328/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com