-
Notifications
You must be signed in to change notification settings - Fork 10
/
1. intro.py
59 lines (37 loc) · 1.59 KB
/
1. intro.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import codecademylib3_seaborn
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.cluster import KMeans
import random
import timeit
mu = 1
std = 0.5
np.random.seed(100)
xs = np.append(np.append(np.append(np.random.normal(0.25,std,100), np.random.normal(0.75,std,100)), np.random.normal(0.25,std,100)), np.random.normal(0.75,std,100))
ys = np.append(np.append(np.append(np.random.normal(0.25,std,100), np.random.normal(0.25,std,100)), np.random.normal(0.75,std,100)), np.random.normal(0.75,std,100))
values = list(zip(xs, ys))
model = KMeans(init='random', n_clusters=2)
results = model.fit_predict(values)
print("The inertia of model that randomly initialized centroids is " + str(model.inertia_))
colors = ['#6400e4', '#ffc740']
plt.subplot(211)
for i in range(2):
points = np.array([values[j] for j in range(len(values)) if results[j] == i])
plt.scatter(points[:, 0], points[:, 1], c=colors[i], alpha=0.6)
plt.title('Codecademy Mobile Feedback - Centroids Initialized Randomly')
plt.xlabel('Learn Python')
plt.ylabel('Learn SQL')
plt.subplot(212)
model = KMeans( n_clusters=2)
results = model.fit_predict(values)
print("The inertia of model that initialized the centroids using KMeans++ is " + str(model.inertia_))
colors = ['#6400e4', '#ffc740']
for i in range(2):
points = np.array([values[j] for j in range(len(values)) if results[j] == i])
plt.scatter(points[:, 0], points[:, 1], c=colors[i], alpha=0.6)
plt.title('Codecademy Mobile Feedback - Centroids Initialized Using KMeans++')
plt.xlabel('Learn Python')
plt.ylabel('Learn SQL')
plt.tight_layout()
plt.show()