<jupyter_start><jupyter_text><jupyter_code>import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
xy = [[1,1],[0,0],[2,2],[1.5,1.5],[1,1.5],[4.5,4.5],[5,4],[4.5,5],[5,5],[5.5,6]]
xy = np.array(xy)
print(len(xy))
from sklearn.datasets import make_blobs
plt.figure(figsize=(80, 80))
plt.subplots_adjust(bottom=.05, top=.9, left=.05, right=.95)
def plot_kmeans(xy, c12):
    plt.scatter(xy[:,0], xy[:,1])
    plt.scatter(c12[:,0], c12[:,1], marker="x", color='r')
    plt.show()
    
def find_distance(xy,c12):
    dist0 = np.linalg.norm(xy-c12[0],axis=1,ord=2)
    dist1 = np.linalg.norm(xy-c12[1],axis=1,ord=2)
    return (dist0, dist1)

def find_groups(D):
    S = D[0]<=D[1]
    return S.astype('float')


def update_centroids(xy, S):
    S0 = np.where(S==0)
    S1 = np.where(S==1)
    c12[0] = (np.sum(xy[S0],axis=0))/len(S0[0])
    c12[1] = (np.sum(xy[S1],axis=0))/len(S1[0])
    return c12

plt.subplot(111)
plt.title("Three blobs", fontsize='small')
X1, Y1 = make_blobs(n_features=2, centers=2)
plt.scatter(X1[:, 0], X1[:, 1], marker='o',
            s=25, edgecolor='k')

xy = X1
plt.scatter(xy[:,0], xy[:,1])
c12 = np.array([[0.1,0.1],[0.3,0.4]])
xy
plot_kmeans(xy, c12)
print(min(min(xy[0]), min(xy[1])))
print(max(max(xy[0]), max(xy[1])))

min_v = min(min(xy[0]), min(xy[1]))
max_v = max(max(xy[0]), max(xy[1]))
from collections import defaultdict
C = defaultdict(list)
for i in range(10):
    D = find_distance(xy,c12)
    G = find_groups(D)
    c12 = update_centroids(xy, G)
    print(c12)
    C[str(i)] = c12
    print(D,G,c12)
    plot_kmeans(xy, c12)
c12
print(np.mean(c12,axis=0))<jupyter_output>[0.36519393 5.27139002]<jupyter_text>\begin{equation}ax+by=c\\by = -ax+c\\y = 1/b*(-ax+c)\end{equation}<jupyter_code>p1, p2 = c12
print(p1)
print(p2)

def find_slope(p1, p2):
    x1, y1 = p1
    x2, y2 = p2
#     print(y2, y1)
#     print(x2, x1)
    m = (y2-y1)/(x2-x1)
    return m
def perpendicular_slope(m1):
    return -(1/m1)
# needed to find the line passing throgh the points
# needed to find the perpendicular line passing through a point

def get_c(slope, point):
    c = point[1] - slope*point[0]
    return c
    
def line_passing_through(point, slope):
    return get_c(slope, point)
plt.gcf().clear()
m = find_slope(c12[0], c12[1])

c = line_passing_through(c12[0], m)
c = line_passing_through(c12[1], m)

x = np.linspace(min_v, max_v, 100, dtype='float')
y = m*x+c
plt.plot(x,y)
plt.hold(True)


m1 = m
m2 = perpendicular_slope(m1)
c1 = get_c(m2, c12[0])
y = m2*x+c1
plt.plot(x,y)
plt.hold(True)

c2 = get_c(m2, c12[1])
y = m2*x+c2
plt.plot(x,y)
plt.hold(True)

cm = get_c(m2, np.mean(c12, axis=0))
y = m2*x+cm
plt.plot(x,y)
plt.hold(True)


plot_kmeans(xy, c12)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2125091441111924, 'fraction_numerical': 0.08266276517922458, 'mean_word_length': 4.437375745526839, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '220661', 'license': 'MIT', 'n_tokens_mistral': 1414, 'n_tokens_neox': 1319, 'n_words': 230, 'path': 'k_means_clustering.ipynb', 'repo_name': 'machinelearning147/100DaysOfMLCode', 'size': '2734'}