0% found this document useful (0 votes)
4 views2 pages

CART Practical 6

Practice

Uploaded by

pranayaws15
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

CART Practical 6

Practice

Uploaded by

pranayaws15
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Experiment No: 06

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

# Load iris dataset for classification


from sklearn.datasets import load_iris
iris = load_iris()
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Train the Decision Tree classifier


clf = DecisionTreeClassifier(criterion='gini', random_state=42)
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f"Classification Accuracy: {accuracy * 100:.2f}%")

# Load California Housing dataset for regression


from sklearn.datasets import fetch_california_housing
housing = fetch_california_housing()
X_housing = housing.data
y_housing = housing.target
X_train_h, X_test_h, y_train_h, y_test_h = train_test_split(X_housing, y_housing, test_size=0.3,
random_state=42)

# Train the Decision Tree regressor


reg = DecisionTreeRegressor(criterion='mse', random_state=42)
reg.fit(X_train_h, y_train_h)
y_pred_h = reg.predict(X_test_h)
mse = mean_squared_error(y_test_h, y_pred_h)
print(f"Mean Squared Error: {mse:.2f}")

# Plot the Decision Tree for Classification


plt.figure(figsize=(12,8))
tree.plot_tree(clf, filled=True, feature_names=iris.feature_names,
class_names=iris.target_names)
plt.show()

# Plot the Decision Tree for Regression


plt.figure(figsize=(12,8))
tree.plot_tree(reg, filled=True, feature_names=housing.feature_names)
plt.show()
```

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.

You might also like