date: 2026-03-10
Decision Tree Regression
This project demonstrates Decision Tree Regression using Python and Scikit-learn.
Decision Tree Regression is a non-linear model that splits the data into smaller parts using conditions and predicts values based on those splits.
📌 What is Decision Tree Regression?
Decision Tree Regression is a machine learning algorithm that works like a tree structure.
It divides the dataset using rules (if-else conditions) and makes predictions at the leaf nodes.
Unlike Linear Regression, Decision Tree can handle non-linear relationships.
Example logic: - if value < 5 → predict 10 - else → predict 50
📊 Dataset
Simple numeric dataset used for practice. X = [1,2,3,4,5,6,7,8,9,10] y = [1,4,9,16,25,36,49,64,81,100]
This dataset represents a non-linear pattern.
⚙️ Libraries Used
- numpy
- matplotlib
- scikit-learn
Install using:
pip install numpy matplotlib scikit-learn
🧠 Model Used
DecisionTreeRegressor()
Decision Tree splits data into smaller regions and predicts values.
💻 Code
import numpy as np
import matplotlib.pyplot as plt
from sklearn.tree import DecisionTreeRegressor
from sklearn.metrics import r2_score
X = np.array([1,2,3,4,5,6,7,8,9,10]).reshape(-1,1)
y = np.array([1,4,9,16,25,36,49,64,81,100])
model = DecisionTreeRegressor()
model.fit(X,y)
y_pred = model.predict(X)
print("R2 score:", r2_score(y,y_pred))
plt.scatter(X,y)
plt.plot(X,y_pred)
📈 Output
R2 Score: 1.0 Decision Tree fits training data perfectly, but it may cause overfitting.
⚠️ Important Concepts
Non-linear model
Works like if-else
Can overfit
Tree depth controls complexity
Base model for Random Forest
👨💻 Author
Suraj Singh Machine Learning Practice Series