How can I view the model of the output more easily? Small model output directly look at the XML text is good, if the data compare more processing XML is not very convenient, json better with Python,
But it's not very convenient to convert to a JSON map because you have to follow the key to access the string type is not automatically prompted
??
In [6]: Import JSON
??
In [7]: M = json.loads (Open ('./model.json '). ReadLine ())
??
In [8]: M.keys ()
OUT[8]: [u ' boost_serialization ']
??
In [9]: m[' boost_serialization '].keys ()
OUT[9]: [u ' @version ', U ' @signature ', U ' data ']
??
In []: m[' boost_serialization ' [' Data '] [' _trees '] [' Item '][0].keys ()
OUT[18]:
[u ' _gainpvalue ',
U ' @tracking_level ',
U ' @class_id ',
U ' _ltechild ',
U ' _gtchild ',
U ' _maxoutput ',
U ' _leafvalue ',
U ' numleaves ',
U ' _splitgain ',
U ' _splitfeature ',
U ' _previousleafvalue ',
U ' _threshold ',
U ' @version ',
U ' _weight ')
??
in [+]: m[' boost_serialization ' [' Data '] [' _trees '] [' Item '][0][' _splitgain '] [' Item '][10]
OUT[19]: U ' 3.89894126598927926e+00 '
??
Since the python prompt is not prompted as private by default, it has been modified
#include "conf_util.h"
#include <boost/serialization/nvp.hpp>
#define GEZI_SERIALIZATION_NVP (name)
BOOST::SERIALIZATION::MAKE_NVP (Gezi::conf_trim (#name). C_STR (), name)
??
This shows that Gainpvalue is not the beginning of _
??
Using Python's introspection function, dict data can be parsed by JSON, and string as key is converted into a Python object for easy access to the following
def H2O (x):
If Isinstance (x, Dict):
return type (' Jo ', (), {K:H2O (v) for K, V in X.iteritems ()})
Elif isinstance (x, List):
L = [H2O (item) for item in X]
return L
Else
return x
??
def H2O2 (x):
If Isinstance (x, Dict):
return type (' Jo ', (), {K:h2o2 (v) for K, V in X.iteritems ()})
Elif isinstance (x, List):
return type (' Jo ', (), {"I" + str (IDX): H2O2 (val) for Idx, Val in enumerate (x)})
return L
Else
return x
??
def xmlfile2obj (path):
Import Xmltodict
Doc = xmltodict.parse (open (path), process_namespaces=true)
Return H2O (DOC)
??
def xmlfile2obj2 (path):
Import Xmltodict
Doc = xmltodict.parse (open (path), process_namespaces=true)
Return H2O2 (DOC)
??
This can be done directly with the serialized XML file using M = xmlfile2obj (' *.xml ') or M = Xml2obj2 (' *.xml ')
The recommendation is to use the first, which is the standard conversion, to provide the second interface is primarily Python's automatic hint for list item no, only Dir () to view:
The second one turns [3] this way. I3 that is, all lists are removed with Dict.
??
m = Xmlfile2obj ('./model.xml ')
In []: m.boost_serialization.data.trees.item[0].splitgain.item[13]
OUT[14]: U ' 3.26213753939964946e+00 '
??
m = Xmlfile2obj2 ('./model.xml ')
in [+]: m.boost_serialization.data.trees.item.i0.splitgain.item.i13
OUT[16]: U ' 3.26213753939964946e+00 '
??
??
??
??
??