Skip to content

Instantly share code, notes, and snippets.

@TheRayTracer
Last active October 6, 2022 20:30
Show Gist options
  • Save TheRayTracer/de8d095fcc1d8a733c4a to your computer and use it in GitHub Desktop.
Save TheRayTracer/de8d095fcc1d8a733c4a to your computer and use it in GitHub Desktop.
Using R to explore the UCI mushroom dataset reveals excellent KNN prediction results.
# Load our data set.
mushroom = read.table("mushrooms.data", header = TRUE, sep = ",");
mushroom$class = as.character(mushroom$class);
mushroom$class[mushroom$class == "e"] = "edible";
mushroom$class[mushroom$class == "p"] = "poisonous";
mushroom$class = as.factor(mushroom$class);
# Remove incomplete data.
mushroom$veiltype = NULL;
mushroom$stalkroot = NULL;
# Load libraries.
library(ggplot2);
library(caret);
library(gridExtra);
set.seed(100);
# Inspect our data set.
names(mushroom);
head(mushroom);
# Explore the data.
qp1 = qplot(capcolor, habitat, color = class, data = mushroom, geom = "jitter", main = "Mushroom data set - Cap color vs Habitat");
qp2 = qplot(capcolor, habitat, color = class, data = mushroom, geom = "jitter", facets = bruises ~ ., main = "Mushroom data set by Bruises - Cap color vs Habitat");
qp3 = qplot(odor, sporeprintcolor, color = class, data = mushroom, geom = "jitter", main = "Mushroom data set - Odor vs Spore Print Color");
qp4 = qplot(stalksurfacebelowring, stalkcolorabovering, color = class, data = mushroom, geom = "jitter", main = "Mushroom data set - Stalk Surface below Ring vs Stalk Color above Ring");
grid.arrange(qp1, qp2, qp3, qp4, ncol = 2, nrow = 2);
# Explore the overlap where there is no odor and a spore print color of white.
mushroom$class = as.character(mushroom$class)
mushroom$class[mushroom$odor == "n" & mushroom$sporeprintcolor == "w" & mushroom$class == "poisonous"] = "poisonous2"
mushroom$class[mushroom$odor == "n" & mushroom$sporeprintcolor == "w" & mushroom$class == "edible"] = "edible2"
mushroom$class = as.factor(mushroom$class)
qp5 = qplot(capcolor, habitat, color = class, data = mushroom, geom = "jitter", main = "Mushroom data set - Cap color vs Habitat");
qp6 = qplot(capcolor, habitat, color = class, data = mushroom, geom = "jitter", facets = bruises ~ ., main = "Mushroom data set by Bruises - Cap color vs Habitat");
qp7 = qplot(odor, sporeprintcolor, color = class, data = mushroom, geom = "jitter", main = "Mushroom data set - Odor vs Spore Print Color");
qp8 = qplot(stalksurfacebelowring, stalkcolorabovering, color = class, data = mushroom, geom = "jitter", main = "Mushroom data set - Stalk Surface below Ring vs Stalk Color above Ring");
grid.arrange(qp5, qp6, qp7, qp8, ncol = 2, nrow = 2);
# Restore.
mushroom$class = as.character(mushroom$class);
mushroom$class[mushroom$class == "edible2"] = "edible";
mushroom$class[mushroom$class == "poisonous2"] = "poisonous";
mushroom$class = as.factor(mushroom$class);
# Split our data into a training (70%) and test (30%) set.
training_split = createDataPartition(y = mushroom$class, p = 0.70, list = FALSE);
training_set = mushroom[training_split,];
testing_set = mushroom[-training_split,];
model_fit = train(class ~ ., method = "knn", data = training_set, trControl = trainControl(method = 'cv', number = 10, classProbs = TRUE));
print(model_fit);
plot(model_fit);
# Classify from our reserved test set.
testing_set_predict = predict(model_fit, newdata = testing_set[, -1]); # Remove the Class field to prove we are not cheating.
# Verifying our model from the classifications.
table(testing_set_predict, testing_set$class);
testing_set$Correct = testing_set_predict == testing_set$class;
accuracy = length(testing_set$Correct[testing_set$Correct == TRUE]) / length(testing_set$Correct);
paste("Training accuracy:", accuracy);
important_features = varImp(model_fit, scale = FALSE);
print(important_features);
plot(important_features);
@sinhavartika
Copy link

The visualization is very interesting. Can we do the same with python?
Also what did you learn from the data exploration? How did it help in classification?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment