Coder Social home page Coder Social logo

decision-tree-from-scratch's Introduction

Decision Tree and Random Forest

This project has three parts:

  1. Decision Tree from Scratch
  2. Random Forest from Scratch
  3. Random Forest using Sklearn Library

Dataset

The dataset used in this project is prison_dataset.csv, which includes information about prisoners in a particular country. The dataset has 6 columns and 524 rows, containing the following information:

  1. age: Age of the prisoner.
  2. gender: Gender of the prisoner (Male or Female).
  3. education: Education level of the prisoner (Primary, High School, University).
  4. criminal_past: Whether the prisoner has a criminal past (Yes or No).
  5. time_served: Time served in prison.
  6. parole: Whether the prisoner is granted parole (Yes or No). The aim of this project is to build a decision tree model to predict whether a prisoner is granted parole or not based on the information given in the dataset.

Algorithms

1. Decision Tree from Scratch : DecisionTree.py

How it works

The algorithm works by recursively building the decision tree top-down. At each step, it selects the most informative feature by calculating the information gain of each feature. The information gain is calculated by comparing the entropy of the dataset before and after splitting by the feature. The algorithm stops when all the instances in a branch belong to the same class or the maximum depth of the tree is reached. This is a Python code for building decision tree based on ID3 algorithm. The code is written in DecisionTree.py and the input dataset is prison_dataset.csv. The DecisionTree.py includes the following functions:

  • trainTestSplit(data): Splits the input dataset into training and testing sets.
  • totalEntropy(data, classes): Calculates the total entropy of the dataset.
  • entropy(data, classes): Calculates the entropy of a given feature.
  • informationGain(colName, data, classes): Calculates the information gain of a given feature.
  • bestColForSplit(data, class_list): Determines the feature with the highest information gain and returns its name.
  • splitData(colName, data, classes, depth): Splits the dataset based on the given feature and returns a tree node and the updated dataset.
  • genTree(root, prevFeatureVal, data, classes, depth): Generates a decision tree recursively.
  • decisionTree(data): Builds the decision tree model and returns the root node.

decision-tree-from-scratch's People

Contributors

shiivashaakeri avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.