vignettes/examples/fitting_dmls.Rmd
fitting_dmls.Rmd
First, we load the needed libraries.
library(datasets)
library(rDML)
We will use the iris dataset.
# Loading dataset
data(iris)
X = iris[1:4]
y = iris[5][,1]
We construct the NCA transformer, and we fit it with the data in iris.
# DML construction
nca = NCA()
# Fitting algorithm
nca$fit(X,y)
Once fitted, we can look at the algorithm metadata.
# We can look at the algorithm metadata after fitting it
meta = nca$metadata()
meta
## $num_iters
## [1] 2
##
## $initial_expectance
## [1] 0.8380717
##
## $final_expectance
## [1] 0.9543913
Also we can see the metric or the transformed we have learned.
# We can see the metric the algorithm has learned
M = nca$metric()
M
## [,1] [,2] [,3] [,4]
## [1,] 1.268526 0.448331 -1.818768 -1.664831
## [2,] 0.448331 1.462379 -1.703565 -1.621492
## [3,] -1.818768 -1.703565 5.478263 4.644449
## [4,] -1.664831 -1.621492 4.644449 5.510808
#Equivalent, we can see the learned linear map
L = nca$transformer()
L
## [,1] [,2] [,3] [,4]
## [1,] 0.88997669 -0.01233273 -0.2769346 -0.1938964
## [2,] 0.04871365 1.05398850 -0.3566736 -0.3212260
## [3,] -0.54828900 -0.46170160 2.0509758 1.1765839
## [4,] -0.41650226 -0.37170808 1.0333693 1.9964158
We can use the transformer to map data to the space defined by the learned distance.
# Finally, we can obtain the transformed data ...
Lx = nca$transform()
Lx[1:5,]
## [,1] [,2] [,3] [,4]
## [1,] 4.069229 3.373811 -1.3055466 -1.579140
## [2,] 3.897400 2.837074 -0.9650380 -1.309985
## [3,] 3.744631 3.073796 -1.1528181 -1.404363
## [4,] 3.601480 2.892192 -0.6416239 -1.118868
## [5,] 3.978998 3.474339 -1.2968878 -1.574660
# ... or transform new data.
X_ = matrix(nrow = 3, ncol = 4, data = c(1,0,0,0,
1,1,0,0,
1,1,1,0))
Lx_ = nca$transform(X_)
Lx_
## [,1] [,2] [,3] [,4]
## [1,] 0.6960803 -0.2725123 0.6282949 1.5799135
## [2,] -0.2062292 0.7327625 0.7148823 1.6247077
## [3,] -0.2892673 0.6973149 1.5892742 0.6616613