# DAL ToolBox
# version 1.01.727
source("https://raw.githubusercontent.com/cefet-rj-dal/daltoolbox/main/jupyter.R")
#loading DAL
load_library("daltoolbox")
Loading required package: daltoolbox Registered S3 method overwritten by 'quantmod': method from as.zoo.data.frame zoo Attaching package: ‘daltoolbox’ The following object is masked from ‘package:base’: transform
iris <- datasets::iris
head(iris)
Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species | |
---|---|---|---|---|---|
<dbl> | <dbl> | <dbl> | <dbl> | <fct> | |
1 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
2 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
3 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
4 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
5 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
6 | 5.4 | 3.9 | 1.7 | 0.4 | setosa |
#extracting the levels for the dataset
slevels <- levels(iris$Species)
slevels
#for performance issues, you can use matrix instead of data.frame (uncomment next line to test)
iris <- cbind(as.matrix(iris[,1:4]), Species=iris$Species)
# preparing dataset for random sampling
set.seed(1)
sr <- sample_random()
sr <- train_test(sr, iris)
iris_train <- sr$train
iris_test <- sr$test
tbl <- rbind(table(iris[,"Species"]),
table(iris_train[,"Species"]),
table(iris_test[,"Species"]))
rownames(tbl) <- c("dataset", "training", "test")
head(tbl)
1 | 2 | 3 | |
---|---|---|---|
dataset | 50 | 50 | 50 |
training | 39 | 38 | 43 |
test | 11 | 12 | 7 |
model <- cla_mlp("Species", slevels, size=3,decay=0.03)
model <- fit(model, iris_train)
train_prediction <- predict(model, iris_train)
iris_train_predictand <- adjust_class_label(iris_train[,"Species"])
train_eval <- evaluate(model, iris_train_predictand, train_prediction)
print(train_eval$metrics)
accuracy TP TN FP FN precision recall sensitivity specificity f1 1 0.9833333 39 81 0 0 1 1 1 1 1
# Test
test_prediction <- predict(model, iris_test)
iris_test_predictand <- adjust_class_label(iris_test[,"Species"])
test_eval <- evaluate(model, iris_test_predictand, test_prediction)
print(test_eval$metrics)
accuracy TP TN FP FN precision recall sensitivity specificity f1 1 0.9666667 11 19 0 0 1 1 1 1 1