text_plain_output_1.png
from scipy.spatial.distance import squareform, pdist

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

wine_data = pd.read_csv('../input/uci-wine-data/wine-clustering.csv')

def normalize_data(data):
    data_normalized = data.copy()
    for col in data.columns:
        data_normalized[col] = (data_normalized[col] - data_normalized[col].min()) / (data_normalized[col].max() - data_normalized[col].min())
    return data_normalized
wine_data_normalized = normalize_data(wine_data)

def get_dissimilarity(data):
    from scipy.spatial.distance import squareform, pdist
    similarity_matrix = pd.DataFrame(squareform(pdist(data, 'euclidean')))
    return similarity_matrix
similarity_matrix = get_dissimilarity(wine_data_normalized)
similarity_matrix

def get_avg_dissimilarity(data):
    avg_dissimilarity = np.zeros((data.shape[0], 1))
    for i in range(data.shape[0]):
        avg_dissimilarity[i] = data[i].mean()
    return avg_dissimilarity
avg_dissimilarity = get_avg_dissimilarity(similarity_matrix)
(avg_dissimilarity[:10], avg_dissimilarity.shape)

def form_m_clusters(data, avg_data):
    cluster_objects = []
    cluster = []
    for i in range(data.shape[0]):
        for j in range(data.shape[1]):
            if data[i][j] < avg_data[i]:
                cluster.append(j)
        cluster_objects.append(cluster)
        cluster = []
    return cluster_objects
cluster_objects = form_m_clusters(similarity_matrix, avg_dissimilarity)
len(cluster_objects)

for i in range(5):
    print('cluster', i, '(', max(cluster_objects[i]), ')', ': ', cluster_objects[i])
    print('')