Decision Trees Example

This is a simple example of Decision Trees using Python and scikit-learn.

Decision Trees Overview

Decision Trees are a versatile and widely used machine learning algorithm that can be applied to both classification and regression tasks. The model makes decisions based on asking a series of questions about the input features, leading to a prediction. In the case of regression, the output is a continuous value.

Key concepts of Decision Trees:

Decision Trees are easy to interpret and visualize, making them a popular choice for various applications. However, they are prone to overfitting, and techniques like pruning are often applied to address this issue.

Python Source Code:

# Import necessary libraries
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeRegressor
from sklearn.metrics import mean_squared_error

# Generate synthetic data
np.random.seed(42)
X = 2 * np.random.rand(100, 1)
y = 4 + 3 * X + np.random.randn(100, 1)

# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train the Decision Tree model
model = DecisionTreeRegressor()
model.fit(X_train, y_train)

# Make predictions on the test set
y_pred = model.predict(X_test)

# Evaluate the model
mse = mean_squared_error(y_test, y_pred)
print(f'Mean Squared Error: {mse}')

# Plot the results
plt.scatter(X_test, y_test, color='black')
plt.plot(X_test, y_pred, color='blue', linewidth=3)
plt.title('Decision Trees Example')
plt.xlabel('X')
plt.ylabel('y')
plt.show()

Explanation: