You are on page 1of 4

Name : - Naitik Patel

Roll No : - 20BCP238
Aim : -
Implement a decision tree for performing classification in the programming language
of your choice. 

Hardware Required : - Laptop, Internet Connection

Software Required : - Anaconda


Knowledge Required : -

 Basic Python , Sklearn, Pandas, Numpy, Matplotlib Knowledge


 Knowledge of various Classification Algorithms
 Knowldege of Decision trees

Theory : -
A decision tree is a flowchart-like structure in which each internal node represents a "test" on
an attribute (e.g. whether a coin flip comes up heads or tails), each branch represents the
outcome of the test, and each leaf node represents a class label (decision taken after
computing all attributes). The paths from root to leaf represent classification rules.
In decision analysis, a decision tree and the closely related influence diagram are used as a
visual and analytical decision support tool, where the expected values (or expected utility) of
competing alternatives are calculated.
A decision tree consists of three types of nodes:

1. Decision nodes – typically represented by squares


2. Chance nodes – typically represented by circles
3. End nodes – typically represented by triangles
Decision trees are commonly used in operations research and operations management. If, in
practice, decisions have to be taken online with no recall under incomplete knowledge, a
decision tree should be paralleled by a probability model as a best choice model or online
selection model algorithm. Another use of decision trees is as a descriptive means for
calculating conditional probabilities.
Decision trees, influence diagrams, utility functions, and other decision analysis tools and
methods are taught to undergraduate students in schools of business, health economics, and
public health, and are examples of operations research or management science methods.
Code : -

import unittest
import tempfile
import json
import numpy as np
import pandas as pd
import os
from numpy.testing import assert_almost_equal
from sklearn import datasets

from supervised.algorithms.decision_tree import (


DecisionTreeAlgorithm,
DecisionTreeRegressorAlgorithm,
)
from supervised.utils.metric import Metric

import tempfile

class DecisionTreeTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.X, cls.y = datasets.make_regression(
n_samples=100,
n_features=5,
n_informative=4,
n_targets=1,
shuffle=False,
random_state=0,
)

def test_reproduce_fit_regression(self):
metric = Metric({"name": "rmse"})
params = {"max_depth": 1, "seed": 1, "ml_task": "regression"}
prev_loss = None
for _ in range(3):
model = DecisionTreeRegressorAlgorithm(params)
model.fit(self.X, self.y)
y_predicted = model.predict(self.X)
loss = metric(self.y, y_predicted)
if prev_loss is not None:
assert_almost_equal(prev_loss, loss)
prev_loss = loss

def test_save_and_load(self):
metric = Metric({"name": "rmse"})
dt = DecisionTreeRegressorAlgorithm({"ml_task": "regression"})
dt.fit(self.X, self.y)
y_predicted = dt.predict(self.X)
loss = metric(self.y, y_predicted)

filename = os.path.join(tempfile.gettempdir(), os.urandom(12).hex())

dt.save(filename)
dt2 = DecisionTreeRegressorAlgorithm({"ml_task": "regression"})
dt2.load(filename)

y_predicted = dt2.predict(self.X)
loss2 = metric(self.y, y_predicted)
assert_almost_equal(loss, loss2)

# Finished with temp file, delete it


os.remove(filename)

def test_is_fitted(self):
params = {"max_depth": 1, "seed": 1, "ml_task": "regression"}
model = DecisionTreeRegressorAlgorithm(params)
self.assertFalse(model.is_fitted())
model.fit(self.X, self.y)
self.assertTrue(model.is_fitted())
Output :-

Attached all discion tree graps in video formate with the code

You might also like