शुरू करेंमुफ़्त में शुरू करें

एक decision tree फिट करें

Random forests भविष्यवाणी के लिए एक पसंदीदा मॉडल हैं; ये बिना ज़्यादा ट्यूनिंग के भी अच्छा काम करते हैं. लेकिन पहले हम random forests के बिल्डिंग ब्लॉक — decision trees — सीखेंगे.

Decision trees, फीचर्स के आधार पर डेटा को समूहों में बाँटते हैं. ये एक root node से शुरू होते हैं और डेटा को नीचे की ओर split करते रहते हैं, जब तक कि हम leaf nodes तक न पहुँच जाएँ.

decision tree

हम sklearn का इस्तेमाल करके DecisionTreeRegressor और .fit(features, targets) से एक decision tree फिट कर सकते हैं.

अगर आप tree की depth (या height) को सीमित नहीं करते, तो यह स्प्लिट करता रहेगा जब तक हर leaf में सिर्फ 1 sample न रह जाए — यह overfitting की चरम स्थिति है. Overfitting के बारे में आप आने वाले चैप्टर्स में और सीखेंगे.

यह अभ्यास पाठ्यक्रम का हिस्सा है

Python में Finance के लिए Machine Learning

पाठ्यक्रम देखें

अभ्यास निर्देश

  • इम्पोर्ट की हुई class DecisionTreeRegressor को default arguments (अर्थात कोई आर्ग्युमेंट नहीं) के साथ इस्तेमाल करके decision_tree नाम का decision tree मॉडल बनाइए.
  • पहले से बनाए गए train_features और train_targets (जिनमें अब day-of-week और volume फीचर्स हैं) का उपयोग करके मॉडल फिट कीजिए.
  • training features और targets पर score प्रिंट कीजिए, साथ ही test_features और test_targets पर भी.

इंटरैक्टिव व्यावहारिक अभ्यास

इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।

from sklearn.tree import DecisionTreeRegressor

# Create a decision tree regression model with default arguments
decision_tree = ____

# Fit the model to the training features and targets
decision_tree.fit(____)

# Check the score on train and test
print(decision_tree.score(train_features, train_targets))
print(decision_tree.score(____))
कोड संपादित करें और चलाएँ