<jupyter_start><jupyter_text>Mean shift clustering Look for groupings of Titanic passengers with similar characteristics<jupyter_code>import math
import pandas as pd
import numpy as np

import sklearn
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error

%pylab inline
pylab.rcParams['figure.figsize'] = (15, 6)

# Do not use normal form (scietific notation) when printing numbers, exponents can make it harder to compare values
pd.set_option('float_format', '{:f}'.format)
titanic_data = pd.read_csv("../datasets/kaggle/titanic/train.csv", quotechar='"')<jupyter_output><empty_output><jupyter_text>Explore<jupyter_code>titanic_data.head()
titanic_data.info()
titanic_data.describe()<jupyter_output><empty_output><jupyter_text>Prepare Remove features that are too specific to individual passengers to be useful when looking for patterns<jupyter_code>titanic_data.drop(["PassengerId", "Name", "Ticket", "Cabin"], "columns", inplace=True)
titanic_data.head()<jupyter_output><empty_output><jupyter_text>Convert Sex to numeric<jupyter_code>le = preprocessing.LabelEncoder()
titanic_data["Sex"] = le.fit_transform(titanic_data["Sex"].astype(str))
titanic_data.head()<jupyter_output><empty_output><jupyter_text>One hot encode the _Embarked_ feature<jupyter_code>titanic_data = pd.get_dummies(titanic_data, columns=["Embarked"])
titanic_data.head()<jupyter_output><empty_output><jupyter_text>Look for any null values<jupyter_code>titanic_data[titanic_data.isnull().any(axis=1)]<jupyter_output><empty_output><jupyter_text>Drop any rows with null values<jupyter_code>titanic_data = titanic_data.dropna()<jupyter_output><empty_output><jupyter_text>Train<jupyter_code>from sklearn.cluster import MeanShift

analyser = MeanShift(bandwidth=30)
analyser.fit(titanic_data)<jupyter_output><empty_output><jupyter_text>Estimate a good value for the bandwidth based on the data, this is called under the hood if no bandwidth is specified<jupyter_code>from sklearn.cluster import estimate_bandwidth

estimate_bandwidth(titanic_data)
labels = analyser.labels_<jupyter_output><empty_output><jupyter_text>See how many clusters the data was distributed into.  A bandwidth of **50** produces **3** clusters - every point is assigned to one of these clusters.  A bandwidth of **30** produces **5** clusters - every point is assigned to one of these clusters.  Each of these groups will contain passengers with similar characteristics.<jupyter_code>np.unique(labels)<jupyter_output><empty_output><jupyter_text>Add a cluster group column<jupyter_code>titanic_data["cluster_group"] = np.nan

data_length = len(titanic_data)

for i in range(data_length):
    titanic_data.iloc[i, titanic_data.columns.get_loc("cluster_group")] = labels[i]
len(titanic_data)
titanic_data.head()<jupyter_output><empty_output><jupyter_text>Evaluate Group passengers by cluster and see how similar the clusters are<jupyter_code>titanic_cluster_data = titanic_data.groupby(["cluster_group"]).mean()
titanic_cluster_data<jupyter_output><empty_output><jupyter_text>View the number of samples in each cluster<jupyter_code>titanic_cluster_data["Counts"] = titanic_data.groupby(["cluster_group"]).size()
titanic_cluster_data<jupyter_output><empty_output><jupyter_text>Look at more detailed information on a single cluster<jupyter_code>titanic_data[titanic_data["cluster_group"] == 1].describe()<jupyter_output><empty_output><jupyter_text>View all the passengers in this cluster<jupyter_code>titanic_data[titanic_data["cluster_group"] == 1]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10130809908154745, 'fraction_numerical': 0.003896465349290287, 'mean_word_length': 9.89090909090909, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 61, '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': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '578868', 'license': 'MIT', 'n_tokens_mistral': 1242, 'n_tokens_neox': 1211, 'n_words': 249, 'path': 'learning/meanshift-clustering.ipynb', 'repo_name': 'stuarthallows/machine-learning', 'size': '3593'}