# kernelMatrix: Kernel Matrix functions In kernlab: Kernel-Based Machine Learning Lab

## Description

`kernelMatrix` calculates the kernel matrix K_{ij} = k(x_i,x_j) or K_{ij} = k(x_i,y_j).
`kernelPol` computes the quadratic kernel expression H = z_i z_j k(x_i,x_j), H = z_i k_j k(x_i,y_j).
`kernelMult` calculates the kernel expansion f(x_i) = ∑_{i=1}^m z_i k(x_i,x_j)
`kernelFast` computes the kernel matrix, identical to `kernelMatrix`, except that it also requires the squared norm of the first argument as additional input, useful in iterative kernel matrix calculations.

## Usage

 ``` 1 2 3 4 5 6 7 8 9 10 11``` ```## S4 method for signature 'kernel' kernelMatrix(kernel, x, y = NULL) ## S4 method for signature 'kernel' kernelPol(kernel, x, y = NULL, z, k = NULL) ## S4 method for signature 'kernel' kernelMult(kernel, x, y = NULL, z, blocksize = 256) ## S4 method for signature 'kernel' kernelFast(kernel, x, y, a) ```

## Arguments

 `kernel` the kernel function to be used to calculate the kernel matrix. This has to be a function of class `kernel`, i.e. which can be generated either one of the build in kernel generating functions (e.g., `rbfdot` etc.) or a user defined function of class `kernel` taking two vector arguments and returning a scalar. `x` a data matrix to be used to calculate the kernel matrix, or a list of vector when a `stringkernel` is used `y` second data matrix to calculate the kernel matrix, or a list of vector when a `stringkernel` is used `z` a suitable vector or matrix `k` a suitable vector or matrix `a` the squared norm of `x`, e.g., `rowSums(x^2)` `blocksize` the kernel expansion computations are done block wise to avoid storing the kernel matrix into memory. `blocksize` defines the size of the computational blocks.

## Details

Common functions used during kernel based computations.
The `kernel` parameter can be set to any function, of class kernel, which computes the inner product in feature space between two vector arguments. kernlab provides the most popular kernel functions which can be initialized by using the following functions:

• `rbfdot` Radial Basis kernel function

• `polydot` Polynomial kernel function

• `vanilladot` Linear kernel function

• `tanhdot` Hyperbolic tangent kernel function

• `laplacedot` Laplacian kernel function

• `besseldot` Bessel kernel function

• `anovadot` ANOVA RBF kernel function

• `splinedot` the Spline kernel

(see example.)

`kernelFast` is mainly used in situations where columns of the kernel matrix are computed per invocation. In these cases, evaluating the norm of each row-entry over and over again would cause significant computational overhead.

## Value

`kernelMatrix` returns a symmetric diagonal semi-definite matrix.
`kernelPol` returns a matrix.
`kernelMult` usually returns a one-column matrix.

## Author(s)

Alexandros Karatzoglou
alexandros.karatzoglou@ci.tuwien.ac.at

`rbfdot`, `polydot`, `tanhdot`, `vanilladot`

## Examples

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19``` ```## use the spam data data(spam) dt <- as.matrix(spam[c(10:20,3000:3010),-58]) ## initialize kernel function rbf <- rbfdot(sigma = 0.05) rbf ## calculate kernel matrix kernelMatrix(rbf, dt) yt <- as.matrix(as.integer(spam[c(10:20,3000:3010),58])) yt[yt==2] <- -1 ## calculate the quadratic kernel expression kernelPol(rbf, dt, ,yt) ## calculate the kernel expansion kernelMult(rbf, dt, ,yt) ```

### Example output

