comparison python/ml.py @ 915:13434f5017dd

work to save trajectory assignment to origin and destinations
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 04 Jul 2017 17:03:29 -0400
parents f228fd649644
children 7345f0d51faa
comparison
equal deleted inserted replaced
914:f228fd649644 915:13434f5017dd
262 if similarities[k][l] < 0: 262 if similarities[k][l] < 0:
263 similarities[k][l] = similarityFunc(instances[k], instances[l]) 263 similarities[k][l] = similarityFunc(instances[k], instances[l])
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 plotGMMClusters(model, labels, dataset = None, fig = None, colors = utils.colors, nUnitsPerPixel = 1., alpha = 0.3): 269 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 270 '''plot the ellipse corresponding to the Gaussians
271 and the predicted classes of the instances in the dataset''' 271 and the predicted classes of the instances in the dataset'''
272 if fig is None: 272 if fig is None:
273 fig = plt.figure() 273 fig = plt.figure()
274 tmpDataset = dataset/nUnitsPerPixel 274 axes = fig.get_axes()
275 if len(axes) == 0:
276 axes = [fig.add_subplot(111)]
275 for i in xrange(model.n_components): 277 for i in xrange(model.n_components):
276 mean = model.means_[i]/nUnitsPerPixel 278 mean = model.means_[i]/nUnitsPerPixel
277 covariance = model.covariances_[i]/nUnitsPerPixel 279 covariance = model.covariances_[i]/nUnitsPerPixel
280 # plot points
278 if dataset is not None: 281 if dataset is not None:
282 tmpDataset = dataset/nUnitsPerPixel
279 plt.scatter(tmpDataset[labels == i, 0], tmpDataset[labels == i, 1], .8, color=colors[i]) 283 plt.scatter(tmpDataset[labels == i, 0], tmpDataset[labels == i, 1], .8, color=colors[i])
280 plt.annotate(str(i), xy=(mean[0]+1, mean[1]+1)) 284 # plot an ellipse to show the Gaussian component
281
282 # Plot an ellipse to show the Gaussian component
283 v, w = np.linalg.eigh(covariance) 285 v, w = np.linalg.eigh(covariance)
284 angle = np.arctan2(w[0][1], w[0][0]) 286 angle = 180*np.arctan2(w[0][1], w[0][0])/np.pi
285 angle = 180*angle/np.pi # convert to degrees
286 v *= 4 287 v *= 4
287 ell = mpl.patches.Ellipse(mean, v[0], v[1], 180+angle, color=colors[i]) 288 ell = mpl.patches.Ellipse(mean, v[0], v[1], 180+angle, color=colors[i])
288 ell.set_clip_box(fig.bbox) 289 ell.set_clip_box(fig.bbox)
289 ell.set_alpha(alpha) 290 ell.set_alpha(alpha)
290 fig.axes[0].add_artist(ell) 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
295 minima = model.means_.min(0)
296 maxima = model.means_.max(0)
297 xwidth = 0.5*(maxima[0]-minima[0])
298 ywidth = 0.5*(maxima[1]-minima[1])
299 plt.xlim(minima[0]-xwidth,maxima[0]+xwidth)
300 plt.ylim(minima[1]-ywidth,maxima[1]+ywidth)