Question

In: Computer Science

The following code will generate a Decision Tree. You need to run the code and explain...

The following code will generate a Decision Tree. You need to run the code and explain the tree. After you get the Tree. You need to explain how does it draw like that.


install.packages("rpart.plot") # install package rpart.plot

##########################################
# section 7.1.1 Overview of a Decision Tree
##########################################

library("rpart")
library("rpart.plot")

# Read the data
setwd("c:/data/")
banktrain <- read.table("bank-sample-test.csv",header=TRUE,sep=",")

## drop a few columns to simplify the tree
drops<-c("age", "balance", "day", "campaign", "pdays", "previous", "month")
banktrain <- banktrain [,!(names(banktrain) %in% drops)]
summary(banktrain)

# Make a simple decision tree by only keeping the categorical variables
fit <- rpart(subscribed ~ job + marital + education + default + housing + loan + contact + poutcome,
method="class",
data=banktrain,
control=rpart.control(minsplit=1),
parms=list(split='information'))
summary(fit)
# Plot the tree
rpart.plot(fit, type=4, extra=2, clip.right.labs=FALSE, varlen=0, faclen=3)


Solutions

Expert Solution

HI,

please see the code I have used for the decision tree. The dataset used has the column name as "y" instead of "subscribed" so please do the necessary substitution if you use your dataset.

The dataset I have taken is uploaded on the github repository - https://github.com/just4jin/bank-marketing-prediction/blob/master/data/bank.csv

The code-

install.packages("rpart.plot") # install package rpart.plot
##########################################
# section 7.1.1 Overview of a Decision Tree
##########################################
library("rpart")
library("rpart.plot")
# Read the data
setwd("c:/data/")
banktrain <- read.table("bank_full.csv",head=TRUE,sep=";")
## drop a few columns to simplify the tree
drops<-c("age", "balance", "day", "campaign", "pdays", "previous", "month")
banktrain <- banktrain [,!(names(banktrain) %in% drops)]
summary(banktrain)
# Make a simple decision tree by only keeping the categorical variables
fit <- rpart(y ~ job + marital + education + default + housing + loan + contact + poutcome,
method="class",
data=banktrain,
control=rpart.control(minsplit=1),
parms=list(split='information'))
summary(fit)
# Plot the tree
rpart.plot(fit, type=4, extra=2, clip.right.labs=FALSE, varlen=0, faclen=3)


Related Solutions

Read the following news items and explain where a short-run decision and a long-run decision are...
Read the following news items and explain where a short-run decision and a long-run decision are involved? a. January 31, 2020: Tim Horton will open 60 more stores in Asian countries   b. March 30, 2020: All stores of Tim Horton will shut down on Tuesday stores so that baristas can receive a refresher course. c. June 2, 2020: Tim Horton replaces baristas with vending machines. d. June 30, 2020: Tim Horton is closing 200 stores in British Columbia by the...
Run the following code and explain the results (expectation of the array). Test it on the...
Run the following code and explain the results (expectation of the array). Test it on the following arrays: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] [1.0, 4.0, 6.0,2.0 , 3.0, 5.0] [5.0, 3.0, 2.0, 6.0, 4.0, 1.0] [6.0, 5.0, 4.0, 3.0, 2.0, 1.0] // Java code to calculate expected // value of an array import java.io.*; class GFG { // Function to calculate expectation static float calc_Expectation(float a[], float n) { // Variable prb is for probability of each // element...
In a decision tree, how does the algorithm pick the attributes for splitting? Would you explain...
In a decision tree, how does the algorithm pick the attributes for splitting? Would you explain it logically and specifically?
In each of the following decision situations, explain the trade-offs involved.             a.         You need to decide whether...
In each of the following decision situations, explain the trade-offs involved.             a.         You need to decide whether to buy a used or a new car.             b.         You are thinking about opening a computer-services business in your basement.             c.         You are a consultant for the City of Toronto. The city council asks you to analyze the implications of a new high-rise office building downtown.
I need a decision tree Consider a scenario in which a college must recruit students. They...
I need a decision tree Consider a scenario in which a college must recruit students. They have three options. They can do nothing. Alternatively, they could make their own ad campaign, spending $200,000. Their campaign has an 70% chance of being successful, which would mean bringing in 100 new students at $40,000 per student in tuition. Or the college could decide to spend $800,000 to hire a social media company to do the ads. This social media campaign has a...
Write a simple Java code to make a Binary Tree for the following tree: Don’t use...
Write a simple Java code to make a Binary Tree for the following tree: Don’t use serializable interface implantation /** Class to encapsulate a tree node. */ protected static class Node implements Serializable {
R code Need assistance in getting this R code to run variables (Process) char ( Age)...
R code Need assistance in getting this R code to run variables (Process) char ( Age) char tmp <- expand.grid(Process = unique(words$Process),Age =unique(words$Age)) X <- model.matrix(~ factor(Process):factor(Age), data = tmp) glht(mod, linfct = X)
NEED IN C++ For the following programming problems, you need to time a section of code...
NEED IN C++ For the following programming problems, you need to time a section of code in C++. For example, the following statements time the execution of the function doSomething: #include clock_t start = clock(); doSomething(); clock_t finish = clock(); double overallTime = static_cast(finish - start)/ CLOCKS_PER_SEC; Consider the following two loops: //Loop A for(i = 1; i <= n; i++)    for(j = 1; j <= 10000; j++)     sum = sum + j; //Loop B for(i = 1;...
Explain how a firm may transition from the short-run production decision to the long-run production decision....
Explain how a firm may transition from the short-run production decision to the long-run production decision. In your explanation, us applicable graphs (6%) to illustrate short-run and long-run laws in operation. (10%)
What is the decision learning tree?
What is the decision learning tree?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT