Get the App
SLTechnology News&Howtos  ›  Development  › 

Python explains how the model library Shap realizes the visualization of machine learning model output

Shulou Source: shulou.com Published: 2022-06-02 12:08:39 10月03日 Update

This article mainly explains "python explains how the model library Shap realizes the visualization of machine learning model output". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "python explains how the model library Shap realizes the visualization of machine learning model output".

Install the required libraries

Start installing Shap using pip. The commands given below can do this.

Pip install shap imports required libraries

In this step, we will import the libraries needed to load the data, create the model, and create the visualization of the model.

Df = pd.read_csv ('/ content/Diabetes.csv') features = ['Pregnancies',' Glucose','BloodPressure','SkinThickness','Insulin','BMI','DiabetesPedigreeFunction','Age'] Y = df ['Outcome'] X = DF [features] X_train, X_test, Y_train, Y_test = train_test_split (X, Y, test_size = 0.2, random_state= 1234) xgb_model = xgb.XGBRegressor (random_state=42) xgb_model.fit (X_train Y_train) create a model

In this step, we will create a machine learning model. In this article, I will create a XGBoost model, but you can choose any model. The dataset we will use for this model is the famous diabetes dataset, which can be downloaded from Kaggle.

Df = pd.read_csv ('/ content/Diabetes.csv') features = ['Pregnancies',' Glucose','BloodPressure','SkinThickness','Insulin','BMI','DiabetesPedigreeFunction','Age'] Y = df ['Outcome'] X = DF [features] X_train, X_test, Y_train, Y_test = train_test_split (X, Y, test_size = 0.2, random_state= 1234) xgb_model = xgb.XGBRegressor (random_state=42) xgb_model.fit (X_train Y_train)

Create Visualization

Now we will create an interpreter for shap, find out the shape values of the model, and use them to create visualization.

Explainer = shap.Explainer (xgb_model) shap_values = explainer (X_test) 1, Bar Plotshap.plots.bar (shap_values, max_display=10)

2. Queue diagram shap.plots.bar (shap_values.cohorts (2). Abs.mean (0))

3. Heat map shap.plots.heatmap (shap_values [1 100])

4. Waterfall shap.plots.waterfall (shap_values [0]) # For the first observation

5. Try to shap.initjs () explainer = shap.TreeExplainer (xgb_model) shap_values = explainer.shap_values (X_test) def p (j): return (shap.force_plot (explainer.expected_value, shap_values [JP:], X_test.iloc [JL:])) p (0)

6. Decision graph shap_values = explainer.shap_values (X_test) [1] print ("The expected value is", expected_value) print ("The final prediction is", xgb_model.predict (X_test) [1]) shap.decision_plot (expected_value, shap_values, X_test)

At this point, I believe you have a deeper understanding of "python explains how the model library Shap realizes the visualization of machine learning model output". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Model visualization learning machine interpretation output data content practical deeper famous interest command practical practical simple operation effect method more friends Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Microsoft Huawei Docker vpn