This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Import library. | |
import pymc3 as pm | |
# Import data. | |
filename = "wine.csv" | |
data = pd.read_csv(filename, sep=';', header=0, index_col=False) | |
data["Class"][data["Class"] > 1] = 0 | |
data.head() | |
# Define model. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define Gaussian kernel. | |
def Gaussian_kernel(x, x0, gamma): | |
return (1/np.sqrt(2*np.pi*gamma)) * np.exp(-(x - x0)**2/(2*gamma)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define kernel density. | |
def p(x, x0, gamma, kernel=Gaussian_kernel): | |
p_x = 0 | |
for num in x0: | |
p_x += kernel(x, num, gamma) | |
return 1/(len(x0)) * p_x |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.model_selection import KFold | |
folds = 10 | |
kf = KFold(n_splits=folds) | |
gammas = np.logspace(-2, 1, 51) | |
log_proba = np.zeros((gammas.shape[0])) | |
# Perform cross-validation. | |
for i, gamma in enumerate(gammas): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define y = f(x). | |
def f_y(x, x0, y0, gamma, kernel=Gaussian_kernel): | |
assert len(x0) == len(y0) | |
f_y = 0 | |
f_x = 0 | |
for k in range(len(x0)): | |
f_y += kernel(x, x0[k], gamma)*y0[k] | |
f_x += kernel(x, x0[k], gamma) | |
return f_y / f_x |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
folds = 10 | |
kf = KFold(n_splits=folds) | |
gammas = np.logspace(-4, -1, 51) | |
mse = np.zeros((gammas.shape[0])) | |
# Perform cross-validation. | |
for i, gamma in enumerate(gammas): | |
mse_ = 0 | |
for train_index, test_index in kf.split(x0): |