Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import ipdb # ipdb.set_trace()
- import matplotlib.pyplot as plt
- import numpy as np
- from sklearn.cluster import KMeans
- np.random.seed(0)
- samples = 1000 #por grupo
- # grupo artificialmente criado 1, ao redor do ponto [5,10], com desvio padrão 0.5
- coordX_1 = 5+2.5*np.random.randn(samples)
- coordY_1 = 10+2.5*np.random.randn(samples)
- # grupo artificialmente criado 2 ao redor do ponto [2,2], com desvio padrão 0.5
- coordX_2 = 2+0.5*np.random.randn(samples)
- coordY_2 = 2+0.5*np.random.randn(samples)
- # grupo artificialmente criado 3 ao redor do ponto [7,3], com desvio padrão 0.5
- coordX_3 = 7+0.5*np.random.randn(samples)
- coordY_3 = 3+0.5*np.random.randn(samples)
- coordX = np.concatenate([coordX_1, coordX_2, coordX_3])
- coordY = np.concatenate([coordY_1, coordY_2, coordY_3])
- X = np.column_stack([coordX,coordY])
- #plt.scatter(X[:,0],X[:,1])
- #plt.show()
- #ipdb.set_trace()
- kmeans = KMeans(n_clusters=5, random_state=0).fit(X)
- print(kmeans.labels_)
- print(kmeans.cluster_centers_)
- # plot the decision boundaries, based on https://scikit-learn.org/stable/auto_examples/cluster/plot_kmeans_digits.html
- step = 0.1
- x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
- y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
- xx, yy = np.meshgrid(np.arange(x_min, x_max, step), np.arange(y_min, y_max, step))
- Z = kmeans.predict(np.c_[xx.ravel(), yy.ravel()])
- Z = Z.reshape(xx.shape)
- # plotar o resultado dos clusters baseado em cor
- plt.imshow(Z, interpolation='nearest',
- extent=(xx.min(), xx.max(), yy.min(), yy.max()),
- cmap=plt.cm.Paired,
- aspect='auto', origin='lower')
- # plotar os dados
- plt.plot(X[:, 0], X[:, 1], 'k.', markersize=2)
- # plotar os centroides
- centroids = kmeans.cluster_centers_
- plt.plot(centroids[:,0],centroids[:,1], 'r.', markersize=10)
- plt.show()
Add Comment
Please, Sign In to add comment