उत्तम tree depth पहचाने
अब आप decision tree के max_depth पैरामीटर को tune करेंगे ताकि over-fitting कम हो और मॉडल के performance metrics अच्छे बने रहें. आप एक for लूप कई max_depth मानों पर चलाएँगे, हर मान के लिए decision tree फिट करेंगे, और फिर performance metrics निकालेंगे.
depth_list नाम की list, जिसमें पैरामीटर के candidate हैं, आपके लिए लोड कर दी गई है. depth_tuning नाम का array आपके लिए 2 कॉलम के साथ तैयार है: पहले कॉलम में depth candidates भरे हुए हैं, और अगले कॉलम में recall score के लिए placeholder है. इसके अलावा, features और target वैरिएबल्स training डेटा के लिए train_X, train_Y और test डेटा के लिए test_X, test_Y नाम से लोड हैं. numpy और pandas लाइब्रेरी क्रमशः np और pd के रूप में उपलब्ध हैं.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में मार्केटिंग के लिए मशीन लर्निंग
अभ्यास निर्देश
- List
depth_listकी length तक 0 से range लेकर एकforलूप चलाएँ. - हर depth candidate के लिए decision tree classifier initialize और fit करें और test डेटा पर churn predict करें.
- हर depth candidate के लिए
recall_score()फंक्शन से recall score निकालें और उसेdepth_tunningके दूसरे कॉलम में स्टोर करें. depth_tuningसे उपयुक्त कॉलम नामों के साथ एकpandasDataFrame बनाएँ.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# Run a for loop over the range of depth list length
for index in ___(0, len(depth_list)):
# Initialize and fit decision tree with the `max_depth` candidate
mytree = DecisionTreeClassifier(___=depth_list[index])
mytree.fit(___, train_Y)
# Predict churn on the testing data
pred_test_Y = mytree.predict(___)
# Calculate the recall score
depth_tuning[index,1] = ___(test_Y, ___)
# Name the columns and print the array as pandas DataFrame
col_names = ['Max_Depth','Recall']
print(pd.DataFrame(depth_tuning, columns=___))