<jupyter_start><jupyter_text>Python HANA ML API Using Composite Variables in Predictive Model.   Declare Training Data<jupyter_code># Connect using the HANA secure user store 
from hana_ml import dataframe as hd
conn = hd.ConnectionContext(userkey='MLMDA_KEY')
# Get Training Data
sql_cmd = 'SELECT * FROM "APL_SAMPLES"."AUTO_CLAIMS_FRAUD" ORDER BY CLAIM_ID'
training_data = hd.DataFrame(conn, sql_cmd)
training_data.head(8).collect()<jupyter_output><empty_output><jupyter_text>Extend AutoClassifier by overloading the method for roles<jupyter_code>from hana_ml.algorithms.apl.classification import AutoClassifier
from hana_ml.algorithms.apl.apl_base import APLArtifactTable

class AutoClassifier_ExplicitCompositeRoles(AutoClassifier):
    def _create_var_roles_table(self, 
                                data,
                                key,
                                label,
                                features):
        if getattr(self, 'composite_roles', None):
            return APLArtifactTable(
                name='#VARIABLE_ROLES_{}'.format(self.id),
                type_name=APLArtifactTable.VARIABLE_ROLES_WITH_COMPOSITES_OID,
                apl_version=self._apl_version,
                data=self.composite_roles)
        return super(AutoClassifier, self)._create_var_roles_table(data, key, label, features)<jupyter_output><empty_output><jupyter_text>Build the model<jupyter_code># Create Model  
model = AutoClassifier_ExplicitCompositeRoles(conn_context=conn, variable_auto_selection = True)
setattr(model, 'composite_roles',
        (
            ('GENDER_AGE', 'input', 'cross1', 'GENDER', None),
            ('GENDER_AGE', 'input', 'cross2', 'AGE', None),
            ('CLAIM_ID', 'skip',  None, None, None),
            ('IS_FRAUD', 'target', None, None, None)
        ))
# Train the model
model.set_params(cutting_strategy = 'random with no test')
model.fit(training_data, label='IS_FRAUD', key='CLAIM_ID')
# Debrief the trained model 
import pandas as pd
print('\r\n\x1b[1m'+ 'MODEL PERFORMANCE' + '\x1b[0m')
d = model.get_performance_metrics()
df = pd.DataFrame(list(d.items()), columns=["Metric", "Value"])
df.loc[df.Metric == 'AUC'].style.hide_index()
print('\r\n\x1b[1m'+ 'VARIABLES IMPORTANCE' + '\x1b[0m')
d = model.get_feature_importances() 
df = pd.DataFrame(list(d.items()), columns=["Variable", "Contribution"])
df['Contribution'] = df['Contribution'].astype(float)
df['Cumulative'] = df['Contribution'].cumsum()
df['Contribution'] = df['Contribution'].round(4)*100
df['Cumulative'] = df['Cumulative'].round(4)*100
non_zero = df['Contribution'] != 0
dfs = df[non_zero].sort_values(by=['Contribution'], ascending=False)
dfs.style.hide_index()<jupyter_output>[1mVARIABLES IMPORTANCE[0m
{'fraction_non_alphanumeric': 0.13070463672873311, 'fraction_numerical': 0.008032128514056224, 'mean_word_length': 4.111940298507463, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '479579', 'license': 'BSD-Source-Code', 'n_tokens_mistral': 955, 'n_tokens_neox': 917, 'n_words': 167, 'path': 'Python-API/apl/notebooks/HANA_ML_APL_RR_COMPOSITE.ipynb', 'repo_name': 'rschop/hana-ml-samples', 'size': '2739'}