示例#1
0
 def _nyoka_pmml_model(
     self, simple_pmml_ruleset_model: models.SimplePMMLRuleSetModel
 ) -> nyoka_pmml.PMML:
     timestamp = datetime.datetime.now(
     ) if self._timestamp is None else self._timestamp
     return nyoka_pmml.PMML(
         version=nyoka_constants.PMML_SCHEMA.VERSION,
         Header=nyoka_pmml.Header(
             copyright=NyokaSerializer.COPYRIGHT_STRING,
             description=nyoka_constants.HEADER_INFO.DEFAULT_DESCRIPTION,
             Timestamp=nyoka_pmml.Timestamp(timestamp),
             Application=nyoka_pmml.Application(
                 name=NyokaSerializer.APPLICATION_NAME,
                 version=version.version)),
         DataDictionary=None
         if simple_pmml_ruleset_model.dataDictionary is None else
         self._nyoka_data_dictionary(
             simple_pmml_ruleset_model.dataDictionary),
         RuleSetModel=None
         if simple_pmml_ruleset_model.ruleSetModel is None else [
             self._nyoka_rule_set_model(
                 simple_pmml_ruleset_model.ruleSetModel)
         ])
示例#2
0
def lgb_to_pmml(pipeline,
                col_names,
                target_name,
                pmml_f_name='from_lgbm.pmml',
                model_name=None,
                description=None):
    """
    Exports LGBM pipeline object into pmml

    Parameters
    ----------
    pipeline :
        Contains an instance of Pipeline with preprocessing and final estimator
    col_names : List
        Contains list of feature/column names.
    target_name : String
        Name of the target column.
    pmml_f_name : String
        Name of the pmml file. (Default='from_lgbm.pmml')
    model_name : string (optional)
        Name of the model
    description : string (optional)
        Description of the model

    Returns
    -------
    Exports the generated PMML object to `pmml_f_name`

    """
    try:
        model = pipeline.steps[-1][1]
    except:
        raise TypeError(
            "Exporter expects pipeleine_instance and not an estimator_instance"
        )
    else:
        if col_names.__class__.__name__ == "ndarray":
            col_names = col_names.tolist()
        ppln_sans_predictor = pipeline.steps[:-1]
        trfm_dict_kwargs = dict()
        derived_col_names = col_names
        categoric_values = tuple()
        mining_imp_val = tuple()
        if ppln_sans_predictor:
            pml_pp = pp.get_preprocess_val(ppln_sans_predictor, col_names,
                                           model)
            trfm_dict_kwargs['TransformationDictionary'] = pml_pp['trfm_dict']
            derived_col_names = pml_pp['derived_col_names']
            col_names = pml_pp['preprocessed_col_names']
            categoric_values = pml_pp['categorical_feat_values']
            mining_imp_val = pml_pp['mining_imp_values']
        PMML_kwargs = get_PMML_kwargs(model, derived_col_names, col_names,
                                      target_name, mining_imp_val,
                                      categoric_values, model_name)
        pmml = pml.PMML(version=PMML_SCHEMA.VERSION,
                        Header=sklToPmml.get_header(description),
                        DataDictionary=sklToPmml.get_data_dictionary(
                            model, col_names, target_name, categoric_values),
                        **trfm_dict_kwargs,
                        **PMML_kwargs)
        pmml.export(outfile=open(pmml_f_name, "w"), level=0)