示例#1
0
文件: kde.py 项目: swyoon/dependence
    def __init__(self, data, s):
        """
        Initialize a kernel density estimator with a n x d matrix of data and the appropriate smoothness s.
        We need to know the smoothness to choose the correct kernel and to choose the correct bandwidth.

        We correct for bias on the boundary by mirroring the data across the axis.
        """
        self.s = s
        self.m = None
        if int(self.s) == self.s:
            self.m = self.s
        else:
            self.m = int(self.s)+1
        self.data = data
        self.d = data.shape[1]
        self.n = data.shape[0]
        self.replicated_data = np.matrix(np.zeros((3**self.d*self.n, self.d)))
        for i in range(3**self.d):
            tuple = [int(i/3**j) % 3 for j in range(self.d)]
            for j in range(self.d):
                if tuple[j] == 0:
                    self.replicated_data[(self.n*i):(self.n*(i+1)), j] = -1*self.data[:,j]
                if tuple[j] == 1:
                    self.replicated_data[(self.n*i):(self.n*(i+1)), j] = self.data[:,j]
                if tuple[j] == 2:
                    self.replicated_data[(self.n*i):(self.n*(i+1)), j] = 2 - self.data[:,j]

        self.data = self.replicated_data
        self.h = 0.5*np.power(self.n, -1.0/(2*self.s+self.d))

        to_keep = [i for i in range(self.data.shape[0]) if np.max(np.abs(self.data[i,:] - np.matrix(0.5*np.ones((1, self.d))))) <= 0.5+self.h]
        self.data = self.replicated_data[to_keep,:]

        self.kernel = lambda x, c: kernels.kernel(x, self.m, self.h, centre=c)
示例#2
0
def test_scipy_kernel(input1, input2, signal_amp, lengthscale):
    """
    Test for the scipy kernel.
    """
    x1 = np.array([input1]).reshape(-1, 1)
    x2 = np.array([input2]).reshape(-1, 1)
    x = scipy_kernel(x1, x2, l=lengthscale, sigma_f=signal_amp)
    y = kernel(x1, x2, l=lengthscale, sigma_f=signal_amp)
    y = y[0][0]
    assert np.allclose(x, y)
示例#3
0
def test_anisotropic_kernel(single_lengthscale, lengthscale_list, signal_amp):
    """
    Tests that the implementation of the anisotropic kernel is correct.
    """
    input = np.random.rand(4, 4)
    krasser_cov = kernel(input, input, single_lengthscale, signal_amp)
    anisotropic_cov = anisotropic_kernel(input, input, lengthscale_list,
                                         signal_amp)

    assert np.allclose(krasser_cov, anisotropic_cov)
示例#4
0
def test_compute_kernel_matrix_sq_exp_against_krasser(inputs, signal_amp,
                                                      lengthscale):
    """
    Tests that the squared exponential kernel outputs the same values as Martin Krasser's implementation for a
    one-dimensional input vector.
    """
    krasser_kernel = kernel(inputs, inputs, l=lengthscale, sigma_f=signal_amp)
    kernel_matrix = compute_kernel_matrix_sq_exp(inputs, lengthscale,
                                                 signal_amp)
    assert np.allclose(krasser_kernel, kernel_matrix)
示例#5
0
def test_sq_exp_kernel_against_krasser(input1, input2, signal_amp,
                                       lengthscale):
    """
    Tests that the squared exponential kernel outputs the same values as Martin Krasser's implementation for
    single x-values.
    """
    x1 = np.array([input1]).reshape(-1, 1)
    x2 = np.array([input2]).reshape(-1, 1)
    x = sq_exp(x1, x2, lengthscale=lengthscale, sigma=signal_amp)
    y = kernel(x1, x2, l=lengthscale, sigma_f=signal_amp)
    y = y[0][0]
    assert np.allclose(x, y)
示例#6
0
from kernels import kernel
import cv2
import numpy as np
import math
from matplotlib import pyplot as plt
from scipy.signal import correlate
from skimage.exposure import rescale_intensity

img = cv2.imread("checker.jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('original', img)
K1 = np.array([[-1, -1, -1], [0, 0, 0], [1, 1, 1]])
K2 = np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]])

S1 = kernel(img, K1)
S2 = kernel(img, K2)

S5 = S2 + S1
S5 = rescale_intensity(S5, in_range=(0, 255))
S5 = (S5 * 255).astype("uint8")

cv2.imshow('sharpened', S5)
cv2.waitKey(0)
cv2.destroyAllWindows()
示例#7
0
from kernels import kernel
import cv2
import numpy as np
import math
from matplotlib import pyplot as plt
from scipy.signal import correlate
from skimage.exposure import rescale_intensity

img = cv2.imread("color.jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('original', img)
K1 = (np.array([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]]))

S1 = kernel(img, K1)
S1 = S1 + img
S1 = rescale_intensity(S1, in_range=(0, 255))

S1 = (S1 * 255).astype("uint8")
cv2.imshow('sharpened', S1)
cv2.waitKey(0)
cv2.destroyAllWindows()
import numpy as np
from gp_animation import GaussianProcessAnimation
from visualization import animate_multi_plots
from kernels import kernel, periodic_cov, exponentiated_quadratic, mlp_cov, relu_cov, ratquad_cov, rbf_cov, \
    brownian_cov, sinc_cov


n_dims = 150
n_frames = 100
n_traces = 3

x = np.linspace(0, 10, n_dims).reshape(-1, 1)
kernel_matrix = kernel(x, x, kernel_func=rbf_cov)

gaussian_process_animation = GaussianProcessAnimation(kernel_matrix, n_dims=n_dims, n_frames=n_frames)
frames = gaussian_process_animation.get_traces(n_traces)
frames = np.stack(frames).transpose((2, 0, 1)) #should be in the format of (length, n_traces, n_frame))
animate_multi_plots(frames, interval=10, title="RBF")