<commit_before>"""Hierarchical clustering for DoppioGioco."""
# pylint: disable=C0103
import numpy as np
from time import time
from sklearn.cluster import AgglomerativeClustering
from sklearn.metrics import silhouette_score


# extract plain tension curves from the CSV file
csv_file = 'tension_curves.csv'
tension_curves = np.array([np.asarray(line.split(','), dtype=np.uint32)
                           for line in open(csv_file)])
# get information about the tension curves dataset
n = tension_curves.shape[0]
max_len = max([len(ten) for ten in tension_curves])
# and compute the weighted moving average from the plain tension curves
data = np.zeros((n, max_len))
for i in range(n):
    for j in range(tension_curves[i].shape[0]):
        w = [2**k for k in range(j+1)]  # exponential weighting
        # w = [k+1 for k in range(j+1)]  # standard weighting
        data[i, j] = np.average(tension_curves[i][:j+1], weights=w)

k_range = range(2, 40)
for k in k_range:
    print("## {:} ##".format(k))
    start = time()
    labels = AgglomerativeClustering(k).fit_predict(data)
    print("Elapsed time: {:.4f}".format(time() - start))
    sil_score = silhouette_score(data, labels)
    print("Silhouette score: {:.6f}".format(sil_score))<commit_msg>Sophia tests<commit_after>"""Hierarchical clustering for DoppioGioco."""
# pylint: disable=C0103
import numpy as np
from time import time
from sklearn.cluster import AgglomerativeClustering
from sklearn.metrics import silhouette_score

K_MIN = 2
K_MAX = 50
METHOD = "agglomerative_clustering"

# extract plain tension curves from the CSV file
csv_file = 'tension_curves.csv'
tension_curves = np.array([np.asarray(line.split(','), dtype=np.uint32)
                           for line in open(csv_file)])
# get information about the tension curves dataset
n = tension_curves.shape[0]
max_len = max([len(ten) for ten in tension_curves])
# and compute the weighted moving average from the plain tension curves
data = np.zeros((n, max_len))
for i in range(n):
    for j in range(tension_curves[i].shape[0]):
        w = [2**k for k in range(j+1)]  # exponential weighting
        # w = [k+1 for k in range(j+1)]  # standard weighting
        data[i, j] = np.average(tension_curves[i][:j+1], weights=w)

k_range = np.arange(K_MIN, K_MAX)
sil_scores = np.zeros(K_MAX - K_MIN)
all_labels = np.empty((K_MAX - K_MIN, data.shape[0]))
for k in k_range:
    print("## {:} ##".format(k))
    start = time()
    if METHOD == "agglomerative_clustering":
        labels = AgglomerativeClustering(k).fit_predict(data)
    print("Elapsed time: {:.4f}".format(time() - start))
    sil_score = silhouette_score(data, labels)
    print("Silhouette score: {:.6f}".format(sil_score))
    sil_scores[k - K_MIN] = sil_score
    all_labels[k - K_MIN] = labels

np.savetxt('results/{:}_k_range.gz'.format(METHOD), k_range)
np.savetxt('results/{:}_sil_scores.gz'.format(METHOD), sil_scores)
np.savetxt('results/{:}_all_labels.gz'.format(METHOD), all_labels)
{'fraction_non_alphanumeric': 0.13523489932885907, 'fraction_numerical': 0.012416107382550336, 'mean_word_length': 4.2948490230905865, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, '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}}
{'dir': 'git-commits-cleaned', 'id': '6919640', 'n_tokens_mistral': 1084, 'n_tokens_neox': 1024, 'n_words': 266}