In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 205
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.