SlideShare a Scribd company logo
2
Most read
4
Most read
7
Most read
Swipe
R - Decision Tree
Decision tree is a graph to represent choices and
their results in form of a tree.
The nodes in the graph represent an event or
choice and the edges of the graph represent the
decision rules or conditions.
It is mostly used in Machine Learning and Data
Mining applications using R.
R - Decision Tree
Predicting an email as spam or not spam,
predicting of a tumor is cancerous or predicting a
loan as a good or bad credit risk based on the
factors in each of these.
Generally, a model is created with observed data
also called training data.
Then a set of validation data is used to verify and
improve the model.
R has packages which are used to create and
visualize decision trees.
For new set of predictor variable, we use this
model to arrive at a decision on the category
(yes/No, spam/not spam) of the data.
Examples of use of decision tress
Use the below command in R console to install the
package. You also have to install the dependent
packages if any.
install.packages("party")
The package "party" has the function ctree()
which is used to create and analyze decison tree.
The basic syntax for creating a decision tree in R
is:-
ctree(formula, data)
Following is the description of the parameters
used −
formula is a formula describing the predictor
and response variables.
data is the name of the data set used.
Input Data
We will use the R in-built data set named
readingSkills to create a decision tree.
It describes the score of someone's readingSkills if
we know the variables "age","shoesize","score"
and whether the person is a native speaker or not.
Here is the sample data.
# Load the party package. It will automatically load
other
# dependent packages.
library(party)
# Print some records from data set
readingSkills.print(head(readingSkills))
When we execute the above code, it produces the
following result and chart:-
nativeSpeaker age shoeSize score
1 yes 5 24.83189 32.29385
2 yes 6 25.95238 36.63105
3 no 11 30.42170 49.60593
4 yes 7 28.66450 40.28456
5 yes 11 31.88207 55.46085
6 yes 10 30.07843 52.83124
Loading required package: methods
Loading required package: grid
We will use the ctree() function to create the
decision tree and see its graph.
Example
# Load the party package. It will automatically load
other
# dependent packages.
library(party)
# Create the input data frame.
input.dat <- readingSkills[c(1:105),]
# Give the chart file a name.
png(file = "decision_tree.png")
# Create the tree.
output.tree <- ctree(
nativeSpeaker ~ age + shoeSize + score,
data = input.dat)
# Plot the tree.
plot(output.tree)
# Save the file.
dev.off()
When we execute the above code, it produces the
following result:-
null device
1
Loading required package: methods
Loading required package: grid
Loading required package: mvtnorm
Loading required package: modeltools
Loading required package: stats4
Loading required package: strucchange
Loading required package: zoo
Attaching package: ‘zoo’
The following objects are masked from
‘package:base’:
as.Date, as.Date.numeric
Loading required package: sandwich
R   decision tree
Tableau - Quick Guide
Tableau - Get Started
Tableau - Navigation
Stay Tuned with
Topics for next Post

More Related Content

What's hot (20)

PPTX
Principal component analysis
Partha Sarathi Kar
 
PPTX
ML - Multiple Linear Regression
Andrew Ferlitsch
 
PPT
Classification and prediction
Acad
 
PDF
Logistic Regression in Python | Logistic Regression Example | Machine Learnin...
Edureka!
 
PPTX
Unit 1 - R Programming (Part 2).pptx
Malla Reddy University
 
PPTX
K Nearest Neighbor V1.0 Supervised Machine Learning Algorithm
DataMites
 
PDF
Ridge regression
Ananda Swarup
 
PPTX
Data Management in R
Sankhya_Analytics
 
PDF
Bias and variance trade off
VARUN KUMAR
 
PPTX
Logistic Regression | Logistic Regression In Python | Machine Learning Algori...
Simplilearn
 
PPT
K means Clustering Algorithm
Kasun Ranga Wijeweera
 
PDF
4 Descriptive Statistics with R
Dr Nisha Arora
 
PDF
Classification metrics
SPb_Data_Science
 
PDF
Introduction to Machine Learning Classifiers
Functional Imperative
 
PPTX
K-Folds Cross Validation Method
SHUBHAM GUPTA
 
PDF
Missing data handling
QuantUniversity
 
PDF
PCA (Principal component analysis)
Learnbay Datascience
 
PPTX
Decision Tree.pptx
Ramakrishna Reddy Bijjam
 
PPT
3.7 outlier analysis
Krish_ver2
 
Principal component analysis
Partha Sarathi Kar
 
ML - Multiple Linear Regression
Andrew Ferlitsch
 
Classification and prediction
Acad
 
Logistic Regression in Python | Logistic Regression Example | Machine Learnin...
Edureka!
 
Unit 1 - R Programming (Part 2).pptx
Malla Reddy University
 
