<jupyter_start><jupyter_text>Importing Required Libraries<jupyter_code>#python libraries
import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt
import seaborn as sns
import os
import glob
from google.colab import drive  
import gzip

#Sklearn libraries
from sklearn.cluster import KMeans
from sklearn.metrics import  normalized_mutual_info_score, adjusted_rand_score, silhouette_score
from scipy.optimize import linear_sum_assignment as linear_assignment
from sklearn.preprocessing import MinMaxScaler
from sklearn.manifold import TSNE
from sklearn.mixture import GaussianMixture
from sklearn.metrics import accuracy_score
from sklearn.cluster import DBSCAN
from sklearn.cluster import AgglomerativeClustering

#neural networks libraries
from tensorflow import keras
import tensorflow as tf 
import tensorflow.keras.backend as K
from tensorflow.keras.optimizers import SGD,Adam
from tensorflow.keras.layers import Dense, Input, Layer, InputSpec
from tensorflow.keras.models import Model
from tensorflow.keras.utils import plot_model
from tensorflow.keras.initializers import VarianceScaling
drive.mount('/content/drive')
%cd drive/MyDrive/HPC Dataset
!pwd<jupyter_output>/content/drive/MyDrive/HPC Dataset<jupyter_text>1. Loading Dataset<jupyter_code>df = pd.read_parquet('/content/drive/MyDrive/HPC Dataset/r206n05.gzip')
df = df.drop(['label','timestamp'],axis=1)
df.head()<jupyter_output><empty_output><jupyter_text>2. Data Preprocessing<jupyter_code>df.info()
df.shape
df.describe()
df.isnull().sum()<jupyter_output><empty_output><jupyter_text>2.2 Splitting the Data into X and Y<jupyter_code>X = df.drop('New_label',axis=1)
y = df.New_label
y = y.apply(lambda x: 1 if x == 2 else 0)
y = y.values<jupyter_output><empty_output><jupyter_text>2.3 Scaling Features<jupyter_code>columns = list(X.columns.values)
scaler = MinMaxScaler() 
X[columns] = scaler.fit_transform(X[columns])
X.head()<jupyter_output><empty_output><jupyter_text>3. Models  3.1 Baseline model  K-means<jupyter_code>n_clusters = 2
kmeans = KMeans(n_clusters=n_clusters)
y_pred_kmeans = kmeans.fit_predict(X)<jupyter_output><empty_output><jupyter_text>Evaluation  Clustering Accuracy<jupyter_code>def cluster_accuracy(y_pred,y_true):
  D = max(y_pred.max(), y_true.max()) + 1
  w = np.zeros((D, D), dtype=np.int64)
  # Confusion matrix
  for i in range(y_pred.size):
    w[y_pred[i], y_true[i]] += 1
  ind = linear_assignment(-w)
  acc = sum([w[i, j] for i, j in ind]) * 1.0 / y_pred.size
  return acc
cluster_accuracy(y_pred_kmeans,y)<jupyter_output><empty_output><jupyter_text>Silhoutte Score<jupyter_code>score = silhouette_score(X, y_pred_kmeans, metric='euclidean')
print ("For n_clusters = {}, K_means silhouette score is {})".format(n_clusters, score))<jupyter_output>For n_clusters = 2, K_means silhouette score is 0.4166513420465401)<jupyter_text>Baseline model-2 : Gaussian Mixture Model<jupyter_code>GMM = GaussianMixture(n_components=2, random_state=42)
y_pred_b2 = GMM.fit_predict(X)<jupyter_output><empty_output><jupyter_text>Evaluation<jupyter_code>cluster_accuracy(y_pred_b2,y)
score = silhouette_score(X, y_pred_b2, metric='euclidean')
print ("For n_clusters = {}, GMM silhouette score is {})".format(n_clusters, score))<jupyter_output>For n_clusters = 2, GMM silhouette score is 0.18864048238995582)<jupyter_text>Baseline model-3: Agglomerative Clustering<jupyter_code>aggcl = AgglomerativeClustering(n_clusters=2, affinity='euclidean')
y_pred_b3 = aggcl.fit_predict(X)<jupyter_output><empty_output><jupyter_text>Evaluation<jupyter_code>cluster_accuracy(y_pred_b2,y)
score = silhouette_score(X, y_pred_b3, metric='euclidean')
print ("For n_clusters = {}, Agglomerative Clustering silhouette score is {})".format(n_clusters, score))<jupyter_output>For n_clusters = 2, GMM silhouette score is 0.41874138702518476)<jupyter_text>3.2 Autoencoder Model<jupyter_code>def auto_encoder(dims, init):
    stack_layers = len(dims) - 1
    
    input_data = Input(shape=(dims[0],), name='input')
    x = input_data
    
    # internal layers of encoder
    for i in range(stack_layers-1):
        x = Dense(dims[i + 1], activation='relu',  kernel_initializer=init, name='encoder_%d' % i)(x)

    # latent hidden layer
    encoded = Dense(dims[-1], kernel_initializer=init, name='encoder_%d' % (stack_layers - 1))(x)
    x = encoded

    # internal layers of decoder
    for i in range(stack_layers-1, 0, -1):
        x = Dense(dims[i], activation='relu', kernel_initializer=init, name='decoder_%d' % i)(x)

    # decoder output
    x = Dense(dims[0], kernel_initializer=init, name='decoder_0')(x)
    
    decoded = x
    
    autoencoder_model = Model(inputs=input_data, outputs=decoded, name='autoencoder')
    encoder_model = Model(inputs=input_data, outputs=encoded, name='encoder')
    
    return autoencoder_model, encoder_model
