-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiris-classification.R
72 lines (55 loc) · 2.58 KB
/
iris-classification.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
61
62
63
64
65
66
67
68
69
70
71
# Importing libraries
library(datasets) # for iris dataset
library(caret) # for machine learning algorithms - classification and regression trees
# Importing dataset
data(iris)
# Checking the structure of the dataset
data(iris)
# Check to see if there are any missing values
any(is.na(iris))
# To achieve reproducible results, we set the seed
set.seed(999)
# Splitting the dataset into the Training set and Test set
# 70% of the dataset will be used for training and 30% for testing
TrainingIndex <- createDataPartition(iris$Species, p = 0.8, list = FALSE)
Training_set <- iris[TrainingIndex, ] # Training set
Test_set <- iris[-TrainingIndex, ] # Test set
# Compare the scatterplot of the training and test set
# Training set
plot(Training_set$Petal.Length, Training_set$Petal.Width, col = Training_set$Species, pch = 19, cex = 2)
legend("topright", legend = levels(Training_set$Species), col = 1:3, pch = 19)
# Test set
plot(Test_set$Petal.Length, Test_set$Species, col = Test_set$Species, pch = 19, cex = 2)
legend("topright", legend = levels(Test_set$Species), col = 1:3, pch = 19)
############################################
# SVM model (polynomial kernel)
# Build Training model
Model <- train(Species ~ ., data = Training_set,
method = "svmPoly",
na.action = na.omit,
preProcess = c("center", "scale"),
trControl = trainControl(method = "none"),
tuneGrid = data.frame(degree = 1, scale = 1, C = 1)
)
Model.cv <- train(Species ~ ., data = Training_set,
method = "svmPoly",
na.action = na.omit,
preProcess = c("center", "scale"),
trControl = trainControl(method = "cv", number = 10),
tuneGrid = data.frame(degree = 1, scale = 1, C = 1)
)
# Apply model for prediction
Model.training <- predict(Model, Training_set) # Apply model to make prediction on training set
Model.testing <- predict(Model, Test_set) # Apply model to make prediction on test set
Model.cv <- predict(Model.cv, Training_set) # Perform cross validation on training set
# Model performance (Display confusion matrix and statistics)
Model.training.confusion <- confusionMatrix(Model.training, Training_set$Species)
Model.testing.confusion <- confusionMatrix(Model.training, Training_set$Species)
Model.cv.confusion <- confusionMatrix(Model.cv, Training_set$Species)
print(Model.training.confusion)
print(Model.testing.confusion)
print(Model.cv.confusion)
# Feature importance
Importance <- varImp(Model)
plot(Importance)
plot(Importance, color = "red")