m
차원 데이터를 그리기는 어렵습니다. 이를 수행하는 한 가지 방법은 Principal Component Analysis (PCA)을 통해 2 차원 공간에 매핑하는 것입니다. 일단 우리가 그것을했다면, 우리는 matplotlib (this answer을 기반으로 함)로 음모에 던질 수 있습니다.
당신이 숫자 이외의 음모하려면
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import mlab
import Pycluster as pc
# make fake user data
users = np.random.normal(0, 10, (20, 5))
# cluster
clusterid, error, nfound = pc.kcluster(users, nclusters=3, transpose=0,
npass=10, method='a', dist='e')
centroids, _ = pc.clustercentroids(users, clusterid=clusterid)
# reduce dimensionality
users_pca = mlab.PCA(users)
cutoff = users_pca.fracs[1]
users_2d = users_pca.project(users, minfrac=cutoff)
centroids_2d = users_pca.project(centroids, minfrac=cutoff)
# make a plot
colors = ['red', 'green', 'blue']
plt.figure()
plt.xlim([users_2d[:,0].min() - .5, users_2d[:,0].max() + .5])
plt.ylim([users_2d[:,1].min() - .5, users_2d[:,1].max() + .5])
plt.xticks([], []); plt.yticks([], []) # numbers aren't meaningful
# show the centroids
plt.scatter(centroids_2d[:,0], centroids_2d[:,1], marker='o', c=colors, s=100)
# show user numbers, colored by their cluster id
for i, ((x,y), kls) in enumerate(zip(users_2d, clusterid)):
plt.annotate(str(i), xy=(x,y), xytext=(0,0), textcoords='offset points',
color=colors[kls])
, 그냥
annotate
에 첫 번째 인수를 변경합니다. 예를 들어 사용자 이름이나 다른 것을 할 수 있습니다.
클러스터가 실제로 발생한 공간이 아니기 때문에이 공간에서 클러스터가 약간 "잘못"보일 수 있습니다 (예 : 15가 녹색보다 아래쪽에 빨간색으로 보임).이 경우 처음 두 개의 주요 구성 요소는 보존됩니다 분산의 61 % :
>>> np.cumsum(users_pca.fracs)
array([ 0.36920636, 0.61313708, 0.81661401, 0.95360623, 1. ])