#No of Clusters
n_clusters = 2
#dimensions
dims = [X.shape[-1], 500, 500, 2000, 10]
#Pre_trained Optimizer
optimizer = SGD(learning_rate=0.1, momentum=0.9)
#initialization
init =  tf.keras.initializers.RandomNormal(mean=0,stddev=0.01)
autoencoder, encoder = auto_encoder(dims, init=init)
plot_model(autoencoder,show_shapes=True)
path = '/content/drive/My Drive/HPC Dataset/results'
autoencoder.compile(optimizer=optimizer, loss='mse')
#call_back = keras.callbacks.EarlyStopping(monitor='loss',patience=5) 
autoencoder.fit(X, X, batch_size=128, epochs=100,verbose=1)
autoencoder.save_weights(path + '/ae_weights_dimred_10d.h5')
embedded_data = encoder.predict(X)
embedded_data.shape<jupyter_output><empty_output><jupyter_text>3.3 K-Means<jupyter_code>n_clusters = 2
kmeans = KMeans(n_clusters=n_clusters)
y_pred1 = kmeans.fit_predict(embedded_data)<jupyter_output><empty_output><jupyter_text>Evaluation<jupyter_code>acc = cluster_accuracy(y_pred=y_pred1,y_true=y)
print(acc)
score = silhouette_score(X, y_pred1, metric='euclidean')
print ("For n_clusters = {}, K_means silhouette score is {})".format(n_clusters, score))<jupyter_output>For n_clusters = 2, K_means silhouette score is 0.42106060617556507)<jupyter_text>3.4 Gaussian Mixture Model<jupyter_code>GMM = GaussianMixture(n_components=2, random_state=42)
y_pred2 = GMM.fit_predict(embedded_data)<jupyter_output><empty_output><jupyter_text>Evaluation<jupyter_code>acc = cluster_accuracy(y_pred=y_pred3,y_true=y)
print(acc)
score = silhouette_score(X, y_pred2, metric='euclidean')
print ("For n_clusters = {}, K_means silhouette score is {})".format(n_clusters, score))<jupyter_output>For n_clusters = 2, K_means silhouette score is 0.4201211867168033)<jupyter_text>3.5 Agglomerative Clustering<jupyter_code>aggcl = AgglomerativeClustering(n_clusters=2, affinity='euclidean')
y_pred3 = aggcl.fit_predict(embedded_data)<jupyter_output><empty_output><jupyter_text>Evaluation<jupyter_code>acc = cluster_accuracy(y_pred=y_pred3,y_true=y)
print(acc)
score = silhouette_score(X, y_pred3, metric='euclidean')
print ("For n_clusters = {}, K_means silhouette score is {})".format(n_clusters, score))<jupyter_output>For n_clusters = 2, K_means silhouette score is 0.42296811226245756)
{'fraction_non_alphanumeric': 0.11619668414340371, 'fraction_numerical': 0.030607907042652684, 'mean_word_length': 7.472989195678271, 'pattern_counts': {'":': 0, '<': 70, '<?xml version=': 0, '>': 70, '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': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '246169', 'license': 'Apache-2.0', 'n_tokens_mistral': 2811, 'n_tokens_neox': 2595, 'n_words': 478, 'path': 'Autoencoder_+_Clustering_Techniques.ipynb', 'repo_name': 'LIA-UniBo/Deep_Embedding_Clustering', 'size': '7057'}