0% found this document useful (0 votes)
17 views

Decision Tree

gg

Uploaded by

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

Decision Tree

gg

Uploaded by

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

```r

Install and load required packages


if (!require(party)) install.packages("party", dependencies = TRUE) library(party)

Set the working directory to the location of your


file
setwd("C:/Users/user.DESKTOP-US9GTFN.003/Desktop")

Construct the file path


file_path <- "C:/Users/user.DESKTOP-US9GTFN.003/Desktop/mushrooms.csv"

Check if the file exists


if (file.exists(file_path)) { # Load the dataset data <- read.csv(file_path)

# Check the first few rows of the dataset print("First few rows of the dataset:") print(head(data))

# Check the structure of the dataset print("Structure of the dataset:") str(data)

# Convert all columns to factors data[] <- lapply(data, as.factor)

# Build the decision tree model using ctree model_ctree <- ctree(class ~ ., data = data)

# Print model summary print("Conditional Inference Tree Model Summary:") print(model_ctree)

# Plot the decision tree print("Plotting the decision tree:") plot(model_ctree, main = "Conditional
Inference Tree for Mushroom Classification")

# Make predictions using the decision tree model predictions <- predict(model_ctree, data)

# Add predictions to the dataset data$PredictedClass <- predictions

# View the updated dataset with predictions print("Updated dataset with predictions:")
print(head(data))

# Create and print the confusion matrix confusion_matrix <- table(Predicted = predictions,
Actual = data$class) print("Confusion Matrix:") print(confusion_matrix)

# Calculate and print accuracy accuracy <- sum(diag(confusion_matrix)) / sum(confusion_matrix)


print(paste("Accuracy:", round(accuracy, 2))) } else { stop("File not found") }

You might also like