How to obtain features' weights
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Understanding the weight or importance of features in a model is crucial for interpreting the model's behavior and improving its performance. Feature weights, also known as feature importances, provide insights into which features significantly influence the target variable. This article delves into various techniques to obtain feature weights, aiming to enhance your understanding and ability to apply these methods in practical scenarios.
Methods for Obtaining Feature Weights
Several methods can be utilized to determine the feature weights. These methods vary depending on the type of model being used, such as linear models, tree-based models, and ensemble methods. Below are the most common techniques:
1. Coefficients in Linear Models
For linear models, such as linear regression and logistic regression, feature weights are represented by the coefficients. These coefficients indicate the change in the target variable for a one-unit change in the feature, given that all other variables remain constant.
Example
Consider a simple linear regression model:
Here, and are the feature weights for and , respectively. A larger absolute value of a coefficient implies that the feature has a greater effect on the target variable.
2. Feature Importance in Tree-Based Models
Tree-based models, such as Decision Trees, Random Forests, and Gradient Boosting, offer built-in feature importance measures. These models typically use metrics like Gini importance or mean decrease in impurity (MDI) to assess feature significance.
Calculation
- Gini Importance: Measures the total decrease in node impurities weighted by the probability of reaching that node, averaged over all trees in the ensemble.
- Mean Decrease in Accuracy (MDA): Evaluates how much accuracy decreases when the feature is permuted. This method involves shuffling feature values and observing the impact on model predictions.
3. Permutation Feature Importance
Permutation feature importance is model-agnostic and can be applied to any machine learning model. The concept involves permuting the values of a feature and measuring the change in the model's performance metric, typically accuracy or mean squared error.
Steps
- Train the model and calculate its baseline performance metric.
- Permute the values of a specific feature.
- Re-evaluate the model using the permuted dataset.
- Calculate the change in the performance metric to determine the importance.
4. SHAP Values
SHAP (SHapley Additive exPlanations) values provide a unified measure of feature importance by considering the contribution of each feature alone and in combination with others.
Explanation
SHAP values are based on cooperative game theory and aim to allocate the prediction fairly among the features. They provide insights into individual predictions and can explain both global and local feature contributions.
5. LIME
LIME (Local Interpretable Model-Agnostic Explanations) approximates the model locally using interpretable models, like linear models, to understand feature importance for a particular prediction.
Steps
- Draw samples around the instance you want to explain.
- Train a simple, interpretable model on these samples.
- Use the weights of this interpretable model to assess feature importance.
Comparison of Methods
| Method | Model Type | Explanation Type | Global or Local | Interpretation |
| Coefficients in Linear Models | Linear Models | Intrinsic | Global | Direct relationship via coefficients |
| Feature Importance in Trees | Tree-Based Models | Intrinsic | Global | Based on impurity reduction |
| Permutation Importance | Any | Extrinsic | Global/Local | Effect on model's performance metric |
| SHAP Values | Any | Extrinsic | Global/Local | Fair contribution of features |
| LIME | Any | Extrinsic | Local | Local approximation with interpretable model |
Considerations and Best Practices
- Model Specificity: Choose a method that aligns well with your model type. For example, linear models naturally provide coefficients, whereas tree-based models excel with intrinsic importance metrics.
- Interpretability vs. Accuracy: Assess the trade-off between interpretability and model accuracy. While simpler models may offer clearer interpretations, more complex models often provide better predictive performance.
- Feature Correlation: Consider the impact of feature correlation. Highly correlated features can skew feature importance metrics, particularly in tree-based models.
Conclusion
Understanding how to obtain and interpret feature weights is a vital part of the model interpretability process. By leveraging the appropriate methods based on your model type and complexity, you can gain valuable insights into the driving factors behind your model's predictions. Always choose the technique that offers the clearest understanding while maintaining model performance and integrity.
Related reading
- How to obtain filenames during prediction while using tf.keras.preprocessing.image_dataset_from_directory?
- How to obtain information gain from a scikit-learn DecisionTreeClassifier?
- How to optimize for inference a simple, saved TensorFlow 1.0.1 graph?
- How to optimize for multiple metrics in Optuna
- How to pass another entire column as argument to pandas fillna
- How to pass in multidimensional data to xgboost model
- How to obtain the index permutation after the sorting
- How to optimally divide an array into two subarrays so that sum of elements in both are same, otherwise give an error?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.