Decision Tree Regressor Explained
Suppose we are predicting house prices based on size (sq ft). A Decision Tree Regressor splits data into branches to make predictions.
Tree Diagram (Flow)
Python Implementation
# Import the Decision Tree Regressor
from sklearn.tree import DecisionTreeRegressor
# Define the model (random_state ensures reproducibility)
melbourne_model = DecisionTreeRegressor(random_state=1)
# Fit the model to training data
melbourne_model.fit(X, y)
# Predict on new data
predictions = melbourne_model.predict(X_new)
Key Concepts
- Root Node: The first split of the dataset based on the best feature.
- Branches: Further splits to reduce prediction error.
- Leaf Nodes: The final predicted values.
- random_state: Ensures that the tree splits are reproducible.
- fit(): Learns the optimal splits from your training data.
- predict(): Uses the learned tree to make predictions on new data.
This setup is perfect for predicting numerical outcomes (regression) such as house prices, irrigation requirements, or any continuous target variable.