Mercurial > hg > nsaunier > traffic-intelligence
comparison python/ml.py @ 913:1cd878812529
work in progress
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Wed, 28 Jun 2017 17:57:06 -0400 |
| parents | b297525b2cbf |
| children | f228fd649644 |
comparison
equal
deleted
inserted
replaced
| 912:fd057a6b04db | 913:1cd878812529 |
|---|---|
| 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, dataset = None, fig = None, colors = utils.colors, nUnitsPerPixel = 1., alpha = 0.3): | 269 def plotGMMClusters(model, labels, 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 labels = model.predict(dataset) | |
| 275 tmpDataset = dataset/nUnitsPerPixel | 274 tmpDataset = dataset/nUnitsPerPixel |
| 276 for i in xrange(model.n_components): | 275 for i in xrange(model.n_components): |
| 277 mean = model.means_[i]/nUnitsPerPixel | 276 mean = model.means_[i]/nUnitsPerPixel |
| 278 covariance = model.covars_[i]/nUnitsPerPixel | 277 covariance = model.covars_[i]/nUnitsPerPixel |
| 279 if dataset is not None: | 278 if dataset is not None: |
| 287 v *= 4 | 286 v *= 4 |
| 288 ell = mpl.patches.Ellipse(mean, v[0], v[1], 180+angle, color=colors[i]) | 287 ell = mpl.patches.Ellipse(mean, v[0], v[1], 180+angle, color=colors[i]) |
| 289 ell.set_clip_box(fig.bbox) | 288 ell.set_clip_box(fig.bbox) |
| 290 ell.set_alpha(alpha) | 289 ell.set_alpha(alpha) |
| 291 fig.axes[0].add_artist(ell) | 290 fig.axes[0].add_artist(ell) |
| 292 return labels |
