comparison python/ml.py @ 916:7345f0d51faa

added display of paths
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 04 Jul 2017 17:36:24 -0400
parents 13434f5017dd
children 89cc05867c4c
comparison
equal deleted inserted replaced
915:13434f5017dd 916:7345f0d51faa
264 similarities[l][k] = similarities[k][l] 264 similarities[l][k] = similarities[k][l]
265 print('Mean similarity to prototype: {}'.format((similarities[prototypeIndices[i]][cluster].sum()+1)/(n-1))) 265 print('Mean similarity to prototype: {}'.format((similarities[prototypeIndices[i]][cluster].sum()+1)/(n-1)))
266 print('Mean overall similarity: {}'.format((similarities[cluster][:,cluster].sum()+n)/(n*(n-1)))) 266 print('Mean overall similarity: {}'.format((similarities[cluster][:,cluster].sum()+n)/(n*(n-1))))
267 267
268 # Gaussian Mixture Models 268 # Gaussian Mixture Models
269 def plotGMM(mean, covariance, num, fig, color, alpha = 0.3):
270 v, w = np.linalg.eigh(covariance)
271 angle = 180*np.arctan2(w[0][1], w[0][0])/np.pi
272 v *= 4
273 ell = mpl.patches.Ellipse(mean, v[0], v[1], 180+angle, color=color)
274 ell.set_clip_box(fig.bbox)
275 ell.set_alpha(alpha)
276 fig.axes[0].add_artist(ell)
277 plt.plot([mean[0]], [mean[1]], 'x'+color)
278 plt.annotate(str(num), xy=(mean[0]+1, mean[1]+1))
279
269 def plotGMMClusters(model, labels = None, dataset = None, fig = None, colors = utils.colors, nUnitsPerPixel = 1., alpha = 0.3): 280 def plotGMMClusters(model, labels = None, dataset = None, fig = None, colors = utils.colors, nUnitsPerPixel = 1., alpha = 0.3):
270 '''plot the ellipse corresponding to the Gaussians 281 '''plot the ellipse corresponding to the Gaussians
271 and the predicted classes of the instances in the dataset''' 282 and the predicted classes of the instances in the dataset'''
272 if fig is None: 283 if fig is None:
273 fig = plt.figure() 284 fig = plt.figure()
274 axes = fig.get_axes() 285 if len(fig.get_axes()) == 0:
275 if len(axes) == 0: 286 fig.add_subplot(111)
276 axes = [fig.add_subplot(111)]
277 for i in xrange(model.n_components): 287 for i in xrange(model.n_components):
278 mean = model.means_[i]/nUnitsPerPixel 288 mean = model.means_[i]/nUnitsPerPixel
279 covariance = model.covariances_[i]/nUnitsPerPixel 289 covariance = model.covariances_[i]/nUnitsPerPixel
280 # plot points 290 # plot points
281 if dataset is not None: 291 if dataset is not None:
282 tmpDataset = dataset/nUnitsPerPixel 292 tmpDataset = dataset/nUnitsPerPixel
283 plt.scatter(tmpDataset[labels == i, 0], tmpDataset[labels == i, 1], .8, color=colors[i]) 293 plt.scatter(tmpDataset[labels == i, 0], tmpDataset[labels == i, 1], .8, color=colors[i])
284 # plot an ellipse to show the Gaussian component 294 # plot an ellipse to show the Gaussian component
285 v, w = np.linalg.eigh(covariance) 295 plotGMM(mean, covariance, i, fig, colors[i], alpha)
286 angle = 180*np.arctan2(w[0][1], w[0][0])/np.pi
287 v *= 4
288 ell = mpl.patches.Ellipse(mean, v[0], v[1], 180+angle, color=colors[i])
289 ell.set_clip_box(fig.bbox)
290 ell.set_alpha(alpha)
291 axes[0].add_artist(ell)
292 plt.plot([mean[0]], [mean[1]], 'x'+colors[i])
293 plt.annotate(str(i), xy=(mean[0]+1, mean[1]+1))
294 if dataset is None: # to address issues without points, the axes limits are not redrawn 296 if dataset is None: # to address issues without points, the axes limits are not redrawn
295 minima = model.means_.min(0) 297 minima = model.means_.min(0)
296 maxima = model.means_.max(0) 298 maxima = model.means_.max(0)
297 xwidth = 0.5*(maxima[0]-minima[0]) 299 xwidth = 0.5*(maxima[0]-minima[0])
298 ywidth = 0.5*(maxima[1]-minima[1]) 300 ywidth = 0.5*(maxima[1]-minima[1])