pickle

How to update class attributes with load function

How to update class attributes with load function Question: I have a class in which the method save stores the created object to disk using the pickle package: def save( self, filepath, ): #CHECK ATTRIBUTE VALUES pickle_out = open(filepath+’.pickle’, ‘wb’) pickle.dump(self, pickle_out) pickle_out.close() Similarly, I want to add a load method that loads a pickled …

Total answers: 1

How to serialize custom classes in Python?

How to serialize custom classes in Python? Question: I have a custom class and I want to serialize it for multiprocessing, but pickle and dill doesn’t work fine and loses important data. How can I fix this? My class: import pickle import dill import pandas as pd import numpy as np class C(pd.Series): def __init__(self, …

Total answers: 1

Using external library in PySpark UDF pickle error

Using external library in PySpark UDF pickle error Question: I’m trying the following code: import pandas as pd from pymorphy2 import MorphAnalyzer from pyspark.sql import SparkSession from pyspark.sql import types as T from pyspark.sql import functions as F spark = SparkSession.builder.appName("udf").getOrCreate() def gender(s): m = MorphAnalyzer() return m.parse(s)[0].tag.gender gen = F.udf(gender, T.StringType()) df = spark.createDataFrame(pd.DataFrame({"name": …

Total answers: 2

Picking function to set:get values (and how to pass **kwargs)

Picking function to set:get values (and how to pass **kwargs) Question: I have a program that runs daily where there are a number of objects that a) take a while to create, b) are static for the day once they’re built, so are good candidates to get pickled. To avoid writing this a bunch of …

Total answers: 1

Matplotlib pickle error "TypeError: cannot pickle 'kiwisolver.Solver' object"

Matplotlib pickle error "TypeError: cannot pickle 'kiwisolver.Solver' object" Question: In my project I’m trying to embed the same matplotlib figure in two places and seem to need to make a copy, as I’m using blitting on the original, and embedding a figure in two places & closing one causes a crash issue w/ blitting an …

Total answers: 1

Python error [WinError 6] while using multiprocessing.Process with dill instead of standard Pickle

Python error [WinError 6] while using multiprocessing.Process with dill instead of standard Pickle Question: I have this Process class which overrides standard multiprocessing.Process class in order to catch exceptions occurred in child process. Also I use dill and override multiprocessing standard Pickler because I need to. import dill import traceback import multiprocessing dill.Pickler.dumps, dill.Pickler.loads = …

Total answers: 1

Gradual face_recognition encoding python – Numpy error?

Gradual face_recognition encoding python – Numpy error? Question: I am using the "Extracting features from Face" code from https://www.mygreatlearning.com/blog/face-recognition/ in order to encode facial recognition data in to make an attendance marker. However, this code creates the data from scratch each time. Looping through each file again on every run takes substantial time. For efficiency …

Total answers: 1

Unable to load pickled custom estimator sklearn pipeline

Unable to load pickled custom estimator sklearn pipeline Question: I have a sklearn pipeline that uses custom column transformer, estimator and different lambda functions. Because Pickle cannot serialize the lambda functions, I am using dill. Here is the custom estimator I have: class customOLS(BaseEstimator): def __init__(self, ols): self.estimator_ols = ols def fit(self, X, y): X …

Total answers: 1

Pickle only dumps one value in loop

Pickle only dumps one value in loop Question: I am trying to pickle certain data so I have an easier time retrieving it. My code looks like this: import pickle import networkx as nx import pandas as pd import numpy as np import load_data as load # load the graph g = load.local_data() for node …

Total answers: 1

Unable to load tensorflow model with pickle

Unable to load tensorflow model with pickle Question: I am trying to use pickle for tensorflow models serialization. Here is the code (dump.py) to save the model in a pickle file: import tensorflow as tf import pickle import numpy as np tf.random.set_seed(42) input_x = np.random.randint(0, 50000, (10000,1)) input_y = np.random.randint(0, 50000, (10000,1)) output = input_x …

Total answers: 2