<jupyter_start><jupyter_text>Load Trained Model From Pickle<jupyter_code>import numpy as np
import pandas as pd
import warnings
from sklearn.exceptions import DataConversionWarning
warnings.filterwarnings(action='ignore', category=DataConversionWarning)
import joblib
create_model=None
trained_model = joblib.load('models/LightGBM.pkl')
columns = joblib.load('models/columns.pkl')<jupyter_output><empty_output><jupyter_text>Predict Category For Sample Data<jupyter_code>data = pd.get_dummies(pd.DataFrame({'age':43,
                                    'workclass':'Private',
                                    'fnlwgt':100000,
                                    'education':'Bachelors',
                                    'education-num':13,
                                    'marital-status':'Married-civ-spouse',
                                    'occupation':'Sales',
                                    'relationship':'Husband',
                                    'race':'White',
                                    'sex':'Male',
                                    'capital-gain':0,
                                    'capital-loss':0,
                                    'hours-per-week':40,
                                    'native-country':'Spain'}, index=[0]))
data = data.reindex(columns=columns, fill_value=0)
trained_model.predict(data)<jupyter_output><empty_output><jupyter_text>Explain Weights and Prediction Using ELI5 Split the pipeline between estimator (last step) and transformer (all steps bar last). Then get the transformed data and columns<jupyter_code>from sklearn.pipeline import Pipeline

estimator = trained_model
transformed_data = data
feature_names = columns
if type(trained_model) == Pipeline:
    if len(trained_model.steps) == 1:
        estimator = trained_model.steps[0][1]
    else:
        estimator = trained_model.steps[-1][1]
        transformer = Pipeline(trained_model.steps[:-1])
        transformed_data = transformer.transform(data)
        if 'select' in transformer.named_steps:            
            feature_names = data.columns[transformer.named_steps.select.get_support()].tolist()
import eli5
eli5.show_weights(estimator, feature_names=feature_names, top=None, show=eli5.formatters.fields.ALL)
eli5.show_prediction(estimator, transformed_data[0], feature_names=feature_names, 
                     top=None, show=eli5.formatters.fields.ALL)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10020533880903491, 'fraction_numerical': 0.01190965092402464, 'mean_word_length': 2.305291723202171, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '491743', 'license': 'MIT', 'n_tokens_mistral': 705, 'n_tokens_neox': 658, 'n_words': 116, 'path': 'Census Income Prediction.ipynb', 'repo_name': 'jgbustos/ml-demos', 'size': '2435'}