<jupyter_start><jupyter_text>Kernel Conditional Exponential Family AISTATS 2018 paper by <NAME> and <NAME>. https://arxiv.org/abs/1711.05363Short demo of the code accompanying the paper, found at https://github.com/MichaelArbel/KCEF.  Importing required modules<jupyter_code>import numpy as np
import os
import matplotlib.pyplot as plt
%matplotlib inline

from KCEF.estimators.kcef import KCEF_Gaussian
from KCEF.data_generator import FromFileGenerator
from KCEF.tools import median_heuristic<jupyter_output><empty_output><jupyter_text>Loading data<jupyter_code>dataset_name 	= "../data/red_wine.npy" 
gen = FromFileGenerator(dataset_name)
train_data 		= gen.generate(N = -1,dataset_type = 'train')
test_data 		= gen.generate(N = -1, dataset_type = 'test')
_, d = train_data.shape<jupyter_output><empty_output><jupyter_text>Building the estimator The graph of conditional dependences can be given manually as a list of nodes. Each node contains two lists:  node[0] is the list of indices for the variable $Y$ while node[1] is the list of indices for $X$. The estimator will then estimate the conditional density of $Y$ given $X$ for each node in the graph.Setting graph_type to either 'full', 'markov' or 'marginal' and graph to None, will build the graphs used in the paper.<jupyter_code>graph = [[[5], []],[[6], [5]]]
est 	= KCEF_Gaussian(graph_type = 'custom', d = d, graph = [[[5], []],[[6], [5]]])<jupyter_output><empty_output><jupyter_text>Optimizing the hyperparameters Minimizes the cross-validated score over the hyperparamters.This step can be skiped, as the optimal hyperparameters are saved in the file: 'optimal_params.npy'. Please uncomment the first line to run the optimization.<jupyter_code>#res = est.optimize_cv_score(train_data)
#np.save('../data/optimal_params', res)
res = np.load('../data/optimal_params.npy')
est.update_parameters(res)<jupyter_output><empty_output><jupyter_text>Fitting the estimator<jupyter_code>est.fit(train_data)<jupyter_output><empty_output><jupyter_text>Sampling from the KCEF Sampling is performed using ancestral sampling.<jupyter_code>samples = est.sample(100)<jupyter_output><empty_output><jupyter_text>We plot the samples<jupyter_code>plt.scatter(train_data[:,graph[1][1]], train_data[:,graph[1][0]], color = 'k', label = 'DATA')
plt.scatter(samples[:,0], samples[:,1], color = 'r', label = 'KCEF')
plt.xlabel('$x_6$')
plt.ylabel('$x_7$')
plt.legend()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12330456226880394, 'fraction_numerical': 0.013974517057131115, 'mean_word_length': 6.877022653721683, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '75323', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 853, 'n_tokens_neox': 818, 'n_words': 219, 'path': 'notebooks/demo.ipynb', 'repo_name': 'MichaelArbel/KCEF', 'size': '2448'}