-
Notifications
You must be signed in to change notification settings - Fork 1
/
kmeans.py
112 lines (84 loc) · 3.84 KB
/
kmeans.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# I have adapted this code from https://flothesof.github.io/k-means-numpy.html
# necessary imports
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
sns.set(style='ticks', palette='Set2')
from datetime import datetime
startTime= datetime.now()
# NOTE
# Data currently is Driver_ID Distance_Feature Speeding_Feature
######################################## Retrieving Data from the Files I have #########################################
my_data = np.genfromtxt('data_1024.csv', delimiter='\t')
my_data = my_data[:, 1:]
########################################### Now we Initialize the Points ##############################################
def initialize_centroids(points, k):
"""returns k centroids from the initial points"""
centroids = points.copy()
np.random.shuffle(centroids)
return centroids[:k]
######################################### Now we Initialize the Centroids #############################################
def closest_centroid(points, centroids):
"""returns an array containing the index to the nearest centroid for each point"""
distances = np.sqrt(((points - centroids[:, np.newaxis]) ** 2).sum(axis=2))
return np.argmin(distances, axis=0)
######################################### Now to Move the Centroids ####################################################
def move_centroids(points, closest, centroids):
"""returns the new centroids assigned from the points closest to them"""
return np.array([points[closest==k].mean(axis=0) for k in range(centroids.shape[0])])
############################################## Final Graphing #########################################################
# These are the Performance Plots
# plt.subplot(121)
# plt.scatter(my_data[:, 0], my_data[:, 1])
# centroids = initialize_centroids(my_data, 3)
# plt.scatter(centroids[:, 0], centroids[:, 1], c='r', s=100)
# plt.title('Initial K Centroids')
# plt.xlabel('Distance Driven')
# plt.ylabel('Percentage Time Spent Speeding')
# plt.suptitle('K-Means Algorithm In Action', fontsize=16)
# Plain Drawing
# plt.scatter(my_data[:, 0], my_data[:, 1])
# plt.xlabel('Distance Driven')
# plt.ylabel('Percentage Time Spent Speeding')
# plt.suptitle('Driving Data', fontsize=16)
# Presentation Plots
plt.subplot(221)
centroids = initialize_centroids(my_data, 4)
closest = closest_centroid(my_data, centroids)
centroids = move_centroids(my_data, closest, centroids)
plt.scatter(my_data[:, 0], my_data[:, 1], c=closest)
plt.title('Final K Centroids 1')
plt.xlabel('Distance Driven')
plt.ylabel('Percentage Time Spent Speeding')
plt.scatter(centroids[:, 0], centroids[:, 1], c='r', s=100)
timeElapsed = datetime.now()-startTime
timeElapsed = timeElapsed.total_seconds()
print timeElapsed
plt.subplot(222)
centroids = initialize_centroids(my_data, 4)
closest = closest_centroid(my_data, centroids)
centroids = move_centroids(my_data, closest, centroids)
plt.scatter(my_data[:, 0], my_data[:, 1], c=closest)
plt.title('Final K Centroids 2')
plt.xlabel('Distance Driven')
plt.ylabel('Percentage Time Spent Speeding')
plt.scatter(centroids[:, 0], centroids[:, 1], c='r', s=100)
plt.subplot(223)
centroids = initialize_centroids(my_data, 4)
closest = closest_centroid(my_data, centroids)
centroids = move_centroids(my_data, closest, centroids)
plt.scatter(my_data[:, 0], my_data[:, 1], c=closest)
plt.title('Final K Centroids 3')
plt.xlabel('Distance Driven')
plt.ylabel('Percentage Time Spent Speeding')
plt.scatter(centroids[:, 0], centroids[:, 1], c='r', s=100)
plt.subplot(224)
centroids = initialize_centroids(my_data, 4)
closest = closest_centroid(my_data, centroids)
centroids = move_centroids(my_data, closest, centroids)
plt.scatter(my_data[:, 0], my_data[:, 1], c=closest)
plt.title('Final K Centroids 4')
plt.xlabel('Distance Driven')
plt.ylabel('Percentage Time Spent Speeding')
plt.scatter(centroids[:, 0], centroids[:, 1], c='r', s=100)
plt.show()