In [6]:
from sklearn.datasets import load_iris
iris = load_iris()
In [2]:
from sklearn.cluster import KMeans
X = iris.data
k_means = KMeans(n_clusters=3)
k_means.fit(X)
Out[2]:
In [3]:
k_means.labels_
Out[3]:
In [4]:
figsize(14,5)
subplot(1,2,1)
scatter(X[:,2], X[:,3], c=k_means.labels_)
title(u'K Médias')
subplot(1,2,2)
scatter(X[:,2], X[:,3], c=iris.target)
title(u'Ground-truth')
Out[4]:
In [5]:
from mpl_toolkits.mplot3d import Axes3D
fig = figure(figsize=(14,6))
ax = fig.add_subplot(1, 2, 1, projection='3d')
ax.scatter(X[:, 0], X[:, 2], X[:, 3], c=k_means.labels_)
ax.set_title(u'K Médias')
ax = fig.add_subplot(1, 2, 2, projection='3d')
ax.scatter(X[:, 0], X[:, 2], X[:, 3], c=iris.target)
ax.set_title('Ground-truth')
Out[5]:
In [42]:
import cv2
I = cv2.cvtColor(cv2.imread('./data/BSD-118035.jpg'), cv2.COLOR_BGR2RGB)
In [43]:
imshow(I)
Out[43]:
In [44]:
h, w, _ = I_Lab.shape
X = I.reshape(h*w, -1)
X
Out[44]:
In [45]:
from sklearn.cluster import MeanShift, estimate_bandwidth
In [46]:
b = estimate_bandwidth(X, quantile=0.1, n_samples=2500)
ms = MeanShift(bandwidth=b, bin_seeding=True)
ms.fit(X)
Out[46]:
A opção bin_seeding=True
initializes the kernel locations to discretized version of points, where points are binned onto a grid whose coarseness corresponds to the bandwidth.
ms.labels_
keeps the cluster identification for each pixelms.cluster_centers_
stores the cluster centers
In [47]:
S = zeros_like(I)
L = ms.labels_.reshape(h, w)
num_clusters = ms.cluster_centers_.shape[0]
print num_clusters
for c in range(num_clusters):
S[L == c] = ms.cluster_centers_[c]
In [48]:
subplot(1,2,1)
imshow(I)
subplot(1,2,2)
imshow(S)
Out[48]:
In [51]:
from mpl_toolkits.mplot3d import Axes3D
fig = figure(figsize=(14,8))
ax = fig.add_subplot(1, 2, 1, projection='3d')
centroid_color = [ms.cluster_centers_[c]/255 for c in ms.labels_]
ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=array(X, dtype=float32)/255)
ax.set_title('Cores originais')
ax = fig.add_subplot(1, 2, 2, projection='3d')
centroid_color = [ms.cluster_centers_[c]/255 for c in ms.labels_]
ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=centroid_color)
ax.set_title('Clustering')
Out[51]: