-
Notifications
You must be signed in to change notification settings - Fork 0
/
pickle.py
105 lines (75 loc) · 2.88 KB
/
pickle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
Here’s how you can serialize a Betacal model using each of the alternative methods:
Using hickle
import hickle as hkl
from betacal import BetaCalibrator
# Example: Create and train a BetaCalibrator model
model = BetaCalibrator()
# Assume you have `predictions` and `labels` for calibration
# model.fit(predictions, labels)
# Save the model
hkl.dump(model, 'betacal_model.h5')
# Load the model
loaded_model = hkl.load('betacal_model.h5')
Using quickle
import quickle
from betacal import BetaCalibrator
# Example: Create and train a BetaCalibrator model
model = BetaCalibrator()
# Assume you have `predictions` and `labels` for calibration
# model.fit(predictions, labels)
# Save the model
with open('betacal_model.qkl', 'wb') as f:
quickle.dump(model, f)
# Load the model
with open('betacal_model.qkl', 'rb') as f:
loaded_model = quickle.load(f)
Using JSON
For JSON, you need to convert the model’s parameters into a serializable format since JSON doesn’t support arbitrary Python objects.
import json
from betacal import BetaCalibrator
# Example: Create and train a BetaCalibrator model
model = BetaCalibrator()
# Assume you have `predictions` and `labels` for calibration
# model.fit(predictions, labels)
# Serialize model parameters to JSON
model_data = {'alpha': model.alpha, 'beta': model.beta}
with open('betacal_model.json', 'w') as f:
json.dump(model_data, f)
# Load the model parameters from JSON
with open('betacal_model.json', 'r') as f:
model_data = json.load(f)
# Recreate the model
loaded_model = BetaCalibrator()
loaded_model.alpha = model_data['alpha']
loaded_model.beta = model_data['beta']
Using dill
import dill
from betacal import BetaCalibrator
# Example: Create and train a BetaCalibrator model
model = BetaCalibrator()
# Assume you have `predictions` and `labels` for calibration
# model.fit(predictions, labels)
# Save the model
with open('betacal_model.dill', 'wb') as f:
dill.dump(model, f)
# Load the model
with open('betacal_model.dill', 'rb') as f:
loaded_model = dill.load(f)
Using joblib
import joblib
from betacal import BetaCalibrator
# Example: Create and train a BetaCalibrator model
model = BetaCalibrator()
# Assume you have `predictions` and `labels` for calibration
# model.fit(predictions, labels)
# Save the model
joblib.dump(model, 'betacal_model.joblib')
# Load the model
loaded_model = joblib.load('betacal_model.joblib')
Recommendations
• Use joblib for machine learning models as it is optimized for numerical data and arrays.
• Use hickle for faster serialization and built-in compression.
• Use quickle for better performance and security than pickle.
• Use dill if you need to serialize functions or more complex objects.
• Use JSON if interoperability and human-readability are important, though it requires custom handling for complex objects like the Betacal model.
Let me know if you want a specific one explained further!