<jupyter_start><jupyter_text>Internet Resources:[Python Programming.net - machine learning episodes 34-38](https://pythonprogramming.net/machine-learning-clustering-introduction-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 KMeans
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
'''

df.drop(['body','name'], 1, inplace=True)
df.apply(pd.to_numeric, errors="ignore")
df.fillna(0, inplace=True)
print(df.head())
# 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)
print(df.head())
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 = KMeans(n_clusters=2)
clf.fit(X)

correct = 0
for i in range(len(X)):
    predict_me = np.array(X[i].astype(float))
    predict_me = predict_me.reshape(-1, len(predict_me)) # makes double list [x1,x2...] -> [[x1,x2...]]
    prediction = clf.predict(predict_me)
    if prediction[0] == y[i]:
        correct += 1
        
# this may not be the actual accuracy because the model knows nothing
# about labels because it just finds clusters in the training data
# so accuracy is either correct/len(X) or 1 - correct/len(X)
print(correct/len(X))<jupyter_output>0.7119938884644768
{'fraction_non_alphanumeric': 0.11286089238845144, 'fraction_numerical': 0.018747656542932135, 'mean_word_length': 3.7221238938053096, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, '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': '464116', 'license': 'MIT', 'n_tokens_mistral': 908, 'n_tokens_neox': 853, 'n_words': 234, 'path': 'ML 6.2 - K Means - example - titanic data set.ipynb', 'repo_name': 'cow51/Machine-Learning-Models', 'size': '2667'}