01. Basic example

Description

 7 # Libraries
 8 import json
 9 import numpy as np
10
11 # Jsonpickle
12 import jsonpickle
13 import jsonpickle.ext.numpy as jsonpickle_numpy
14
15 # Configure jsonpickle
16 jsonpickle.set_preferred_backend('json')
17 jsonpickle_numpy.register_handlers()
18
19
20 # -------------------
21 # Main
22 # -------------------
23 """
24 In order to test whether the jsonpickle encode method saves
25 the estimators so they can be retrieved no matter the numpy
26 version. Note that previously using pickle old estimators
27 could not be loaded because of numpy issues.
28
29 At the moment I have tested using the system (no virtual
30 environment) using the following...
31
32     $ py -m pip install numpy==1.18
33     $ py main.py
34     $ py -m pip install numpy==1.19.1
35     $ py main.py
36
37 And it works normally.
38
39 .. todo: Test it through a venvpy28-jsonpickle.
40 """
41
42 # Path
43 path =  './estimator_00.json'
44
45 # Read
46 with open(path) as json_file:
47     d = json.load(json_file)
48
49 # Show information
50 #print(d['iteration_00'])
51
52 # Show information
53 print("\nUsing numpy... <%s>" % np.version.version)
54
55 # Load
56 estimator = jsonpickle.decode(d['iteration_00'])
57
58 # Show
59 print("\nEstimator loaded. \n   %s" % estimator)
60 print("\nPredictions completed. \n   %s" % \
61       estimator.predict_proba(np.arange(6).reshape(1,-1)))

Total running time of the script: ( 0 minutes 0.000 seconds)

Gallery generated by Sphinx-Gallery