<jupyter_start><jupyter_text>05: Training the ML algorithm<jupyter_code>import SMEFT19
d = SMEFT19.ellipse.load('../data/ellipses/rotBII.yaml')
ML = SMEFT19.ml.train(['../data/samples/randompoints.dat', '../data/samples/samplealbl.dat', '../data/samples/sampleaqbq.dat'],
                 '../data/samples/validationpoints.dat', '../data/MLmodels/xgb_lh.json', d['bf'], headers=0)<jupyter_output>0.6555325116199011<jupyter_text>Linear regression Here we check that the ML approximation correctly reproduces the likelihood of the datapoints<jupyter_code>SMEFT19.ml.regr(ML, '../data/samples/validationpoints.dat', '../data/plots/regression_xgb')<jupyter_output><empty_output><jupyter_text>Distribution Now we generate a small sample of points with a Montecarlo algorithm using the ML-approximation of the likelihood<jupyter_code>newpoints = ML.newpoints(500)
df = pd.DataFrame(newpoints)
preds = ML.model.predict(df)
import matplotlib.pyplot as plt
from scipy.stats import chi2
import numpy as np<jupyter_output><empty_output><jupyter_text>And compare the resulting histogram with the p.d.f of a $\chi^2$ distribution with 5 degrees of freedom<jupyter_code>_ = plt.hist(2*max(preds) - 2*preds, bins=50, range=(0,25), density=True, label='Predicted histogram')
plt.plot(np.linspace(0,25,51), chi2(5).pdf(np.linspace(0,25,51)), label=r'$\chi^2$ distribution', lw=2, c='r')
plt.xlim([0,25])
plt.xlabel(r'$\chi^2_\mathrm{bf} - \chi^2$', fontsize=18)
plt.ylabel('Normalized frequency', fontsize=18)
plt.legend(fontsize=16)
plt.xticks(fontsize=16)
plt.yticks(fontsize=16)
plt.tight_layout(pad=0.5)
plt.savefig('../data/plots/hist_xgb.pdf')
plt.savefig('../data/plots/hist_xgb.pgf')<jupyter_output><empty_output><jupyter_text>SHAP<jupyter_code>newpoints += ML.newpoints(500)
ML = SMEFT19.ml.load_model('../data/MLmodels/xgb_lh.json', '../data/samples/validationpoints.dat', d['bf'])
dfnewpoints = pd.DataFrame(newpoints, columns=['C', 'al', 'bl', 'aq', 'bq'])
dfnewpoints['logL'] = ML.model.predict(dfnewpoints)
dfnewpoints.to_csv('../data/samples/mlpoints.dat', sep='\t', index=False)
SMEFT19.ml.SHAP_bf('../data/MLmodels/xgb_lh.json', d['bf'])
SMEFT19.ml.SHAP_summary('../data/MLmodels/xgb_lh.json', '../data/samples/mlpoints.dat', '../data/plots/SHAP_summary', 0)
import matplotlib.pyplot as plt
import numpy as np
names = ['C', 'al', 'bl', 'aq', 'bq']
ranges = [[-0.08, 0.08], [-0.18, 0.07], [-0.025, 0.025], [-0.07, 0.12], [-0.6,2.8]]
for i in range(5):
    SMEFT19.ml.SHAP_param('../data/MLmodels/xgb_lh.json', '../data/samples/mlpoints.dat', i, 0)
    ax = plt.gca()
    plt.xlim([d['bf'][i]+ranges[i][0], d['bf'][i]+ranges[i][1]])
    ax.set_xticks(np.linspace(d['bf'][i]+ranges[i][0], d['bf'][i]+ranges[i][1], 5))
    plt.xticks(fontsize=16)
    plt.yticks(fontsize=16)
    plt.tight_layout(pad=0.5)
    plt.savefig(f'../data/plots/SHAP_{names[i]}.pdf')<jupyter_output>Setting feature_perturbation = "tree_path_dependent" because no background data was given.
Setting feature_perturbation = "tree_path_dependent" because no background data was given.
Setting feature_perturbation = "tree_path_dependent" because no background data was given.
Setting feature_perturbation = "tree_path_dependent" because no background data was given.
Setting feature_perturbation = "tree_path_dependent" because no background data was given.
{'fraction_non_alphanumeric': 0.1796028880866426, 'fraction_numerical': 0.038206979542719614, 'mean_word_length': 5.7995910020449895, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '119734', 'license': 'MIT', 'n_tokens_mistral': 1334, 'n_tokens_neox': 1259, 'n_words': 220, 'path': 'PaperML/05_Training.ipynb', 'repo_name': 'Jorge-Alda/SMEFT19-notebooks', 'size': '3324'}