CART Practical 6
CART Practical 6
AIM
To implement the Classification and Regression Tree (CART) algorithm for both
classification and regression tasks using the scikit-learn library in Python, and evaluate its
performance.
Process
1. Load the required libraries and datasets (Iris for classification and California Housing for
regression).
2. Split the dataset into training and testing sets.
3. Implement the Decision Tree algorithm using scikit-learn's DecisionTreeClassifier and
DecisionTreeRegressor.
4. Train the model and make predictions.
5. Evaluate the model performance using accuracy for classification and mean squared error
(MSE) for regression.
6. Visualize the decision tree structure.
Code
```python
# Importing necessary libraries
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
from sklearn.metrics import accuracy_score, mean_squared_error
import matplotlib.pyplot as plt
from sklearn import tree
Output
Classification Accuracy: 100.00%
Mean Squared Error: 0.25
Conclusion
The Classification and Regression Tree (CART) algorithm was successfully implemented
using the scikit-learn library. The classification task achieved an accuracy of 100% on the
Iris dataset, indicating perfect classification on the test data. For regression, the Mean
Squared Error (MSE) was 0.25, demonstrating a reasonably good prediction performance
on the California Housing dataset.