Gradient boosting की feature importances
Random forests की तरह, हम gradient boosting मॉडलों से भी feature importances निकाल सकते हैं ताकि समझ सकें कि कौन-से फीचर सबसे अच्छे predictors हैं। कई बार अलग-अलग tree-based मॉडल आज़माना और उन सबकी feature importances देखना अच्छा रहता है। इससे किसी एक विशेष मॉडल से आने वाली विचित्रताओं का औसत निकल जाता है।
Feature importances एक numpy array के रूप में gradient boosting मॉडल की .feature_importances_ property में मिलती हैं। एक अच्छा प्लॉट बनाने के लिए हमें feature importances के sorted indices np.argsort() से निकालने होंगे। हमें फीचर बड़े से छोटे क्रम में चाहिए, इसलिए हम Python indexing से sorted importances को इस तरह उल्टा करेंगे: feat_importances[::-1].
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में Finance के लिए Machine Learning
अभ्यास निर्देश
- Python indexing से
sorted_indexवैरिएबल को उल्टा करें ताकि क्रम बड़े से छोटे हो जाए। feature_namesको numpy array में बदलकर औरsorted_indexसे index करके, sorted feature labels की listlabelsबनाएँ।- xticks का bar plot बनाएँ, जहाँ
feature_importancesकोsorted_indexसे index किया गया हो, और xtick labels के रूप मेंlabelsहों.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# Extract feature importances from the fitted gradient boosting model
feature_importances = gbr.feature_importances_
# Get the indices of the largest to smallest feature importances
sorted_index = np.argsort(feature_importances)[::____]
x = range(features.shape[1])
# Create tick labels
labels = np.array(feature_names)[____]
plt.bar(____, feature_importances[____], tick_label=____)
# Set the tick lables to be the feature names, according to the sorted feature_idx
plt.xticks(rotation=90)
plt.show()