Mercurial Hosting > traffic-intelligence
diff scripts/learn-poi.py @ 786:1f2b2d1f4fbf dev
added script and code to learn POIs
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Fri, 11 Mar 2016 17:38:48 -0500 |
parents | |
children | 0a428b449b80 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/learn-poi.py Fri Mar 11 17:38:48 2016 -0500 @@ -0,0 +1,43 @@ +#! /usr/bin/env python + +import argparse + +import numpy as np +from sklearn import mixture +import matplotlib.pyplot as plt + +import storage, ml + +parser = argparse.ArgumentParser(description='The program learns and displays Gaussians fit to beginnings and ends of object trajectories (based on Mohamed Gomaa Mohamed 2015 PhD). TODO: save the data') +parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file', required = True) +parser.add_argument('-t', dest = 'trajectoryType', help = 'type of trajectories to display', choices = ['feature', 'object'], default = 'object') +parser.add_argument('-n', dest = 'nClusters', help = 'number of point clusters', required = True, type = int) +parser.add_argument('--covariance-type', dest = 'covarianceType', help = 'type of covariance of Gaussian model', default = "full") + +args = parser.parse_args() + +objects = storage.loadTrajectoriesFromSqlite(args.databaseFilename, args.trajectoryType) + +beginnings = [] +ends = [] +for o in objects: + beginnings.append(o.getPositionAt(0).aslist()) + ends.append(o.getPositionAt(int(o.length())-1).aslist()) + +beginnings = np.array(beginnings) +ends = np.array(ends) + +gmm = mixture.GMM(n_components=args.nClusters, covariance_type = args.covarianceType) +beginningModel=gmm.fit(beginnings) +gmm = mixture.GMM(n_components=args.nClusters, covariance_type = args.covarianceType) +endModel=gmm.fit(ends) + +ml.plotGMMClusters(beginningModel, beginnings) +plt.axis('equal') +plt.title('Origins') +print('Origin Clusters:\n{}'.format(ml.computeClusterSizes(beginningModel.predict(beginnings), range(args.nClusters)))) + +ml.plotGMMClusters(endModel, ends) +plt.axis('equal') +plt.title('Destinations') +print('Destination Clusters:\n{}'.format(ml.computeClusterSizes(endModel.predict(ends), range(args.nClusters))))