<jupyter_start><jupyter_text>LightGBM  Single Prediction<jupyter_code>from backend.api.prediction import initialize_pipeline
config_path = "/home/joseph/Coding/ml_projects/earthquake_forecasting/backend/config.yml"
lgb_pipeline = initialize_pipeline(config_path, "lightgbm")
lgb_pipeline
import pandas as pd
data = {
                    "building_id": 802906,
                    "geo_level_1_id": 6,
                    "geo_level_2_id": 487,
                    "geo_level_3_id": 12198,
                    "count_floors_pre_eq": 2,
                    "age": 30,
                    "area_percentage": 6,
                    "height_percentage": 5,
                    "land_surface_condition": "t",
                    "foundation_type": "r",
                    "roof_type": "n",
                    "ground_floor_type": "f",
                    "other_floor_type": "q",
                    "position": "t",
                    "plan_configuration": "d",
                    "has_superstructure_adobe_mud": 1,
                    "has_superstructure_mud_mortar_stone": 1,
                    "has_superstructure_stone_flag": 0,
                    "has_superstructure_cement_mortar_stone": 0,
                    "has_superstructure_mud_mortar_brick": 0,
                    "has_superstructure_cement_mortar_brick": 0,
                    "has_superstructure_timber": 0,
                    "has_superstructure_bamboo": 0,
                    "has_superstructure_rc_non_engineered": 0,
                    "has_superstructure_rc_engineered": 0,
                    "has_superstructure_other": 0,
                    "legal_ownership_status": "v",
                    "count_families": 1,
                    "has_secondary_use": 0,
                    "has_secondary_use_agriculture": 0,
                    "has_secondary_use_hotel": 0,
                    "has_secondary_use_rental": 0,
                    "has_secondary_use_institution": 0,
                    "has_secondary_use_school": 0,
                    "has_secondary_use_industry": 0,
                    "has_secondary_use_health_post": 0,
                    "has_secondary_use_gov_office": 0,
                    "has_secondary_use_use_police": 0,
                    "has_secondary_use_other": 0,
                }
df = pd.DataFrame(data, index=[0])
df.head()
out = lgb_pipeline.predict(df)
out
import shap
# explain the model's predictions using SHAP
# (same syntax works for LightGBM, CatBoost, scikit-learn, transformers, Spark, etc.)
explainer = shap.TreeExplainer(lgb_pipeline.model.model)
df: pd.DataFrame = lgb_pipeline.encoder.replace_with_new_embeds(df, batch_size=1)
if "building_id" in df.columns:
    df = df.drop(["building_id"], axis=1)

if "Unnamed: 0" in df.columns:
    df = df.drop(["Unnamed: 0"], axis=1)

df.head()
lgb_pipeline.model.model.params["objective"] = "multiclass"
shap_values = explainer(df)
shap_values
shap.initjs()
shap.force_plot(explainer.expected_value[1], shap_values.data[0,:], df.iloc[0,:])
shap.summary_plot(shap_values.data, df)
df.columns
feat_importances = lgb_pipeline.model.model.feature_importance()
importances = {col:weight for (col, weight) in zip(df.columns, feat_importances)}
importances
shap.dependence_plot("has_superstructure_adobe_mud", shap_values, df)
shap.summary_plot(shap_values, df)
shap.plots.bar(shap_values)
# visualize the first prediction's explanation
shap.plots.waterfall(shap_values[0])<jupyter_output><empty_output><jupyter_text>With Whole Dataset<jupyter_code>from backend.api.prediction import initialize_pipeline
config_path = "/home/joseph/Coding/ml_projects/earthquake_forecasting/backend/config.yml"
lgb_pipeline = initialize_pipeline(config_path, "lightgbm")
lgb_pipeline
import pandas as pd
df = pd.read_csv("~/datasets/earthquake_damage_forecasting/train_data_embeds.csv").drop(["Unnamed: 0", "building_id"], axis=1)
df.info()
import shap
# explain the model's predictions using SHAP
# (same syntax works for LightGBM, CatBoost, scikit-learn, transformers, Spark, etc.)
explainer = shap.TreeExplainer(lgb_pipeline.model.model)
explainer
lgb_pipeline.model.model.params["objective"] = "multiclass"
shap_values = explainer(df.iloc[:50])
shap_values
shap.initjs()
shap.force_plot(explainer.expected_value[1], shap_values.data[1,:], df.iloc[1,:])
shap.summary_plot(shap_values.data, df.iloc[:50])
shap.plots.bar(shap_values[1])
help(explainer)
shap_values[0][0]
# shap_values.values = shap_values.values[:,:,1]
# shap_values.base_values = shap_values.base_values[:,1]

shap.plots.waterfall(explainer.expected_value[0], shap_values[0], df.iloc[0])<jupyter_output><empty_output><jupyter_text>Catboost<jupyter_code>from backend.api.prediction import initialize_pipeline
config_path = "/home/joseph/Coding/ml_projects/earthquake_forecasting/backend/config.yml"
lgb_pipeline = initialize_pipeline(config_path, "catboost")
lgb_pipeline<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11181262729124236, 'fraction_numerical': 0.014867617107942974, 'mean_word_length': 3.123425692695214, 'pattern_counts': {'":': 39, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '5786', 'license': 'MIT', 'n_tokens_mistral': 1716, 'n_tokens_neox': 1609, 'n_words': 250, 'path': 'notebooks/SHAP Values.ipynb', 'repo_name': 'jchen42703/earthquake_forecasting', 'size': '4910'}