def s2c_sym_predict_staged_estimator(estimator): ''' sklearn2code adapter ''' try: return comp(s2c_sym_predict(estimator.final_stage_), s2c_sym_transform(estimator)) except: return comp(s2c_sym_predict(estimator.final_stage_), s2c_sym_transform(estimator))
def s2c_sym_transform_staged_estimator(estimator): ''' sklearn2code adapter ''' try: return comp(*map(s2c_sym_transform, estimator.intermediate_stages_)) except: return comp(*map(s2c_sym_transform, estimator.intermediate_stages_))
def sym_predict_ada_boost_regressor(estimator): estimators = estimator.estimators_ weights = tuple(map(RealNumber, estimator.estimator_weights_)) predictors = [sym_predict(est) for est in estimators] predictors = cart(*predictors) Var = VariableFactory() predictions = tuple(Var() for _ in predictors.outputs) return comp( Function(inputs=predictions, calls=tuple(), outputs=sym_weighted_median(predictions, weights)), predictors)
def sym_predict_voting_classifier(estimator): inputs = syms(estimator) voters = cart(*map(sym_predict, estimator.estimators_)) if estimator.weights: weights = tuple(map(RealNumber, estimator.weights)) else: weights = tuple(repeat(RealNumber(1), len(voters.outputs))) Var = VariableFactory(existing=inputs) votes = tuple(Var() for _ in voters.outputs) return comp( sym_inverse_transform(estimator.le_), Function(inputs=votes, calls=tuple(), outputs=sym_weighted_vote(votes, weights)), (voters))
def sym_predict_proba__calibrated_classifier(estimator): if hasattr(estimator.base_estimator, 'decision_function'): inner_pred = sym_decision_function(estimator.base_estimator) elif hasattr(estimator.base_estimator, 'predict_proba'): inner_pred = sym_predict_proba(estimator.base_estimator) # inner_pred = fallback(sym_decision_function, sym_predict_proba)(estimator.base_estimator) pre_result = reduce(__add__, map( sym_predict, estimator.calibrators_)).compose(inner_pred) / RealNumber( len(estimator.calibrators_)) Var = VariableFactory() n_classes = len(estimator.classes_) if n_classes == 2: x = Var() normalize = Function((x, ), tuple(), (RealNumber(1) - x, x)) else: vars_ = tuple(Var() for _ in range(len(pre_result.outputs))) total = Function(vars_, tuple(), (reduce(__add__, vars_), )) t = Var() normalize = Function(vars_, (((t, ), (total, vars_)), ), tuple(v / t for v in vars_)) return comp(normalize, pre_result)
def sym_transform_intermediate_stages(estimator): return comp(*map(compose(sym_transform, tupget(1)), estimator.steps[:-1]))