<jupyter_start><jupyter_text>SHAP Tutorial Dataset: https://www.kaggle.com/uciml/mushroom-classification<jupyter_code>#imports
import pandas as pd
import xgboost as xgb

import shap

from xverse.transformer import WOE
from sklearn.metrics import accuracy_score,confusion_matrix<jupyter_output><empty_output><jupyter_text>Dataset<jupyter_code>["Sex","Length","Diameter","Height","Whole weight","Shucked weight","Viscera weight","Shell weight","Rings"
data = pd.read_csv("../data/abalone.data",
                  header=)
print(len(data))
data.head()<jupyter_output>4176<jupyter_text>Data Transformation<jupyter_code>y = data['class']
X = data.drop('class', axis=1)
feature_names = X.columns
X
y = y.astype('category').cat.codes

for feature in X.columns: 
    X[feature] = X[feature].astype('category')
    X[feature] = X[feature].cat.codes
X
from sklearn.preprocessing import OneHotEncoder

enc = OneHotEncoder(drop='if_binary')
enc.fit(X)

X_encoded = enc.transform(X).toarray()

X = pd.DataFrame(X_encoded, columns= enc.get_feature_names(feature_names))

X
enc.get_feature_names(feature_names)
clf = WOE()

clf.fit(X, y)
X= clf.transform(X)
X
## Train Model
model = xgb.XGBClassifier(objective="binary:logistic", random_state=42)
model.fit(X, y)

y_pred = model.predict(X)

print(confusion_matrix(y, y_pred))
accuracy_score(y, y_pred)
explainer = shap.Explainer(model)
shap_values = explainer(X)

# visualize the first prediction's explanation
shap.plots.waterfall(shap_values[1])
shap.plots.force(shap_values[0])
#shap.initjs()
shap.plots.force(shap_values[0:100])
shap.plots.scatter(shap_values[:,"odor_n"], color=shap_values)
shap.plots.beeswarm(shap_values)
shap.plots.bar(shap_values)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14294015107495642, 'fraction_numerical': 0.007553747821034283, 'mean_word_length': 7.278846153846154, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, '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': '324126', 'license': 'MIT', 'n_tokens_mistral': 675, 'n_tokens_neox': 649, 'n_words': 102, 'path': 'src/archive/shap_tutorial (mushrooms).ipynb', 'repo_name': 'conorosully/medium-articles', 'size': '1721'}