<jupyter_start><jupyter_text>Step 4.3: Cluster individual traces __Step goals:__ Cluster generated individual traces with Gaussian Mixture Model.__Step overview:__1. Load the data and prepare the features;2. Perform GMM;3. Save the result.<jupyter_code>import matplotlib.pyplot as plt
import pandas as pd
import geopandas as gpd
import numpy as np
import pickle
import math
from tqdm import tqdm
import itertools
from sklearn.mixture import GaussianMixture
plt.style.use('science')<jupyter_output><empty_output><jupyter_text>1. Load the data<jupyter_code># Load the data
data_type = 'entry'
entry_individual = pd.read_csv(f"../data/interim/counts/{data_type}_individual.csv")
print(f'Clustering will be performed on : {entry_individual.shape[0]} points.')
X = entry_individual["time_stamp"].values
X = X.reshape(-1, 1)<jupyter_output>Clustering will be performed on : 4571040 points.<jupyter_text>2. Clustering<jupyter_code>%%time
# Specify hyper-parameters
n_components = 10
random_state = 12345
n_init = 50

# Train the model
model = GaussianMixture(n_components, covariance_type='full', random_state=random_state, n_init=n_init, verbose=2).fit(X)

# Get the parameters
labels = model.predict(X)
weights = model.weights_
probs = np.around(model.predict_proba(X), 3)
means = model.means_
covariances = model.covariances_<jupyter_output>Initialization 0<jupyter_text>3. Save the result<jupyter_code># Save the model
filename = f'../models/individual_traces/gmm_{data_type}_{n_components}.sav'
pickle.dump(model, open(filename, 'wb')) 

# Save parameters
np.save(f"../data/interim/gmm/individual_traces/labels_{n_components}_{data_type}.npy", labels)
np.save(f"../data/interim/gmm/individual_traces/weights_{n_components}_{data_type}.npy", weights)
np.save(f"../data/interim/gmm/individual_traces/probs_{n_components}_{data_type}.npy", probs)
np.save(f"../data/interim/gmm/individual_traces/means_{n_components}_{data_type}.npy", means)
np.save(f"../data/interim/gmm/individual_traces/covariances_{n_components}_{data_type}.npy", covariances)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12216320618058908, 'fraction_numerical': 0.014485755673587638, 'mean_word_length': 7.189723320158103, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '585304', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 771, 'n_tokens_neox': 716, 'n_words': 142, 'path': 'notebooks/4.3-cluster-ind-traces.ipynb', 'repo_name': 'mikhailsirenko/spacetimegeo', 'size': '2071'}