<jupyter_start><jupyter_text>KNN<jupyter_code>from sktime.classifiers.distance_based import KNeighborsTimeSeriesClassifier

clf = KNeighborsTimeSeriesClassifier(n_neighbors=6, metric='dtw')
clf.fit(X_train, y_train) # doctest: +ELLIPSIS
print(clf.score(X_test, y_test))<jupyter_output>0.8<jupyter_text>Bayesian KNN<jupyter_code>np.max([1,2])
from BayKNN import BayesianNeighborsTimeSeriesClassifier

clf = BayesianNeighborsTimeSeriesClassifier()
clf.fit(X_test, y_train)
#print(clf.score(X_test, y_test))
clf.predict(X_test)
y_test<jupyter_output><empty_output><jupyter_text>Finctio<jupyter_code>def get_bayesian_k_posteriori(y_ordered, eta_prior = None, p_gamma = 1/20):
    classes, y_pos = np.unique(y_ordered, return_inverse=True)
    tau = len(y_pos)
    pi = np.zeros(tau)

    growth_prop_1 = np.ones(tau + 1)
    growth_prop_2 = np.zeros(tau + 1)
    nb_classes = len(classes)

    p_gamma = p_gamma

    if eta_prior is None :
        eta = np.array([10]*nb_classes)
    else :
        eta = eta_prior

    s_alpha_prior = np.sum(eta)


    for t in range(tau):
        y_t = y_pos[t] # will also be the indice of the corresponding alpha in eta

        for i in range(t+1):
            k_tempo = i
            pi[i] = (eta[y_t] + np.sum((y_pos[:i] == 0)*1))/(s_alpha_prior +i) # pas sur des bornes

            growth_prop_2[i + 1] = growth_prop_1[i]*pi[i]*(1-p_gamma)

        pi[0] = eta[y_t]/s_alpha_prior
        growth_prop_2[0] = np.sum(growth_prop_1)*pi[0]*p_gamma

        p_evidence = np.sum(growth_prop_2)

        growth_prop_1 = growth_prop_2/p_evidence
        growth_prop_2 = np.zeros(tau + 1)

    return growth_prop_1

jj = get_bayesian_k_posteriori(y_train)
plt.plot(jj)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11754487550665894, 'fraction_numerical': 0.018529241459177764, 'mean_word_length': 4.052631578947368, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '526966', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 750, 'n_tokens_neox': 700, 'n_words': 111, 'path': '.ipynb_checkpoints/Bayesian KNN-checkpoint.ipynb', 'repo_name': 'mehdibnc/Bayesian_Time_Series_Classification', 'size': '1727'}