<jupyter_start><jupyter_text>Exercise 7-2 - toc: true  Recommended Music> twitter: https://twitter.com/nopartyforcd/status/1194952354954502145<jupyter_code>#collapse

import pandas as pd,\
  numpy as np

from sklearn.cluster import KMeans
from sklearn import metrics
import matplotlib.pyplot as plt
#collapse

df = pd.read_csv('../data/forbes_2000.csv', index_col=0)

df.dropna(inplace=True)

X = df[['sales','assets','profits']]
#collapse

range_n_clusters = [2, 3]

for n_clusters in range_n_clusters:

    kmeans = KMeans(n_clusters=n_clusters, random_state=0)
    kmeans = kmeans.fit(X)

    result = pd.concat([df, pd.DataFrame({'cluster':kmeans.labels_})], axis=1)
    result = result.sort_values(by=['cluster'], ascending=[True])

    with pd.option_context('display.max_rows', None, 'display.max_columns', None):    
        
        print('{}\t{}\t{}\t{}'.format('Cluster', 'sales', 'assets', 'profits'))
        print('{}'.format('-' * 80))
        
        for cluster in result.cluster.unique():
            print('{:.3f}\t{:.3f} ({:.3f})\t{:.3f} ({:.3f})\t{:.3f} ({:.3f})\t)'.format(cluster, result[result.cluster==cluster].sales.mean()
                                                                                                            , result[result.cluster==cluster].sales.std()
                                                                                                            , result[result.cluster==cluster].assets.mean()
                                                                                                            , result[result.cluster==cluster].assets.std()
                                                                                                            , result[result.cluster==cluster].profits.mean()
                                                                                                            , result[result.cluster==cluster].profits.std()))

        print('\n{}\n'.format('=' * 80))
        
        x_axis = result.sales
        y_axis = result.assets
        clusters = result.cluster

        x_axis_min, x_axis_max = x_axis.min() - .5, x_axis.max() + .5
        y_axis_min, y_axis_max = y_axis.min() - .5, y_axis.max() + .5

        # Scatterplot
        plt.figure() 
        plt.scatter(x_axis, y_axis, c=clusters)
        plt.xlabel('sales')
        plt.ylabel('assets')
        plt.xlim(x_axis_min, x_axis_max)
        plt.ylim(y_axis_min, y_axis_max)
        plt.xticks(())
        plt.yticks(())

plt.show()
result.dropna().pivot_table(
  index=['cluster'],
  values=['sales', 'assets', 'profits'],
  aggfunc=[np.average,np.std]
)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1462955998495675, 'fraction_numerical': 0.01692365550959007, 'mean_word_length': 1.6362735381565907, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 25, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '59183', 'license': 'Apache-2.0', 'n_tokens_mistral': 824, 'n_tokens_neox': 767, 'n_words': 120, 'path': '_notebooks/2021-07-26-exercise-72.ipynb', 'repo_name': 'kasano/nus_msba_bootcamp', 'size': '2659'}