<jupyter_start><jupyter_text>Internet Resources:[Python Programming.net - machine learning episodes 39-42](https://pythonprogramming.net/hierarchical-clustering-mean-shift-machine-learning-tutorial/)<jupyter_code>import matplotlib.pyplot as plt
from matplotlib import style
style.use('ggplot')
import numpy as np
from sklearn.cluster import MeanShift
from sklearn import preprocessing
import pandas as pd

# data from https://pythonprogramming.net/static/downloads/machine-learning-data/titanic.xls
df = pd.read_excel('data/titanic.xls') 
'''
Pclass Passenger Class (1 = 1st; 2 = 2nd; 3 = 3rd)
survival Survival (0 = No; 1 = Yes)
name Name
sex Sex
age Age
sibsp Number of Siblings/Spouses Aboard
parch Number of Parents/Children Aboard
ticket Ticket Number
fare Passenger Fare (British pound)
cabin Cabin
embarked Port of Embarkation (C = Cherbourg; Q = Queenstown; S = Southampton)
boat Lifeboat
body Body Identification Number
home.dest Home/Destination
'''

original_df = pd.DataFrame.copy(df)

df.drop(['body','name'], 1, inplace=True)
df.apply(pd.to_numeric, errors="ignore")
df.fillna(0, inplace=True)

# handling non linear data:
# every unique feature value gets a different integer number
# e.g. female becomes 0, male becomes 1
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()
# train
X = np.array(df.drop(['survived'], 1).astype(float))
X = preprocessing.scale(X) # https://scikit-learn.org/stable/modules/preprocessing.html
y = np.array(df['survived'])

clf = MeanShift()
clf.fit(X)

# lets take a look at the different clusters mean shift has found
labels = clf.labels_
cluster_centers = clf.cluster_centers_
original_df['cluster_group']=np.nan

for i in range(len(X)):
    original_df.at[i, 'cluster_group'] = labels[i] 

n_clusters_ = len(np.unique(labels))

survival_rates = {}
for i in range(n_clusters_):
    temp_df = original_df[ (original_df['cluster_group']==float(i)) ] # temp only has data with cluster_group i
    #print(temp_df.head())
    
    survival_cluster = temp_df[  (temp_df['survived'] == 1) ] # temp df just with only suvivors
    
    survival_rate = len(survival_cluster) / len(temp_df)
    print("Cluster ", i,": ",survival_rate)
    survival_rates[i] = survival_rate
# detailed look at clusters
for i in range(n_clusters_):
    print("Cluster "+str(i)+ ":\n")
    print(original_df[(original_df['cluster_group'] == float(i))].describe())
    print("\n\n\n")<jupyter_output>Cluster 0:

            pclass     survived          age        sibsp        parch  \
count  1282.000000  1282.000000  1021.000000  1282.000000  1282.000000   
mean      2.306552     0.378315    29.689194     0.485959     0.322933   
std       0.830961     0.485156    14.383434     1.038767     0.670736   
min       1.000000     0.000000     0.166700     0.000000     0.000000   
25%       2.000000     0.000000    21.000000     0.000000     0.000000   
50%       3.000000     0.000000    28.000000     0.000000     0.000000   
75%       3.000000     1.000000    38.000000     1.000000     0.000000   
max       3.000000     1.000000    80.000000     8.000000     4.000000   

              fare        body  cluster_group  
count  1281.000000  119.000000         1282.0  
mean     29.398523  159.571429            0.0  
std      38.294234   97.302914            0.0  
min       0.000000    1.000000            0.0  
25%       7.895800   71.000000            0.0  
50%      13.900000  155.000000   [...]
{'fraction_non_alphanumeric': 0.10519832471051983, 'fraction_numerical': 0.11012564671101256, 'mean_word_length': 2.6842105263157894, 'pattern_counts': {'":': 2, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '302151', 'license': 'MIT', 'n_tokens_mistral': 1657, 'n_tokens_neox': 1342, 'n_words': 335, 'path': 'ML 7.2 - Mean Shift - example - titanic dataset.ipynb', 'repo_name': 'cow51/Machine-Learning-Models', 'size': '4059'}