Example #1
0
 def __setstate__(self, state):
     import tempfile
     with tempfile.NamedTemporaryFile(delete=False) as tf:
         tf.write(state["so_f"])
     self._n_features = state["n_features"]
     self._so_f = tf.name
     self._evaluator = _compiled.CompiledPredictor(
         tf.name.encode("ascii"), cg.EVALUATE_FN_NAME.encode("ascii"))
 def __setstate__(self, state):
     import tempfile
     self._so_f_object = tempfile.NamedTemporaryFile(mode='w+b', prefix='compiledtrees_', suffix='.so', delete=True)
     self._so_f_object.write(state["so_f"])
     self._so_f_object.flush()
     self._so_f = self._so_f_object.name
     self._n_features = state["n_features"]
     self._evaluator = _compiled.CompiledPredictor(
         self._so_f_object.name.encode("ascii"),
         cg.EVALUATE_FN_NAME.encode("ascii"))
    def _build(cls, clf, n_jobs=1):
        if not cls.compilable(clf):
            raise ValueError("Predictor {} cannot be compiled".format(
                clf.__class__.__name__))

        files = None
        n_features = None
        if isinstance(clf, DecisionTreeRegressor):
            n_features = clf.n_features_
            files = cg.code_gen_regressor_tree(tree=clf.tree_)

        if isinstance(clf, GradientBoostingRegressor):
            n_features = clf.n_features_

            # hack to get the initial (prior) on the decision tree.
            if hasattr(clf, '_raw_predict_init'):
                initial_value = clf._raw_predict_init(
                    np.zeros(shape=(1, n_features))).item((0, 0))
            else:
                # older scikit-learn
                initial_value = clf._init_decision_function(
                    np.zeros(shape=(1, n_features))).item((0, 0))

            files = cg.code_gen_ensemble_regressor(
                trees=[e.tree_ for e in clf.estimators_.flat],
                individual_learner_weight=clf.learning_rate,
                initial_value=initial_value,
                n_jobs=n_jobs)

        if isinstance(clf, ForestRegressor):
            n_features = clf.n_features_

            files = cg.code_gen_ensemble_regressor(
                trees=[e.tree_ for e in clf.estimators_],
                individual_learner_weight=1.0 / clf.n_estimators,
                initial_value=0.0,
                n_jobs=n_jobs)

        assert n_features is not None
        assert files is not None

        so_f = cg.compile_code_to_object(files, n_jobs=n_jobs)
        evaluator = _compiled.CompiledPredictor(
            so_f.name.encode("ascii"), cg.EVALUATE_FN_NAME.encode("ascii"))
        return n_features, evaluator, so_f
 def __setstate__(self, state):
     import tempfile
     self._so_f_object = tempfile.NamedTemporaryFile(
         mode='w+b',
         prefix='compiledtrees_',
         suffix='.so',
         delete=delete_files)
     if isinstance(state["so_f"], six.text_type):
         state["so_f"] = state["so_f"].encode('latin1')
     self._so_f_object.write(state["so_f"])
     self._so_f_object.flush()
     self._so_f = self._so_f_object.name
     if platform.system() == 'Windows':
         self._so_f_object.close()
     self._n_features = state["n_features"]
     self._evaluator = _compiled.CompiledPredictor(
         self._so_f_object.name.encode("ascii"),
         cg.EVALUATE_FN_NAME.encode("ascii"))
Example #5
0
    def _build(cls, clf):
        if not cls.compilable(clf):
            raise ValueError("Predictor {} cannot be compiled".format(
                clf.__class__.__name__))

        lines = None
        n_features = None
        if isinstance(clf, DecisionTreeRegressor):
            n_features = clf.n_features_
            lines = cg.code_gen_tree(tree=clf.tree_)

        if isinstance(clf, GradientBoostingRegressor):
            n_features = clf.n_features

            # hack to get the initial (prior) on the decision tree.
            initial_value = clf._init_decision_function(
                np.zeros(shape=(1, n_features))).item((0, 0))

            lines = cg.code_gen_ensemble(
                trees=[e.tree_ for e in clf.estimators_.flat],
                individual_learner_weight=clf.learning_rate,
                initial_value=initial_value)

        if isinstance(clf, ForestRegressor):
            n_features = clf.n_features_
            lines = cg.code_gen_ensemble(
                trees=[e.tree_ for e in clf.estimators_],
                individual_learner_weight=1.0 / clf.n_estimators,
                initial_value=0.0)

        assert n_features is not None
        assert lines is not None

        so_f = cg.compile_code_to_object("\n".join(lines))
        return n_features, _compiled.CompiledPredictor(
            so_f.encode("ascii"), cg.EVALUATE_FN_NAME.encode("ascii"))
 def __setstate__(self, state):
     super(CompiledRegressionPredictor, self).__setstate__(state)
     self._evaluator = _compiled.CompiledPredictor(
         self._so_f_object.name.encode("ascii"),
         cg.EVALUATE_FN_NAME.encode("ascii"))