library(gmum.r)
library(caret) # For ConfusionMatrix
wine <- get.wine.dataset.X(scale=TRUE)
# Train in an offline manner
gng <- GNG(wine, labels=get.wine.dataset.y(), max.nodes=20,
max.iter=10000, min.improvement=1e-1)
## [1] "Training offline"
## [1] "Iteration 10000"
## [1] "Iteration 10000"
# Print number of nodes
numberNodes(gng)
## [1] 20
# Convert to igraph directly!
ig = convertToIGraph(gng)
# Print mean degree of the network
mean(degree(ig))
## [1] 5.2
# You can access different attributes of the nodes
V(ig)$error[1]
## [1] 7037.935
# Plot using igraph layout
plot(gng, vertex.color="label", layout=igraph::layout.fruchterman.reingold,
vertex.size=9)

# Print summary of trained object
summary(gng)
## [1] "Growing Neural Gas"
## [1] "20 nodes with mean error 3.476649"
## [1] "Trained 10000 iterations"
## [1] "Mean errors[s]: "
## [1] 3.430407 3.457238 3.505368 3.497008 3.618063 3.425084 3.373586
## [8] 3.515994 3.477688 3.521820 3.476649
# You can use graph to predict new samples
# (in a closest neighbour way)
preds <- c()
for(i in 1:nrow(wine)){
preds <- c(preds,round(node(gng, predict(gng, wine[i,]))$label))
}
# Print prediction statistics
confusionMatrix(table(preds, get.wine.dataset.y()))
## Confusion Matrix and Statistics
##
##
## preds 1 2 3
## 1 59 2 0
## 2 0 51 0
## 3 0 18 48
##
## Overall Statistics
##
## Accuracy : 0.8876
## 95% CI : (0.8318, 0.93)
## No Information Rate : 0.3989
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.8328
## Mcnemar's Test P-Value : NA
##
## Statistics by Class:
##
## Class: 1 Class: 2 Class: 3
## Sensitivity 1.0000 0.7183 1.0000
## Specificity 0.9832 1.0000 0.8615
## Pos Pred Value 0.9672 1.0000 0.7273
## Neg Pred Value 1.0000 0.8425 1.0000
## Prevalence 0.3315 0.3989 0.2697
## Detection Rate 0.3315 0.2865 0.2697
## Detection Prevalence 0.3427 0.2865 0.3708
## Balanced Accuracy 0.9916 0.8592 0.9308