<jupyter_start><jupyter_text>Cluster Analysis
---

Naval Engine Data (Kaggle)<jupyter_code># Dependencies
import numpy as np, pandas as pd
import matplotlib.pyplot as plt, seaborn as sns

from sklearn.model_selection import train_test_split
from sklearn.cluster import KMeans, DBSCAN, OPTICS, AgglomerativeClustering, AffinityPropagation

sns.set()
# Import and split data sets
heads = [i.split('-')[1] for i in pd.read_csv(r'input\features.txt', header = None)[0]]
df_raw = pd.read_csv(r'input\data.txt', delimiter = '\s+', names = heads)<jupyter_output><empty_output><jupyter_text>---
 Explore Data<jupyter_code>df_raw.info()
df_raw.sample(5)
df_raw.describe()
plt.scatter(df_raw.iloc[:, 0], df_raw.iloc[:, 2])
# Correlations
cor = df_raw.corr()
sns.heatmap(cor, square = True);

# final two columns are skewing results of others - plot without below
# Correlations less efficiency coeff and decay coeff
cor = df_raw.iloc[:, :-2].corr()
sns.heatmap(cor, square = True);

# As shown in the heatmap there are a range of strong connections across this data - let's apply clustering to invetigate further<jupyter_output><empty_output><jupyter_text>---
 Cluster Analysis<jupyter_code># Normalise data
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
df_norm = pd.DataFrame(scaler.fit_transform(df_raw))
df_norm
# Clustering Function
def clusterAnalysis(X, clusters_n, method):

    if method.__name__ in ['KMeans']:
        model = method(n_clusters = clusters_n, random_state = 88)
        model.fit_predict(X)
        
        clust_labels = model.labels_
        cent = model.cluster_centers_
        
        return (clust_labels, cent)

    if method.__name__ in ['AgglomerativeClustering']:
        model = method(n_clusters = clusters_n)
        clust_labels = model.fit_predict(X)
        
        return clust_labels

    if method.__name__ in ['AffinityPropagation']:
        model = method(damping = 0.88, random_state = 88)
        model.fit_predict(X)
        
        clust_labels = model.labels_
        cent = model.cluster_centers_
        
        return (clust_labels, cent)

    if method.__name__ in ['DBSCAN', 'OPTICS']:
        model = method()
        clust_labels = model.fit_predict(X)
        
        return clust_labels
# Test function
kMeans = clusterAnalysis(df_norm, 8, KMeans)
df_raw['KMeans'] = pd.Series(kMeans[0], name = 'KMeans')

df_raw.sample(3)
fig = plt.figure()
ax = fig.add_subplot(111)
for i in range(kMeans[0].max()+1):
    scatter = ax.scatter(df_raw.iloc[:, 0], df_raw.iloc[:, -4], c = kMeans[0], s = 50)
    ax.set_title('K-Means Clustering')
    
plt.scatter(x = df_raw['KMeans'], y = df_raw.iloc[:, -4])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1169019182048498, 'fraction_numerical': 0.009771986970684038, 'mean_word_length': 3.472491909385113, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '313766', 'license': 'MIT', 'n_tokens_mistral': 1008, 'n_tokens_neox': 963, 'n_words': 222, 'path': 'Clustering/clusterAnalysis.ipynb', 'repo_name': 'sciDelta/Machine-Learning', 'size': '2763'}