-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mush_Code.txt
185 lines (144 loc) · 6.56 KB
/
Mush_Code.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#install.packages('rpart')
install.packages('finalfit')
library(finalfit)
library(ggplot2)
library(tidyverse)
library(gridExtra)
library(rpart)
library(rpart.plot)
library(naniar)
library(party)
library(dplyr)
library(e1071)
# options(warn=-1)
getwd()
setwd("C:\\Users\\srees\\OneDrive - Northumbria University - Production Azure AD\\Documents\\DS\\Assess")
getwd()
# Reading the csv file into mushroom and getting the header / Feature names
mushroom <- read.csv(file = 'mushrooms.csv', header = TRUE , stringsAsFactors = TRUE)
names(mushroom)
# Getting the summary of the attribute count in each of the features & Viewing the imported File
summary(mushroom)
library(naniar)
View(mushroom)
# Visual representation of the data set mushroom - 100% Present, nothing is missing
vis_miss(mushroom)
#To check the dimensions of the data
dim(mushroom)
str(mushroom)
summary(mushroom$stalk.root)
#Taking the backup the data set Mushroom & checking the distinct value of it
mush_backup <- mushroom
mush <- mushroom %>% distinct()
#fixing '?' error
mush$stalk.root <- mush$stalk.root %>% dplyr::na_if('?')
summary(mush$stalk.root)
mush <- mush[-11] # Removing the Stalk Root Feature as it has many NA's value
View(mush)
library(naniar)
vis_miss(mush)
#missing <- is.na(mush$stalk.root)
#missing_sum <- sum(missing == TRUE)
#missingness <- (missing_sum/8124)*100
#print(missingness)
#mcar_test(mush)
View(mush)
### Bar Graph for plotting a few features - Cap shape, Cap surface, Cap Color
ggplot(mush, aes(x = cap.shape, fill = class)) + geom_bar() + ggtitle("CAP SHAPE")
ggplot(mush, aes(x = cap.surface, fill = class)) + geom_bar() + ggtitle("CAP SURFACE")
ggplot(mush, aes(x = cap.color, fill = class)) + geom_bar() + ggtitle("CAP COLOR")
###Bar Graph plotting of few features - Bruises & Odor
ggplot(mush, aes(x = bruises, fill = class)) + geom_bar() + ggtitle("BRUISES")
ggplot(mush, aes(x = odor, fill = class)) + geom_bar() + ggtitle("ODOR")
### Bar Graph for plotting of few features - Gill Attachment, Gill Spacing, Gill Size, Gill Color
ggplot(mush, aes(x = gill.attachment, fill = class)) + geom_bar() + ggtitle("GILL ATTACHMENT")
ggplot(mush, aes(x = gill.spacing, fill = class)) + geom_bar() + ggtitle("GILL SPACING")
ggplot(mush, aes(x = gill.size, fill = class)) + geom_bar() + ggtitle("GILL SIZE")
ggplot(mush, aes(x = gill.color, fill = class)) + geom_bar() + ggtitle("GILL COLOR")
### Bar Graph for plotting of few features - Stalk Shape
ggplot(mush, aes(x = stalk.shape, fill = class)) + geom_bar() + ggtitle("STALK SHAPE")
### Bar Graph for plotting of few features - Stalk SURFACE above & below ring
s1 <- ggplot(mush, aes(x = stalk.surface.above.ring, fill = class)) + geom_bar() + ggtitle("Stalk Surface Above Ring")
s2 <- ggplot(mush, aes(x = stalk.surface.below.ring, fill = class)) + geom_bar() + ggtitle("Stalk Surface Below Ring")
# Arranging it in grid manner to compare the values and conclude values for the observations
grid.arrange(s1, s2) # ncol = 2)
grid.arrange(s1, s2, ncol = 2)
### Bar Graph for plotting of few features - Stalk COLOR above & below ring
s3 <- ggplot(mush, aes(x = stalk.color.above.ring, fill = class)) + geom_bar() + ggtitle("Stalk Color Above Ring")
s4 <- ggplot(mush, aes(x = stalk.color.below.ring, fill = class)) + geom_bar() + ggtitle("Stalk Color Below Ring")
# Arranging it in grid manner to compare the values and conclude values for the observations
grid.arrange(s3, s4) # ncol = 2)
grid.arrange(s3, s4, ncol = 2)
### Bar Graph for plotting of few features - Veil Type & Veil Color
ggplot(mush, aes(x = veil.type, fill = class)) + geom_bar() + ggtitle("VEIL TYPE")
ggplot(mush, aes(x = veil.color, fill = class)) + geom_bar() + ggtitle("VEIL COLOR")
### Bar Graph for plotting of few features - Ring Number & Type
ggplot(mush, aes(x = ring.number, fill = class)) + geom_bar() + ggtitle("Ring Number")
ggplot(mush, aes(x = ring.type, fill = class)) + geom_bar() + ggtitle("Ring Type")
### Bar Graph for plotting of few features - Spore Print Color, Population & Habitat
ggplot(mush, aes(x = spore.print.color, fill = class)) + geom_bar() + ggtitle("Spore Print Color")
ggplot(mush, aes(x = population, fill = class)) + geom_bar() + ggtitle("Population")
ggplot(mush, aes(x = habitat, fill = class)) + geom_bar() + ggtitle("Habitat")
# Only feature with 1 attribute
summary(mush$veil.type) # Completely redundant, Data present but no use in the data 1set
summary(mush$stalk.root) # Redundant as it has many values as NA
# Create a new data set - ROOM
room <- mush
View(room)
# Remove the features that are not require - //Mainly Veil Type//
room <- room[-15] # Removing the Veil Type Feature
View(room)
library(naniar)
vis_miss(room)
# 21 Columns are there in the ROOM data set
vis_miss(room)
View(room)
# Training and Test Data -- Split into 80% & 20%
set.seed(40)
room[,"train"] <- ifelse(runif(nrow(room))<0.8, 1, 0)
trainset <- room[room$train == "1",]
testset <- room[room$train == "0",]
trainset <- trainset[-22]
testset <- testset[-22]
## DECISION TREE ##
library(party)
library(dplyr)
library(rpart)
library(rpart.plot)
room_tree <- rpart(class~., data = trainset, method = "class")
rpart.plot(room_tree, extra = "auto")
test_data <- testset[-22]
tree_pred <- predict(room_tree, newdata = test_data, type = "class")
table(predicted = tree_pred, actual = testset$class)
mean(tree_pred==testset$class)
View(test_data)
View(trainset)
View(testset)
library(e1071)
# Setting the class to Boolean, considering Edible as '0' and Poisonous as '1'
room2 <- room
room2$class <- ifelse(room2$class == 'e', 0 , 1)
View(room2)
# Training and Test Data -- Split into 80% & 20% - Using for SVM
trainset1 <- room2[room2$train == "1",]
testset1 <- room2[room2$train == "0",]
trainset1 <- trainset1[-22]
testset1 <- testset1[-22]
# RANDOM FOREST
library(party)
forest_churn <- cforest(class~., data = trainset1, control = cforest_unbiased(mtry = 9, ntree = 50))
rf_prob <- predict(forest_churn, newdata = test_data, type = "response")
#print(rf_prob)
rf_pred <- ifelse(rf_prob > 0.5, 1, 0)
table(predicted = rf_pred, actual = testset1$class)
mean(rf_pred == testset1$class)
ForestVarImp <- varimp(forest_churn)
barplot(ForestVarImp)
# SVM
svm_trainset <- trainset1
svm_trainset$class <- as.factor(svm_trainset$class)
svm_churn <- svm(class~., data = svm_trainset)
summary(svm_churn)
svm_pred <- predict(svm_churn, newdata = test_data, type = "response")
table(predicted = svm_pred, actual = test_data$class)
mean(svm_pred==testset1$class)