Mercurial Hosting > traffic-intelligence
diff 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 |
line wrap: on
line diff
--- a/python/ml.py Tue Jul 04 17:03:29 2017 -0400 +++ b/python/ml.py Tue Jul 04 17:36:24 2017 -0400 @@ -266,14 +266,24 @@ print('Mean overall similarity: {}'.format((similarities[cluster][:,cluster].sum()+n)/(n*(n-1)))) # Gaussian Mixture Models +def plotGMM(mean, covariance, num, fig, color, alpha = 0.3): + v, w = np.linalg.eigh(covariance) + angle = 180*np.arctan2(w[0][1], w[0][0])/np.pi + v *= 4 + ell = mpl.patches.Ellipse(mean, v[0], v[1], 180+angle, color=color) + ell.set_clip_box(fig.bbox) + ell.set_alpha(alpha) + fig.axes[0].add_artist(ell) + plt.plot([mean[0]], [mean[1]], 'x'+color) + plt.annotate(str(num), xy=(mean[0]+1, mean[1]+1)) + def plotGMMClusters(model, labels = None, dataset = None, fig = None, colors = utils.colors, nUnitsPerPixel = 1., alpha = 0.3): '''plot the ellipse corresponding to the Gaussians and the predicted classes of the instances in the dataset''' if fig is None: fig = plt.figure() - axes = fig.get_axes() - if len(axes) == 0: - axes = [fig.add_subplot(111)] + if len(fig.get_axes()) == 0: + fig.add_subplot(111) for i in xrange(model.n_components): mean = model.means_[i]/nUnitsPerPixel covariance = model.covariances_[i]/nUnitsPerPixel @@ -281,16 +291,8 @@ if dataset is not None: tmpDataset = dataset/nUnitsPerPixel plt.scatter(tmpDataset[labels == i, 0], tmpDataset[labels == i, 1], .8, color=colors[i]) - # plot an ellipse to show the Gaussian component - v, w = np.linalg.eigh(covariance) - angle = 180*np.arctan2(w[0][1], w[0][0])/np.pi - v *= 4 - ell = mpl.patches.Ellipse(mean, v[0], v[1], 180+angle, color=colors[i]) - ell.set_clip_box(fig.bbox) - ell.set_alpha(alpha) - axes[0].add_artist(ell) - plt.plot([mean[0]], [mean[1]], 'x'+colors[i]) - plt.annotate(str(i), xy=(mean[0]+1, mean[1]+1)) + # plot an ellipse to show the Gaussian component + plotGMM(mean, covariance, i, fig, colors[i], alpha) if dataset is None: # to address issues without points, the axes limits are not redrawn minima = model.means_.min(0) maxima = model.means_.max(0)