```Gaussian Radial Basis kernel function.
Hyperparameter : sigma =  0.05
An object of class "kernelMatrix"
10            11            12            13            14
10   1.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00
11   0.000000e+00  1.000000e+00  0.000000e+00  0.000000e+00  3.076372e-01
12   0.000000e+00  0.000000e+00  1.000000e+00 5.576760e-184  0.000000e+00
13   0.000000e+00  0.000000e+00 5.576760e-184  1.000000e+00  0.000000e+00
14   0.000000e+00  3.076372e-01  0.000000e+00  0.000000e+00  1.000000e+00
15   0.000000e+00  0.000000e+00  7.620012e-15  2.283705e-99  0.000000e+00
16   0.000000e+00  0.000000e+00 4.354479e-135  6.728227e-05  0.000000e+00
17   0.000000e+00 1.759111e-175 1.580090e-138  0.000000e+00 1.474439e-159
18   0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00
19   0.000000e+00  3.704851e-53 4.048455e-284  0.000000e+00  5.620206e-45
20   0.000000e+00  0.000000e+00 2.375087e-140 1.144518e-142  0.000000e+00
3000 0.000000e+00  0.000000e+00  0.000000e+00 9.823504e-126  0.000000e+00
3001 0.000000e+00  4.157797e-25  0.000000e+00  0.000000e+00  1.629625e-19
3002 0.000000e+00  1.584893e-07  0.000000e+00  0.000000e+00  1.153939e-04
3003 0.000000e+00  2.485273e-06  0.000000e+00  0.000000e+00  3.459807e-09
3004 0.000000e+00  0.000000e+00  3.802475e-19 5.318413e-295  0.000000e+00
3005 0.000000e+00 6.080875e-113 2.419462e-191  0.000000e+00 2.324676e-100
3006 0.000000e+00  2.141277e-36  0.000000e+00  0.000000e+00  5.957980e-30
3007 0.000000e+00  1.347476e-05  0.000000e+00  0.000000e+00  7.850063e-08
3008 0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00
3009 0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00
3010 1.836994e-45  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00
15            16            17            18            19
10    0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00
11    0.000000e+00  0.000000e+00 1.759111e-175  0.000000e+00  3.704851e-53
12    7.620012e-15 4.354479e-135 1.580090e-138  0.000000e+00 4.048455e-284
13    2.283705e-99  6.728227e-05  0.000000e+00  0.000000e+00  0.000000e+00
14    0.000000e+00  0.000000e+00 1.474439e-159  0.000000e+00  5.620206e-45
15    1.000000e+00  1.700705e-64 3.198028e-211  0.000000e+00  0.000000e+00
16    1.700705e-64  1.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00
17   3.198028e-211  0.000000e+00  1.000000e+00  0.000000e+00  1.131156e-46
18    0.000000e+00  0.000000e+00  0.000000e+00  1.000000e+00  0.000000e+00
19    0.000000e+00  0.000000e+00  1.131156e-46  0.000000e+00  1.000000e+00
20   4.909368e-107 1.406121e-115 7.630353e-215  0.000000e+00  0.000000e+00
3000 1.748673e-300 5.672366e-150  0.000000e+00  0.000000e+00  0.000000e+00
3001  0.000000e+00  0.000000e+00  5.322112e-77  0.000000e+00  8.829844e-07
3002  0.000000e+00  0.000000e+00 3.492160e-118  0.000000e+00  2.787511e-23
3003  0.000000e+00  0.000000e+00 1.069177e-239  0.000000e+00  5.288180e-90
3004  1.076909e-58 1.857644e-231  3.153752e-58  0.000000e+00 1.161063e-159
3005 6.669696e-284  0.000000e+00  1.473126e-08  0.000000e+00  1.980755e-17
3006  0.000000e+00  0.000000e+00  4.958692e-65  0.000000e+00  1.809605e-22
3007  0.000000e+00  0.000000e+00 3.077777e-225  0.000000e+00  1.125459e-80
3008  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00
3009  0.000000e+00  0.000000e+00  0.000000e+00 4.882814e-270  0.000000e+00
3010  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00
20          3000          3001          3002          3003
10    0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00
11    0.000000e+00  0.000000e+00  4.157797e-25  1.584893e-07  2.485273e-06
12   2.375087e-140  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00
13   1.144518e-142 9.823504e-126  0.000000e+00  0.000000e+00  0.000000e+00
14    0.000000e+00  0.000000e+00  1.629625e-19  1.153939e-04  3.459807e-09
15   4.909368e-107 1.748673e-300  0.000000e+00  0.000000e+00  0.000000e+00
16   1.406121e-115 5.672366e-150  0.000000e+00  0.000000e+00  0.000000e+00
17   7.630353e-215  0.000000e+00  5.322112e-77 3.492160e-118 1.069177e-239
18    0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00
19    0.000000e+00  0.000000e+00  8.829844e-07  2.787511e-23  5.288180e-90
20    1.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00
3000  0.000000e+00  1.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00
3001  0.000000e+00  0.000000e+00  1.000000e+00  1.352868e-06  4.823404e-51
3002  0.000000e+00  0.000000e+00  1.352868e-06  1.000000e+00  2.576800e-23
3003  0.000000e+00  0.000000e+00  4.823404e-51  2.576800e-23  1.000000e+00
3004 2.356445e-154  0.000000e+00 7.945641e-224 3.305267e-299  0.000000e+00
3005 9.713541e-301  0.000000e+00  3.387569e-37  1.378134e-67 8.914794e-166
3006  0.000000e+00  0.000000e+00  2.663515e-15  2.320201e-19  1.902969e-65
3007  0.000000e+00  0.000000e+00  3.061563e-44  1.242501e-19  1.643562e-02
3008  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00
3009  0.000000e+00  5.593377e-93  0.000000e+00  0.000000e+00  0.000000e+00
3010  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00
3004          3005          3006          3007 3008          3009
10    0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00    0  0.000000e+00
11    0.000000e+00 6.080875e-113  2.141277e-36  1.347476e-05    0  0.000000e+00
12    3.802475e-19 2.419462e-191  0.000000e+00  0.000000e+00    0  0.000000e+00
13   5.318413e-295  0.000000e+00  0.000000e+00  0.000000e+00    0  0.000000e+00
14    0.000000e+00 2.324676e-100  5.957980e-30  7.850063e-08    0  0.000000e+00
15    1.076909e-58 6.669696e-284  0.000000e+00  0.000000e+00    0  0.000000e+00
16   1.857644e-231  0.000000e+00  0.000000e+00  0.000000e+00    0  0.000000e+00
17    3.153752e-58  1.473126e-08  4.958692e-65 3.077777e-225    0  0.000000e+00
18    0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00    0 4.882814e-270
19   1.161063e-159  1.980755e-17  1.809605e-22  1.125459e-80    0  0.000000e+00
20   2.356445e-154 9.713541e-301  0.000000e+00  0.000000e+00    0  0.000000e+00
3000  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00    0  5.593377e-93
3001 7.945641e-224  3.387569e-37  2.663515e-15  3.061563e-44    0  0.000000e+00
3002 3.305267e-299  1.378134e-67  2.320201e-19  1.242501e-19    0  0.000000e+00
3003  0.000000e+00 8.914794e-166  1.902969e-65  1.643562e-02    0  0.000000e+00
3004  1.000000e+00  1.023004e-91 3.147192e-234  0.000000e+00    0  0.000000e+00
3005  1.023004e-91  1.000000e+00  2.479034e-35 2.698841e-153    0  0.000000e+00
3006 3.147192e-234  2.479034e-35  1.000000e+00  2.494577e-60    0  0.000000e+00
3007  0.000000e+00 2.698841e-153  2.494577e-60  1.000000e+00    0  0.000000e+00
3008  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00    1  0.000000e+00
3009  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00    0  1.000000e+00
3010  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00    0  0.000000e+00
3010
10   1.836994e-45
11   0.000000e+00
12   0.000000e+00
13   0.000000e+00
14   0.000000e+00
15   0.000000e+00
16   0.000000e+00
17   0.000000e+00
18   0.000000e+00
19   0.000000e+00
20   0.000000e+00
3000 0.000000e+00
3001 0.000000e+00
3002 0.000000e+00
3003 0.000000e+00
3004 0.000000e+00
3005 0.000000e+00
3006 0.000000e+00
3007 0.000000e+00
3008 0.000000e+00
3009 0.000000e+00
3010 1.000000e+00
10             11             12             13             14
10    1.000000e+00   0.000000e+00   0.000000e+00   0.000000e+00   0.000000e+00
11    0.000000e+00   1.000000e+00   0.000000e+00   0.000000e+00   3.076372e-01
12    0.000000e+00   0.000000e+00   1.000000e+00  5.576760e-184   0.000000e+00
13    0.000000e+00   0.000000e+00  5.576760e-184   1.000000e+00   0.000000e+00
14    0.000000e+00   3.076372e-01   0.000000e+00   0.000000e+00   1.000000e+00
15    0.000000e+00   0.000000e+00   7.620012e-15   2.283705e-99   0.000000e+00
16    0.000000e+00   0.000000e+00  4.354479e-135   6.728227e-05   0.000000e+00
17    0.000000e+00  1.759111e-175  1.580090e-138   0.000000e+00  1.474439e-159
18    0.000000e+00   0.000000e+00   0.000000e+00   0.000000e+00   0.000000e+00
19    0.000000e+00   3.704851e-53  4.048455e-284   0.000000e+00   5.620206e-45
20    0.000000e+00   0.000000e+00  2.375087e-140  1.144518e-142   0.000000e+00
3000  0.000000e+00   0.000000e+00   0.000000e+00 -9.823504e-126   0.000000e+00
3001  0.000000e+00  -4.157797e-25   0.000000e+00   0.000000e+00  -1.629625e-19
3002  0.000000e+00  -1.584893e-07   0.000000e+00   0.000000e+00  -1.153939e-04
3003  0.000000e+00  -2.485273e-06   0.000000e+00   0.000000e+00  -3.459807e-09
3004  0.000000e+00   0.000000e+00  -3.802475e-19 -5.318413e-295   0.000000e+00
3005  0.000000e+00 -6.080875e-113 -2.419462e-191   0.000000e+00 -2.324676e-100
3006  0.000000e+00  -2.141277e-36   0.000000e+00   0.000000e+00  -5.957980e-30
3007  0.000000e+00  -1.347476e-05   0.000000e+00   0.000000e+00  -7.850063e-08
3008  0.000000e+00   0.000000e+00   0.000000e+00   0.000000e+00   0.000000e+00
3009  0.000000e+00   0.000000e+00   0.000000e+00   0.000000e+00   0.000000e+00
3010 -1.836994e-45   0.000000e+00   0.000000e+00   0.000000e+00   0.000000e+00
15             16             17             18             19
10     0.000000e+00   0.000000e+00   0.000000e+00   0.000000e+00   0.000000e+00
11     0.000000e+00   0.000000e+00  1.759111e-175   0.000000e+00   3.704851e-53
12     7.620012e-15  4.354479e-135  1.580090e-138   0.000000e+00  4.048455e-284
13     2.283705e-99   6.728227e-05   0.000000e+00   0.000000e+00   0.000000e+00
14     0.000000e+00   0.000000e+00  1.474439e-159   0.000000e+00   5.620206e-45
15     1.000000e+00   1.700705e-64  3.198028e-211   0.000000e+00   0.000000e+00
16     1.700705e-64   1.000000e+00   0.000000e+00   0.000000e+00   0.000000e+00
17    3.198028e-211   0.000000e+00   1.000000e+00   0.000000e+00   1.131156e-46
18     0.000000e+00   0.000000e+00   0.000000e+00   1.000000e+00   0.000000e+00
19     0.000000e+00   0.000000e+00   1.131156e-46   0.000000e+00   1.000000e+00
20    4.909368e-107  1.406121e-115  7.630353e-215   0.000000e+00   0.000000e+00
3000 -1.748673e-300 -5.672366e-150   0.000000e+00   0.000000e+00   0.000000e+00
3001   0.000000e+00   0.000000e+00  -5.322112e-77   0.000000e+00  -8.829844e-07
3002   0.000000e+00   0.000000e+00 -3.492160e-118   0.000000e+00  -2.787511e-23
3003   0.000000e+00   0.000000e+00 -1.069177e-239   0.000000e+00  -5.288180e-90
3004  -1.076909e-58 -1.857644e-231  -3.153752e-58   0.000000e+00 -1.161063e-159
3005 -6.669696e-284   0.000000e+00  -1.473126e-08   0.000000e+00  -1.980755e-17
3006   0.000000e+00   0.000000e+00  -4.958692e-65   0.000000e+00  -1.809605e-22
3007   0.000000e+00   0.000000e+00 -3.077777e-225   0.000000e+00  -1.125459e-80
3008   0.000000e+00   0.000000e+00   0.000000e+00   0.000000e+00   0.000000e+00
3009   0.000000e+00   0.000000e+00   0.000000e+00 -4.882814e-270   0.000000e+00
3010   0.000000e+00   0.000000e+00   0.000000e+00   0.000000e+00   0.000000e+00
20           3000           3001           3002           3003
10     0.000000e+00   0.000000e+00   0.000000e+00   0.000000e+00   0.000000e+00
11     0.000000e+00   0.000000e+00  -4.157797e-25  -1.584893e-07  -2.485273e-06
12    2.375087e-140   0.000000e+00   0.000000e+00   0.000000e+00   0.000000e+00
13    1.144518e-142 -9.823504e-126   0.000000e+00   0.000000e+00   0.000000e+00
14     0.000000e+00   0.000000e+00  -1.629625e-19  -1.153939e-04  -3.459807e-09
15    4.909368e-107 -1.748673e-300   0.000000e+00   0.000000e+00   0.000000e+00
16    1.406121e-115 -5.672366e-150   0.000000e+00   0.000000e+00   0.000000e+00
17    7.630353e-215   0.000000e+00  -5.322112e-77 -3.492160e-118 -1.069177e-239
18     0.000000e+00   0.000000e+00   0.000000e+00   0.000000e+00   0.000000e+00
19     0.000000e+00   0.000000e+00  -8.829844e-07  -2.787511e-23  -5.288180e-90
20     1.000000e+00   0.000000e+00   0.000000e+00   0.000000e+00   0.000000e+00
3000   0.000000e+00   1.000000e+00   0.000000e+00   0.000000e+00   0.000000e+00
3001   0.000000e+00   0.000000e+00   1.000000e+00   1.352868e-06   4.823404e-51
3002   0.000000e+00   0.000000e+00   1.352868e-06   1.000000e+00   2.576800e-23
3003   0.000000e+00   0.000000e+00   4.823404e-51   2.576800e-23   1.000000e+00
3004 -2.356445e-154   0.000000e+00  7.945641e-224  3.305267e-299   0.000000e+00
3005 -9.713541e-301   0.000000e+00   3.387569e-37   1.378134e-67  8.914794e-166
3006   0.000000e+00   0.000000e+00   2.663515e-15   2.320201e-19   1.902969e-65
3007   0.000000e+00   0.000000e+00   3.061563e-44   1.242501e-19   1.643562e-02
3008   0.000000e+00   0.000000e+00   0.000000e+00   0.000000e+00   0.000000e+00
3009   0.000000e+00   5.593377e-93   0.000000e+00   0.000000e+00   0.000000e+00
3010   0.000000e+00   0.000000e+00   0.000000e+00   0.000000e+00   0.000000e+00
3004           3005           3006           3007 3008
10     0.000000e+00   0.000000e+00   0.000000e+00   0.000000e+00    0
11     0.000000e+00 -6.080875e-113  -2.141277e-36  -1.347476e-05    0
12    -3.802475e-19 -2.419462e-191   0.000000e+00   0.000000e+00    0
13   -5.318413e-295   0.000000e+00   0.000000e+00   0.000000e+00    0
14     0.000000e+00 -2.324676e-100  -5.957980e-30  -7.850063e-08    0
15    -1.076909e-58 -6.669696e-284   0.000000e+00   0.000000e+00    0
16   -1.857644e-231   0.000000e+00   0.000000e+00   0.000000e+00    0
17    -3.153752e-58  -1.473126e-08  -4.958692e-65 -3.077777e-225    0
18     0.000000e+00   0.000000e+00   0.000000e+00   0.000000e+00    0
19   -1.161063e-159  -1.980755e-17  -1.809605e-22  -1.125459e-80    0
20   -2.356445e-154 -9.713541e-301   0.000000e+00   0.000000e+00    0
3000   0.000000e+00   0.000000e+00   0.000000e+00   0.000000e+00    0
3001  7.945641e-224   3.387569e-37   2.663515e-15   3.061563e-44    0
3002  3.305267e-299   1.378134e-67   2.320201e-19   1.242501e-19    0
3003   0.000000e+00  8.914794e-166   1.902969e-65   1.643562e-02    0
3004   1.000000e+00   1.023004e-91  3.147192e-234   0.000000e+00    0
3005   1.023004e-91   1.000000e+00   2.479034e-35  2.698841e-153    0
3006  3.147192e-234   2.479034e-35   1.000000e+00   2.494577e-60    0
3007   0.000000e+00  2.698841e-153   2.494577e-60   1.000000e+00    0
3008   0.000000e+00   0.000000e+00   0.000000e+00   0.000000e+00    1
3009   0.000000e+00   0.000000e+00   0.000000e+00   0.000000e+00    0
3010   0.000000e+00   0.000000e+00   0.000000e+00   0.000000e+00    0
3009          3010
10     0.000000e+00 -1.836994e-45
11     0.000000e+00  0.000000e+00
12     0.000000e+00  0.000000e+00
13     0.000000e+00  0.000000e+00
14     0.000000e+00  0.000000e+00
15     0.000000e+00  0.000000e+00
16     0.000000e+00  0.000000e+00
17     0.000000e+00  0.000000e+00
18   -4.882814e-270  0.000000e+00
19     0.000000e+00  0.000000e+00
20     0.000000e+00  0.000000e+00
3000   5.593377e-93  0.000000e+00
3001   0.000000e+00  0.000000e+00
3002   0.000000e+00  0.000000e+00
3003   0.000000e+00  0.000000e+00
3004   0.000000e+00  0.000000e+00
3005   0.000000e+00  0.000000e+00
3006   0.000000e+00  0.000000e+00
3007   0.000000e+00  0.000000e+00
3008   0.000000e+00  0.000000e+00
3009   1.000000e+00  0.000000e+00
3010   0.000000e+00  1.000000e+00
[,1]
[1,] -1.0000000
[2,] -1.3076210
[3,] -1.0000000
[4,] -1.0000673
[5,] -1.3075217
[6,] -1.0000000
[7,] -1.0000673
[8,] -1.0000000
[9,] -1.0000000
[10,] -0.9999991
[11,] -1.0000000
[12,]  1.0000000
[13,]  1.0000005
[14,]  0.9998858
[15,]  1.0164331
[16,]  1.0000000
[17,]  1.0000000
[18,]  1.0000000
[19,]  1.0164221
[20,]  1.0000000
[21,]  1.0000000
[22,]  1.0000000
```

kernlab documentation built on Nov. 12, 2019, 9:07 a.m.