<jupyter_start><jupyter_text>Exploring Feature Importance with SHAP<jupyter_code>import pandas as pd
import numpy as np
np.random.seed(0)
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn import preprocessing
from sklearn.ensemble import RandomForestRegressor
from sklearn import metrics
df = pd.read_csv('../data/winequality-red.csv') # Load the data

df.shape
df.columns
# The target variable is 'quality'.
Y = df['quality']
X =  df[['fixed acidity', 'volatile acidity', 'citric acid', 'residual sugar','chlorides', 'free sulfur dioxide', 'total sulfur dioxide', 'density','pH', 'sulphates', 'alcohol']]
# Split the data into train and test data:
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size = 0.2)
# Build the model with the random forest regression algorithm:
model = RandomForestRegressor(max_depth=6, random_state=0, n_estimators=10)
model.fit(X_train, y_train)
import shap
shap_values = shap.TreeExplainer(model).shap_values(X_train)
shap.summary_plot(shap_values, X_train, plot_type="bar")
f = plt.figure()
shap.summary_plot(shap_values, X_train)
f.savefig("../images/shap_summary_plot1.png", bbox_inches='tight', dpi=600)
shap.dependence_plot("alcohol", shap_values, X_train)
shap.dependence_plot("volatile acidity", shap_values, X_train)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11646884272997032, 'fraction_numerical': 0.008160237388724036, 'mean_word_length': 7.37888198757764, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '87209', 'license': 'MIT', 'n_tokens_mistral': 490, 'n_tokens_neox': 464, 'n_words': 117, 'path': 'notebooks/Explaining Wine Quality with SHAP.ipynb', 'repo_name': 'philmui/study-algorithmic-bias', 'size': '1348'}