comparison python/ml.py @ 805:180b6b0231c0

added saving/loading points of interests
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 09 Jun 2016 15:36:21 -0400
parents 1158a6e2d28e
children 52aa03260f03
comparison
equal deleted inserted replaced
801:c5f98916297e 805:180b6b0231c0
207 clusterSizes = {i: sum(np.array(labels) == i) for i in prototypeIndices} 207 clusterSizes = {i: sum(np.array(labels) == i) for i in prototypeIndices}
208 clusterSizes['outlier'] = sum(np.array(labels) == outlierIndex) 208 clusterSizes['outlier'] = sum(np.array(labels) == outlierIndex)
209 return clusterSizes 209 return clusterSizes
210 210
211 # Gaussian Mixture Models 211 # Gaussian Mixture Models
212 def plotGMMClusters(model, dataset = None, fig = None, colors = utils.colors, nPixelsPerUnit = 1., alpha = 0.3): 212 def plotGMMClusters(model, dataset = None, fig = None, colors = utils.colors, nUnitsPerPixel = 1., alpha = 0.3):
213 '''plot the ellipse corresponding to the Gaussians 213 '''plot the ellipse corresponding to the Gaussians
214 and the predicted classes of the instances in the dataset''' 214 and the predicted classes of the instances in the dataset'''
215 if fig is None: 215 if fig is None:
216 fig = plt.figure() 216 fig = plt.figure()
217 labels = model.predict(dataset) 217 labels = model.predict(dataset)
218 tmpDataset = nPixelsPerUnit*dataset 218 tmpDataset = dataset/nUnitsPerPixel
219 for i in xrange(model.n_components): 219 for i in xrange(model.n_components):
220 mean = nPixelsPerUnit*model.means_[i] 220 mean = model.means_[i]/nUnitsPerPixel
221 covariance = nPixelsPerUnit*model.covars_[i] 221 covariance = model.covars_[i]/nUnitsPerPixel
222 if dataset is not None: 222 if dataset is not None:
223 plt.scatter(tmpDataset[labels == i, 0], tmpDataset[labels == i, 1], .8, color=colors[i]) 223 plt.scatter(tmpDataset[labels == i, 0], tmpDataset[labels == i, 1], .8, color=colors[i])
224 plt.annotate(str(i), xy=(mean[0]+1, mean[1]+1)) 224 plt.annotate(str(i), xy=(mean[0]+1, mean[1]+1))
225 225
226 # Plot an ellipse to show the Gaussian component 226 # Plot an ellipse to show the Gaussian component
230 v *= 4 230 v *= 4
231 ell = mpl.patches.Ellipse(mean, v[0], v[1], 180+angle, color=colors[i]) 231 ell = mpl.patches.Ellipse(mean, v[0], v[1], 180+angle, color=colors[i])
232 ell.set_clip_box(fig.bbox) 232 ell.set_clip_box(fig.bbox)
233 ell.set_alpha(alpha) 233 ell.set_alpha(alpha)
234 fig.axes[0].add_artist(ell) 234 fig.axes[0].add_artist(ell)
235 return labels