som.nn.set: Set parameters for k-NN-like classifier in som.nn model

Description Usage Arguments Details Value See Also Examples

View source: R/som.nn.set.R

Description

Parameters for the k-NN-like classification can be set for an existing model of type SOMnn after training.

Usage

1
som.nn.set(model, x, dist.fun = NULL, max.dist = -1, name = "")

Arguments

model

model of type SOMnn.

x

data.fame with training data. Samples are requested as rows and taken randomly for the training steps. All columns except of the class lables are considered to be attributes and parts of the training vector. x must include the same columns as the data.frame with which the model have been trained originally. One column is needed as class labels. The column with class lables is selected by the slot class.idx of the model.

dist.fun

distance function for weighting distances between codebook vectors on the som (kernel for k-NN classifier).

max.dist

maximum distance to be considered by the nearest-neighbour counting.

name

new name of the model.

Details

The distance function defines the behaviour of the k-nearest-neighbour algorithm. Choices for the distance function include dist.fun.inverse or dist.fun.tricubic, as defined in this package, or any other function that accepts exactly two arguments x (the distance) and sigma (a parameter defined by max.distance).

A data set must be presented to calculate the accuracy statistics of the modified predictor.

Value

S4 object of type SOMnn with the updated model.

See Also

dist.fun.bubble, dist.fun.linear, dist.fun.inverse, dist.fun.tricubic.

Examples

 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
## get example data and add class labels:
data(iris)
species <- iris$Species

## train with default radius = diagonal / 2:
som <- som.nn.train(iris, class.col = "Species", 
                    xdim = 15, ydim = 9, alpha = 0.02, len = 10000, 
                    norm = TRUE, toroidal = FALSE)

## predict some samples:
unk <- iris[,!(names(iris) %in% "Species")]

setosa <- unk[species=="setosa",]
setosa <- setosa[sample(nrow(setosa), 20),]

versicolor <- unk[species=="versicolor",]
versicolor <- versicolor[sample(nrow(versicolor), 20),]

virginica <- unk[species=="virginica",]
virginica <- virginica[sample(nrow(virginica), 20),]

## predict with generic function:
predict(som, unk)

## predict with function in SOMnn object:
som@predict(setosa)
som@predict(versicolor)
som@predict(virginica)

## get mapping with visual:
som.nn.visual(som@codes, setosa)


## change parameters of k-NN classifier:
som.nn.set(som, iris, dist.fun = dist.fun.bubble, max.dist = 3.1)
som.nn.set(som, iris, dist.fun = dist.fun.tricubic, max.dist = 3)

## define custom distance function:
som <- som.nn.set(som, iris, dist.fun = function(x, sigma){
            ifelse(x < sigma, dnorm(x, sd = sigma), 0)}, 
            max.dist = 3)

predict(som, setosa)
som

som.nn documentation built on May 2, 2019, 8:26 a.m.