spyndex.spyndex.computeKernel¶
- spyndex.spyndex.computeKernel(kernel: str, params: dict | None = None, **kwargs) Any[source]¶
Computes a kernel
k(a,b).Kernel parameters are used for kernel indices like the kNDVI that requires the
kNN(k(N,N)) andkNR(k(N,R)) parameters.- Parameters:
kernel (str) – Kernel to use. One of ‘linear’, ‘poly’ or ‘RBF’.
params (dict) – Parameters to use for the kernel computation. For
kernel = 'linear', the parameters ‘a’ (band A) and ‘b’ (band B) must be declared. Forkernel = 'RBF', the parameters ‘a’ (band A), ‘b’ (band B) and ‘sigma’ (length-scale) must be declared. Forkernel = 'poly', the parameters ‘a’ (band A), ‘b’ (band B), ‘p’ (kernel degree) and ‘c’ (trade-off) must be declared.kwargs –
Parameters used as inputs for the computation as keyword pairs. Ignored when params is defined.
Added in version 0.0.5.
- Returns:
Computed kernel.
- Return type:
Any
See also
computeIndexComputes one or more Spectral Indices from the Awesome Spectral Indices list.
Examples
Compute a kernel index with the help of
spyndex.computeKernel():>>> import spyndex >>> spyndex.computeIndex( ... index = "kNDVI", ... params = { ... "kNN": 1.0, ... "kNR": spyndex.computeKernel( ... kernel = "RBF", ... params = { ... "a" : 0.68, "b": 0.13, "sigma": (0.68 + 0.13) / 2 ... } ... ) ... } ... ) 0.4309459271768674
Parameters can also be passed as keyword pairs:
>>> import spyndex >>> spyndex.computeIndex( ... index = "kNDVI", ... kNN = 1.0, ... kNR = spyndex.computeKernel("RBF",a = 0.68,b = 0.13,sigma = (0.68 + 0.13) / 2) ... ) 0.4309459271768674
Use the polynomial kernel:
>>> import spyndex >>> spyndex.computeIndex( ... index = "kNDVI", ... params = { ... "kNN": spyndex.computeKernel( ... kernel = "poly", ... params = { ... "a" : 0.68, ... "b": 0.68, ... "p": 2.0, ... "c": spyndex.constants.c.default ... } ... ), ... "kNR": spyndex.computeKernel( ... kernel = "poly", ... params = { ... "a" : 0.68, ... "b": 0.13, ... "p": 2.0, ... "c": spyndex.constants.c.default ... } ... ) ... } ... ) 0.2870700138954041
Now let’s try a
numpy.ndarray:>>> import numpy as np >>> R = np.random.normal(0.12,0.05,10000) >>> N = np.random.normal(0.67,0.12,10000) >>> spyndex.computeIndex( ... index = "kNDVI", ... params = { ... "kNN": 1.0, ... "kNR": spyndex.computeKernel( ... kernel = "RBF", ... params = { ... "a" : N, ... "b" : R, ... "sigma" : np.mean([N,R],0) ... } ... ) ... } ... ) array([0.36776416, 0.57727362, 0.5252302 , ..., 0.5209451 , 0.53162097, 0.67689597])
It’s time for a
pandas.DataFrame!>>> import pandas as pd >>> R = np.random.normal(0.12,0.05,10000) >>> N = np.random.normal(0.67,0.12,10000) >>> df = pd.DataFrame({"Red":R,"NIR":N}) >>> spyndex.computeIndex( ... index = "kNDVI", ... params = { ... "kNN": 1.0, ... "kNR": spyndex.computeKernel( ... kernel = "RBF", ... params = { ... "a" : df["NIR"], ... "b" : df["Red"], ... "sigma" : df.mean(1) ... } ... ) ... } ... ) 0 0.468294 1 0.535752 2 0.745249 3 0.402761 4 0.432528 ... 9995 0.475168 9996 0.482034 9997 0.403363 9998 0.489537 9999 0.508163 Length: 10000, dtype: float64