comparison 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
comparison
equal deleted inserted replaced
785:3aa6102ccc12 786:1f2b2d1f4fbf
1 #! /usr/bin/env python
2
3 import argparse
4
5 import numpy as np
6 from sklearn import mixture
7 import matplotlib.pyplot as plt
8
9 import storage, ml
10
11 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')
12 parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file', required = True)
13 parser.add_argument('-t', dest = 'trajectoryType', help = 'type of trajectories to display', choices = ['feature', 'object'], default = 'object')
14 parser.add_argument('-n', dest = 'nClusters', help = 'number of point clusters', required = True, type = int)
15 parser.add_argument('--covariance-type', dest = 'covarianceType', help = 'type of covariance of Gaussian model', default = "full")
16
17 args = parser.parse_args()
18
19 objects = storage.loadTrajectoriesFromSqlite(args.databaseFilename, args.trajectoryType)
20
21 beginnings = []
22 ends = []
23 for o in objects:
24 beginnings.append(o.getPositionAt(0).aslist())
25 ends.append(o.getPositionAt(int(o.length())-1).aslist())
26
27 beginnings = np.array(beginnings)
28 ends = np.array(ends)
29
30 gmm = mixture.GMM(n_components=args.nClusters, covariance_type = args.covarianceType)
31 beginningModel=gmm.fit(beginnings)
32 gmm = mixture.GMM(n_components=args.nClusters, covariance_type = args.covarianceType)
33 endModel=gmm.fit(ends)
34
35 ml.plotGMMClusters(beginningModel, beginnings)
36 plt.axis('equal')
37 plt.title('Origins')
38 print('Origin Clusters:\n{}'.format(ml.computeClusterSizes(beginningModel.predict(beginnings), range(args.nClusters))))
39
40 ml.plotGMMClusters(endModel, ends)
41 plt.axis('equal')
42 plt.title('Destinations')
43 print('Destination Clusters:\n{}'.format(ml.computeClusterSizes(endModel.predict(ends), range(args.nClusters))))