In this post, I'll show a classification method by using 'mxnet' library in R. You may find more info about the library in this link.
We use an 'iris' dataset for this tutorial. First, we separate a dataset into train and test parts, then fit a model with a train data, predict a test data, and finally, check the results. To split a dataset and calculate the statistics of observed and predicted data, we use 'caret' library.
Here is a source code in R.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# classification with mxnet | |
library(mxnet) | |
library(caret) | |
data(iris) | |
# split data into train and test part (90/10). | |
indexes = createDataPartition(iris$Species, p = 0.9, list = F) | |
train <- iris[indexes, ] | |
test <- iris[-indexes, ] | |
# x part of data should be as a matrix type | |
train.x <- data.matrix(train[, 1:4]) | |
train.y <- as.numeric(train[, 5]) - 1 # to start from 0,1,2 order | |
test.x <- data.matrix(test[, 1:4]) | |
test.y <- as.numeric(test[, 5]) - 1 | |
mx.set.seed(0) | |
model <- mx.mlp(train.x, train.y, | |
hidden_node = 10, | |
out_node = 3, | |
out_activation = "softmax", | |
num.round = 100, | |
array.batch.size = 10, | |
learning.rate = 0.01, | |
momentum = 0.9, | |
eval.metric = mx.metric.accuracy, | |
array.layout = "rowmajor") | |
pred <- predict(model, test.x, array.layout = "rowmajor") | |
pred.label <- max.col(t(pred)) - 1 | |
cfm <- confusionMatrix(as.factor(pred.label), as.factor(test.y)) | |
print(cfm) |
Start training with 1 devices [1] Train-accuracy=0.230769230769231 [2] Train-accuracy=0.507142857142857 [3] Train-accuracy=0.407142857142857 [4] Train-accuracy=0.514285714285714 [5] Train-accuracy=0.678571428571429 [6] Train-accuracy=0.678571428571429
......
[93] Train-accuracy=0.95 [94] Train-accuracy=0.957142857142857 [95] Train-accuracy=0.971428571428572 [96] Train-accuracy=0.978571428571429 [97] Train-accuracy=0.964285714285714 [98] Train-accuracy=0.95 [99] Train-accuracy=0.95 [100] Train-accuracy=0.95 Confusion Matrix and Statistics Reference Prediction 0 1 2 0 5 0 0 1 0 5 0 2 0 0 5 Overall Statistics Accuracy : 1 95% CI : (0.782, 1) No Information Rate : 0.3333 P-Value [Acc > NIR] : 6.969e-08 Kappa : 1 Mcnemar's Test P-Value : NA Statistics by Class: Class: 0 Class: 1 Class: 2 Sensitivity 1.0000 1.0000 1.0000 Specificity 1.0000 1.0000 1.0000 Pos Pred Value 1.0000 1.0000 1.0000 Neg Pred Value 1.0000 1.0000 1.0000 Prevalence 0.3333 0.3333 0.3333 Detection Rate 0.3333 0.3333 0.3333 Detection Prevalence 0.3333 0.3333 0.3333 Balanced Accuracy 1.0000 1.0000 1.0000
The model training accuracy has reached 100%, and test data has predicted with 100% accuracy. I hope you have found the post useful. Thank you for reading!
No comments:
Post a Comment