-
Notifications
You must be signed in to change notification settings - Fork 0
/
MEANSHIFT
86 lines (67 loc) · 1.99 KB
/
MEANSHIFT
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
#clustering dengan meanshift bukan algorithm kmeans
[]
import modules
import numpy as np
import pandas as pd
from sklearn.cluster import MeanShift
from sklearn.datasets.samples_generator import make_blobs
import matplotlib.pyplot as plt
from matplotlib import style
style.use("ggplot")
[]
#generate sample data terdiri dari 500 poin, membentuk 3 cluster
centers = [[1,1],[5,5],[3,10]]
X, _ = make_blobs(n_samples = 500, centers = centers, cluster_std = 1)
make_blobs?
[]
#menggambar data point
plt.scatter(X[:,0],X[:,1])
plt.show()
[]
ms = MeanShift()
ms.fit(X)
cluster_centers = ms.cluster_centers_
cluster_centers
[]
labels = ms.labels_
labels
[]
print(cluster_centers)
n_clusters_ = len(np.unique(labels))
print("Number of estimated cluster:", n_clusters_)
[]
colors = 10*['r.','g.','b.','c.','k.','y.','m.']
for i in range(len(X)):
plt.plot(X[i][0], X[i][1], colors[labels[i]], markersize= 10)
plt.scatter(cluster_centers[:,0],cluster_centers[:,1],
marker="x", color='k', s=150, linewidths= 5, zorder=10)
plt.show()
[]
#bagaimana kalau kita aplikasikan ke data driver sebelumnya
#import data
df = pd.read_csv('delivery.csv', delimiter=";")
#prepocesssing hanya fitur distance dan speeding
df_2_vars = df.drop(['DRIVER_ID'], axis=1)
data = df_2_vars.values
ms = MeanShift()
ms.fit(data)
cluster_centers = ms.cluster_centers_
cluster_centers
[]
labels = ms.labels_
labels
[]
print(cluster_centers)
n_clusters_ = len(np.unique(labels))
print("Number of estimated clusters:", n_clusters_)
[]
colors = 10*['r.','g.','b.','c.','k.','y.','m.']
for i in range(len(data)):
plt.plot(data[i][0], data[i][1], colors[labels[i]], markersize= 10)
plt.scatter(cluster_centers[:,0],cluster_centers[:,1],
marker="x", color='k', s=150, linewidths= 5, zorder=10)
#"x"(marker)=posisi cluster centernya
plt.show()
[]
#kesimpulan meanshift adalah salah satu bentuk clustering, berguna untuk eksplorasi data dan melihat gambaran awal untuk eksporasi data dan melihat
#gambaran awal mengenai tren yang ada pada data