Mercurial > hg > nsaunier > traffic-intelligence
diff 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 |
line wrap: on
line diff
--- a/python/ml.py Wed Jun 28 23:43:52 2017 -0400 +++ b/python/ml.py Tue Jul 04 17:03:29 2017 -0400 @@ -264,27 +264,37 @@ similarities[l][k] = similarities[k][l] print('Mean similarity to prototype: {}'.format((similarities[prototypeIndices[i]][cluster].sum()+1)/(n-1))) print('Mean overall similarity: {}'.format((similarities[cluster][:,cluster].sum()+n)/(n*(n-1)))) - + # Gaussian Mixture Models -def plotGMMClusters(model, labels, dataset = None, fig = None, colors = utils.colors, nUnitsPerPixel = 1., alpha = 0.3): +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() - tmpDataset = dataset/nUnitsPerPixel + axes = fig.get_axes() + if len(axes) == 0: + axes = [fig.add_subplot(111)] for i in xrange(model.n_components): mean = model.means_[i]/nUnitsPerPixel covariance = model.covariances_[i]/nUnitsPerPixel + # plot points if dataset is not None: + tmpDataset = dataset/nUnitsPerPixel plt.scatter(tmpDataset[labels == i, 0], tmpDataset[labels == i, 1], .8, color=colors[i]) - plt.annotate(str(i), xy=(mean[0]+1, mean[1]+1)) - - # Plot an ellipse to show the Gaussian component + # plot an ellipse to show the Gaussian component v, w = np.linalg.eigh(covariance) - angle = np.arctan2(w[0][1], w[0][0]) - angle = 180*angle/np.pi # convert to degrees + 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) - fig.axes[0].add_artist(ell) + 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)) + 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) + xwidth = 0.5*(maxima[0]-minima[0]) + ywidth = 0.5*(maxima[1]-minima[1]) + plt.xlim(minima[0]-xwidth,maxima[0]+xwidth) + plt.ylim(minima[1]-ywidth,maxima[1]+ywidth)
