<jupyter_start><jupyter_text>Titanic (Mean Shift)<jupyter_code>import matplotlib.pyplot as plt
from matplotlib import style
import numpy as np
from sklearn.cluster import MeanShift
from sklearn import preprocessing
import pandas as pd
style.use("ggplot")
df = pd.read_excel("titanic.xls")
original_df = pd.DataFrame.copy(df)  # Copy of the df.
df.head()
df.drop(["body", "name"], 1, inplace=True)
df.fillna(0, inplace=True)<jupyter_output><empty_output><jupyter_text>Handling non-numerical data<jupyter_code>def handle_non_numerical_data(df):
    columns = df.columns.values
    for column in columns:
        text_digit_vals = {}
        def convert_to_int(val):
            return text_digit_vals[val]
        
        if df[column].dtype != np.int64 and df[column].dtype != np.float64:
            column_contents = df[column].values.tolist()
            unique_elements = set(column_contents)
            x = 0
            for unique in unique_elements:
                if unique not in text_digit_vals:
                    text_digit_vals[unique] = x
                    x+=1
            df[column] = list(map(convert_to_int, df[column]))
    return df

df = handle_non_numerical_data(df)
df.head()
X = np.array(df.drop(["survived"], 1).astype(float))
X = preprocessing.scale(X)
y = np.array(df["survived"])

clf = MeanShift()
clf.fit(X)

labels = clf.labels_
cluster_centers = clf.cluster_centers_

original_df["cluster_group"] = np.nan
# Iter through the labels and populate the values of the "cluster_group" column.
for i in range(len(X)):
    original_df["cluster_group"].iloc[i] = labels[i]  # iloc[i] is the row in the df (i is the number), column: cluster_group = labels[i]
    
n_clustters_ = len(np.unique(labels))

survival_rates = {}
for i in range(n_clustters_):
    temp_df = original_df[(original_df["cluster_group"] == float(i))]
    survival_cluster = temp_df[(temp_df["survived"] == 1)]
    survival_rate = len(survival_cluster) / len(temp_df)
    survival_rates[i] = survival_rate

for cluster in survival_rates:
    print("Cluster " + str(cluster + 1) + " - survival rate: " + str(survival_rates[cluster]))
print("Description of cluster 1\n")
print(original_df[(original_df["cluster_group"] == 0)].describe())
print("Description of cluster 2\n")
print(original_df[(original_df["cluster_group"] == 1)].describe())
print("Description of cluster 3\n")
print(original_df[(original_df["cluster_group"] == 2)].describe())
cluster_0 = original_df[(original_df["cluster_group"] == 0)]
cluster_0_fc = cluster_0[(cluster_0["pclass"] == 1)]
cluster_0_fc.describe()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13271960107403144, 'fraction_numerical': 0.009205983889528193, 'mean_word_length': 4.311608961303462, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '166227', 'license': 'Apache-2.0', 'n_tokens_mistral': 914, 'n_tokens_neox': 877, 'n_words': 189, 'path': 'Clustering/Hierarchical Clustering/Titanic-Mean-Shift.ipynb', 'repo_name': 'KostadinPlachkov/MachineLearning', 'size': '2607'}