-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSML.R
More file actions
64 lines (48 loc) · 2.04 KB
/
SML.R
File metadata and controls
64 lines (48 loc) · 2.04 KB
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
###############################
# Supervised Machine Learning #
###############################
# Zusatzprogramm CARET installieren und aktivieren
install.packages("caret", dependencies=TRUE)
library(caret)
# Features anlegen
x <- iris[,1:4]
y <- iris[,5]
# Grafische Darstellung der Features
featurePlot(x=x, y=y, plot="box")
# Manuelle Klassifikation von Schwertlilienarten
classification <- subset(iris, Petal.Length <= "2" & Petal.Width <= "1", select=c(Species))
summary(classification)
# Grafische Darstellung über SEPAL.WIDTH und SEPAL.LENGTH
shapes=c(1,0,20)
shapes <- shapes[as.numeric(iris$Species)]
plot(x=iris$Sepal.Length, y=iris$Sepal.Width, frame=FALSE, xlab="Sepal Length", ylab="Sepal Width", pch=shapes)
legend("topright", legend=levels(iris$Species), pch=c(1,0,20))
# Training Data und Validation Data anlegen
validation_index <- createDataPartition(iris$Species, p=0.80, list=FALSE)
validation <- iris[-validation_index,]
model <- iris[validation_index,]
# Training Data und Validation Data einsehen
summary(validation)
summary(model)
# Validierung festlegen
control <- trainControl(method="cv", number=10)
metric <- "Accuracy"
# Linear Discriminant Analysis trainieren
fit.lda <- train(Species~., data=model, method="lda", metric=metric, trControl=control)
# K-Nearest Neighbors trainieren
fit.knn <- train(Species~., data=model, method="knn", metric=metric, trControl=control)
# Random Forest trainieren
fit.rf <- train(Species~., data=model, method="rf", metric=metric, trControl=control)
# Vergleich der Machine Learning Algorithmen
results <- resamples(list(lda=fit.lda, knn=fit.knn, rf=fit.rf))
summary(results)
# Grafische Darstellung des Vergleichs der Machine Learning Algorithmen
dotplot(results)
# Fokus auf besten Algorithmus
print(fit.lda)
# Machine Learning Algorithmus auf Validation Data anwenden
predictions <- predict(fit.lda, validation)
confusionMatrix(predictions, validation$Species)
# Machine Learning Algorithmus auf gesamten Datensatz anwenden
predictions <- predict(fit.lda, iris)
confusionMatrix(predictions, iris$Species)