K Nearest Neighbor V1.0 Supervised Machine Learning Algorithm
DataMites
 
Ridge regression
Ananda Swarup
 
Data Management in R
Sankhya_Analytics
 
Bias and variance trade off
VARUN KUMAR
 
Logistic Regression | Logistic Regression In Python | Machine Learning Algori...
Simplilearn
 
K means Clustering Algorithm
Kasun Ranga Wijeweera
 
4 Descriptive Statistics with R
Dr Nisha Arora
 
Classification metrics
SPb_Data_Science
 
Introduction to Machine Learning Classifiers
Functional Imperative
 
K-Folds Cross Validation Method
SHUBHAM GUPTA
 
Missing data handling
QuantUniversity
 
PCA (Principal component analysis)
Learnbay Datascience
 
Decision Tree.pptx
Ramakrishna Reddy Bijjam
 
3.7 outlier analysis
Krish_ver2
 

Similar to R decision tree (20)

PPT
Basics of R
Sachita Yadav
 
PPTX
An Interactive Introduction To R (Programming Language For Statistics)
Dataspora
 
PDF
Machine Learning with Python- Machine Learning Algorithms- Decision Tree.pdf
KalighatOkira
 
DOCX
A random decision tree frameworkfor privacy preserving data mining
Venkat Projects
 
DOCX
A random decision tree frameworkfor privacy preserving data mining
Venkat Projects
 
PDF
Data analytics using R programming
Umang Singh
 
PPTX
Lecture-6-7.pptx
JohnMichaelPadernill
 
PDF
Know How to Create and Visualize a Decision Tree with Python.pdf
Data Science Council of America
 
PPTX
Implementing a data_science_project (Python Version)_part1
Dr Sulaimon Afolabi
 
PDF
Workshop: Your first machine learning project
Alex Austin
 
PDF
Start machine learning in 5 simple steps
Renjith M P
 
PDF
Machine Learning with Python- Machine Learning Algorithms- Random Forest.pdf
KalighatOkira
 
PDF
Analysis using r
Priya Mohan
 
PDF
ITB Term Paper - 10BM60066
rahulsm27
 
PPT
R studio
Kinza Irshad
 
PDF
R-Language-Lab-Manual-lab-1.pdf
DrGSakthiGovindaraju
 
PDF
R-Language-Lab-Manual-lab-1.pdf
KabilaArun
 
PDF
R-Language-Lab-Manual-lab-1.pdf
attalurilalitha
 
PDF
Unraveling The Meaning From COVID-19 Dataset Using Python – A Tutorial for be...
Kavika Roy
 
PPTX
IT in Business: Chapter 11 Data Sciences
AreebaSaqib5
 
Basics of R
Sachita Yadav
 
An Interactive Introduction To R (Programming Language For Statistics)
Dataspora
 
Machine Learning with Python- Machine Learning Algorithms- Decision Tree.pdf
KalighatOkira
 
A random decision tree frameworkfor privacy preserving data mining
Venkat Projects
 
A random decision tree frameworkfor privacy preserving data mining
Venkat Projects
 
Data analytics using R programming
Umang Singh
 
Lecture-6-7.pptx
JohnMichaelPadernill
 
Know How to Create and Visualize a Decision Tree with Python.pdf
Data Science Council of America
 
Implementing a data_science_project (Python Version)_part1
Dr Sulaimon Afolabi
 
Workshop: Your first machine learning project
Alex Austin
 
Start machine learning in 5 simple steps
Renjith M P
 
Machine Learning with Python- Machine Learning Algorithms- Random Forest.pdf
KalighatOkira
 
Analysis using r
Priya Mohan
 
ITB Term Paper - 10BM60066
rahulsm27
 
R studio
Kinza Irshad
 
R-Language-Lab-Manual-lab-1.pdf
DrGSakthiGovindaraju
 
R-Language-Lab-Manual-lab-1.pdf
KabilaArun
 
R-Language-Lab-Manual-lab-1.pdf
attalurilalitha
 
Unraveling The Meaning From COVID-19 Dataset Using Python – A Tutorial for be...
Kavika Roy
 
IT in Business: Chapter 11 Data Sciences
AreebaSaqib5
 
Ad

More from Learnbay Datascience (20)

PDF
Top data science projects
Learnbay Datascience
 
PDF
Python my SQL - create table
Learnbay Datascience
 
PDF
Python my SQL - create database
Learnbay Datascience
 
PDF
Python my sql database connection
Learnbay Datascience
 
PDF
Python - mySOL
Learnbay Datascience
 
PDF
AI - Issues and Terminology
Learnbay Datascience
 
PDF
AI - Fuzzy Logic Systems
Learnbay Datascience
 
PDF
AI - working of an ns
Learnbay Datascience
 
