# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: 'Python 3.8.8 64-bit (''virtPenv'': conda)'
#     language: python
#     name: python3
# ---

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from random import sample


def distances(data):
    dist_mat = np.matrix(np.zeros((data.shape[0],data.shape[0])))
    for i in range(data.shape[0]):
         a = data - data.loc[i]
         dist_mat[i,:] = np.linalg.norm(a, axis=1)
    return dist_mat


def greedyk(data, k):
    dist_mat = distances(data)
    centroid = data.sample()
    while (centroid.shape[0] < k):
        next_cen_loc = np.argmax(np.min(dist_mat[:,centroid.index],axis = 1))        
        centroid = centroid.append(data.loc[next_cen_loc])
    C = np.argmin(dist_mat[:,centroid.index],axis = 1)
    price = np.max(np.min(dist_mat[:,centroid.index],axis = 1))
    return centroid, C, price


def swapping(data, centroid, price, tau, dist_mat):
    rem_data = data.drop(labels=centroid.index)
    c = price
    new_cen = centroid.copy()
    for i in range(centroid.shape[0]):
        #print("#"*80)
        j_ = 5000
        new_cen = new_cen.drop(labels=centroid.index[i])
        
        for j in rem_data.index:
            new_cen = new_cen.append(rem_data.loc[j])
            new_price = np.sum(np.min(dist_mat[:,new_cen.index],axis = 1))
            if (new_price < (1-tau)*c):
                c = new_price
                j_ = j
                #print(j_)

            new_cen = new_cen.drop(labels=j)
        #print("*"*80)
        print(j_)

        if (j_ != 5000):
            new_cen = new_cen.append(rem_data.loc[j_])
            rem_data = rem_data.drop(labels=j_)
            #print(new_cen.shape)
        elif (j_ == 5000):
            new_cen = new_cen.append(centroid.loc[centroid.index[i]])
            #print(new_cen.shape)


    return new_cen

data = pd.read_csv(f"clustering.csv", header = None)
Q, C, price = greedyk(data, 2)
dist_mat = distances(data)
cost = np.sum(np.min(dist_mat[:,Q.index],axis = 1))

Q

color = ['y', 'blue', 'green', 'cyan', 'black', 'brown']
for m in range(len(color)):
    id = []
    for i in range(len(C)):
        if (C[i] == m):
            id.append(i)
    plt.scatter(data.loc[id][0],data.loc[id][1], c = color[m])
plt.scatter(Q.loc[:][0],Q.loc[:][1], c = 'r')

cost

new_cen = swapping(data,Q,cost,0.05,dist_mat)

new_cen

price

C = np.argmin(dist_mat[:,new_cen.index],axis = 1)
color = ['y', 'blue', 'green', 'cyan', 'black', 'brown']
for m in range(len(color)):
    id = []
    for i in range(len(C)):
        if (C[i] == m):
            id.append(i)
    plt.scatter(data.loc[id][0],data.loc[id][1], c = color[m])
plt.scatter(new_cen.loc[:][0],new_cen.loc[:][1], c = 'r')

cost = np.sum(np.min(dist_mat[:,new_cen.index],axis = 1))

cost

data1 =  pd.read_csv(f"Shape CSV.csv", header = None)
Q1, C1, price1 = greedyk(data1, 4)
dist_mat1 = distances(data1)
cost1 = np.sum(np.min(dist_mat1[:,Q.index],axis = 1))

Q1

color = ['y', 'blue', 'green', 'cyan', 'black', 'brown']
for m in range(len(color)):
    id = []
    for i in range(len(C1)):
        if (C1[i] == m):
            id.append(i)
    plt.scatter(data1.loc[id][0],data1.loc[id][1], c = color[m])
plt.scatter(Q1.loc[:][0],Q1.loc[:][1], c = 'r')

cost1

new_cen1 = swapping(data1,Q1,cost1,0.05,dist_mat1)

new_cen1

C1 = np.argmin(dist_mat1[:,new_cen1.index],axis = 1)
color = ['y', 'blue', 'green', 'cyan', 'black', 'brown']
for m in range(len(color)):
    id = []
    for i in range(len(C1)):
        if (C1[i] == m):
            id.append(i)
    plt.scatter(data1.loc[id][0],data1.loc[id][1], c = color[m])
plt.scatter(new_cen1.loc[:][0],new_cen1.loc[:][1], c = 'r')



StarcoderdataJupyterScriptsDedupFiltered