Learn R Programming

FastKRR (version 0.1.2)

make_kernel: Kernel matrix \(K\) construction for given datasets

Description

Constructs a kernel matrix \(K \in \mathbb{R}^{n \times n'}\) given two datasets \(X \in \mathbb{R}^{n \times d}\) and \(X' \in \mathbb{R}^{n' \times d}\), where \(x_i \in \mathbb{R}^d\) and \(x'_j \in \mathbb{R}^d\) denote the i-th and j-th rows of \(X\) and \(X'\), respectively, and \(K_{ij}=\mathcal{K}(x_i, x'_j)\) for a user-specified kernel. Implemented in C++ via RcppArmadillo.

Value

An S3 object of class "kernel_matrix" that represents the computed kernel matrix. If X_new is NULL, the result is a symmetric matrix \(K_{ij} = \mathcal{K}(x_i, x_j)\), with \(K \in \mathbb{R}^{n \times n}\). Otherwise, the result is a rectangular matrix \(K'_{ij} = \mathcal{K}(x_i, x'_j)\), with \(K' \in \mathbb{R}^{n \times n'}\).

Arguments

X

Design matrix \(X \in \mathbb{R}^{n \times d}\) (rows \(x_i \in \mathbb{R}^d\)).

X_new

Second matrix \(X' \in \mathbb{R}^{n' \times d}\) (rows \(x'_j \in \mathbb{R}^d\)). If omitted, \(X' = X\) and \(n' = n\).

kernel

Kernel type; one of "gaussian" or "laplace".

rho

Kernel width parameter (\(\rho > 0\)).

n_threads

Number of parallel threads. The default is 4. If the system does not support 4 threads, it automatically falls back to 1 thread. Parallelization (implemented in C++) is one of the main advantages of this package and is applied only for "laplace" kernels.

Details

Gaussian: $$\mathcal{K}(x_i,x_j)=\exp\!\big(-\rho\|x_i-x_j\|_2^2\big)$$ Laplace: $$\mathcal{K}(x_i,x_j)=\exp\!\big(-\rho\|x_i-x_j\|_1\big)$$

Examples

Run this code
# Data setting
set.seed(1)
d = 1
rho = 1
n = 1000
X = matrix(runif(n*d, 0, 1), nrow = n, ncol = d)

# New design matrix
new_n = 1500
new_X = matrix(runif(new_n*d, 0, 1), nrow = new_n, ncol = d)

# Make kernel : Gaussian kernel
K = make_kernel(X, kernel = "gaussian", rho = rho) ## symmetric matrix
new_K = make_kernel(X, new_X, kernel = "gaussian", rho = rho) ## rectangular matrix

# Make kernel : Laplace kernel
K = make_kernel(X, kernel = "laplace", rho = rho, n_threads = 1) ## symmetric matrix
new_K = make_kernel(X, new_X, kernel = "laplace", rho = rho, n_threads = 1) ## rectangular matrix

Run the code above in your browser using DataLab