PDF
Artificial Intelligence- Neural Networks
Learnbay Datascience
 
PDF
AI - Robotics
Learnbay Datascience
 
PDF
Applications of expert system
Learnbay Datascience
 
PDF
Components of expert systems
Learnbay Datascience
 
PDF
Artificial intelligence - expert systems
Learnbay Datascience
 
PDF
AI - natural language processing
Learnbay Datascience
 
PDF
Ai popular search algorithms
Learnbay Datascience
 
PDF
AI - Agents & Environments
Learnbay Datascience
 
PDF
Artificial intelligence - research areas
Learnbay Datascience
 
PDF
Artificial intelligence composed
Learnbay Datascience
 
PDF
Artificial intelligence intelligent systems
Learnbay Datascience
 
PDF
Applications of ai
Learnbay Datascience
 
Top data science projects
Learnbay Datascience
 
Python my SQL - create table
Learnbay Datascience
 
Python my SQL - create database
Learnbay Datascience
 
Python my sql database connection
Learnbay Datascience
 
Python - mySOL
Learnbay Datascience
 
AI - Issues and Terminology
Learnbay Datascience
 
AI - Fuzzy Logic Systems
Learnbay Datascience
 
AI - working of an ns
Learnbay Datascience
 
Artificial Intelligence- Neural Networks
Learnbay Datascience
 
AI - Robotics
Learnbay Datascience
 
Applications of expert system
Learnbay Datascience
 
Components of expert systems
Learnbay Datascience
 
Artificial intelligence - expert systems
Learnbay Datascience
 
AI - natural language processing
Learnbay Datascience
 
Ai popular search algorithms
Learnbay Datascience
 
AI - Agents & Environments
Learnbay Datascience
 
Artificial intelligence - research areas
Learnbay Datascience
 
Artificial intelligence composed
Learnbay Datascience
 
Artificial intelligence intelligent systems
Learnbay Datascience
 
Applications of ai
Learnbay Datascience
 
Ad

Recently uploaded (20)

PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PPTX
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 

R decision tree

  • 2. Decision tree is a graph to represent choices and their results in form of a tree. The nodes in the graph represent an event or choice and the edges of the graph represent the decision rules or conditions. It is mostly used in Machine Learning and Data Mining applications using R. R - Decision Tree
  • 3. Predicting an email as spam or not spam, predicting of a tumor is cancerous or predicting a loan as a good or bad credit risk based on the factors in each of these. Generally, a model is created with observed data also called training data. Then a set of validation data is used to verify and improve the model. R has packages which are used to create and visualize decision trees. For new set of predictor variable, we use this model to arrive at a decision on the category (yes/No, spam/not spam) of the data. Examples of use of decision tress
  • 4. Use the below command in R console to install the package. You also have to install the dependent packages if any. install.packages("party") The package "party" has the function ctree() which is used to create and analyze decison tree. The basic syntax for creating a decision tree in R is:- ctree(formula, data) Following is the description of the parameters used − formula is a formula describing the predictor and response variables. data is the name of the data set used.
  • 5. Input Data We will use the R in-built data set named readingSkills to create a decision tree. It describes the score of someone's readingSkills if we know the variables "age","shoesize","score" and whether the person is a native speaker or not. Here is the sample data. # Load the party package. It will automatically load other # dependent packages. library(party) # Print some records from data set readingSkills.print(head(readingSkills))
  • 6. When we execute the above code, it produces the following result and chart:- nativeSpeaker age shoeSize score 1 yes 5 24.83189 32.29385 2 yes 6 25.95238 36.63105 3 no 11 30.42170 49.60593 4 yes 7 28.66450 40.28456 5 yes 11 31.88207 55.46085 6 yes 10 30.07843 52.83124 Loading required package: methods Loading required package: grid
  • 7. We will use the ctree() function to create the decision tree and see its graph. Example # Load the party package. It will automatically load other # dependent packages. library(party) # Create the input data frame. input.dat <- readingSkills[c(1:105),] # Give the chart file a name. png(file = "decision_tree.png") # Create the tree. output.tree <- ctree( nativeSpeaker ~ age + shoeSize + score, data = input.dat) # Plot the tree. plot(output.tree) # Save the file. dev.off()
  • 8. When we execute the above code, it produces the following result:- null device 1 Loading required package: methods Loading required package: grid Loading required package: mvtnorm Loading required package: modeltools Loading required package: stats4 Loading required package: strucchange Loading required package: zoo Attaching package: ‘zoo’ The following objects are masked from ‘package:base’: as.Date, as.Date.numeric Loading required package: sandwich
  • 10. Tableau - Quick Guide Tableau - Get Started Tableau - Navigation Stay Tuned with Topics for next Post