-
Notifications
You must be signed in to change notification settings - Fork 0
/
Iris_ML.R
60 lines (39 loc) · 1.31 KB
/
Iris_ML.R
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
#Loading Dataset iris
data("iris")
head(iris)
tail(iris)
#Summary of Dataset
summary(iris)
#Stucture of Dataset
str(iris)
#Data Visualization with ggplot2
library(ggplot2)
#Pairs Plot
pairs(iris[, 1:4], main = "Iris Data", pch = 21, bg = c("green", "orange", "blue")[unclass(iris$Species)])
#ScatterPlot
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) + geom_point() + labs(title = "Sepal Length vs Sepal Width")
#Boxplot
ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) + geom_boxplot() + labs(title = "Boxplot of Sepal Length by Species")
# Data PreProcessing
sum(is.na(iris))
normalize <- function(x) {return ((x - min(x)) / (max(x) - min(x)))}
iris_normalized <- as.data.frame(lapply(iris[, 1:4], normalize))
iris_normalized$Species<- iris$Species
head(iris_normalized)
#Train-Test Split
library(caret)
set.seed(123)
trainIN<- createDataPartition(iris$Species, p=0.8, list= FALSE , times = 1)
Trainiris<- iris[trainIN,]
Testiris <- iris[trainIN,]
# Model Building
library(rpart)
#Model Trained by Decision Tree
modelo <- rpart( Species ~ . ,data = Trainiris , method = 'class')
# Plotting Decision Tree
plot(modelo)
text(modelo, use.n =TRUE)
# Evaluating The Model
prediction <- predict(modelo, Testiris, type='class')
confusionMatrix(prediction, Testiris$Species)
print(modelo)