annotate sparse_instance.py @ 524:317a052f9b14

better main, allow to debug in a debugger.
author Frederic Bastien <bastienf@iro.umontreal.ca>
date Fri, 14 Nov 2008 16:46:03 -0500
parents 217c8789284b
children
rev   line source
400
269d5c5a4209 Cleaned up, added sparse_instance
Joseph Turian <turian@gmail.com>
parents:
diff changeset
1 """
269d5c5a4209 Cleaned up, added sparse_instance
Joseph Turian <turian@gmail.com>
parents:
diff changeset
2 Sparse instances.
269d5c5a4209 Cleaned up, added sparse_instance
Joseph Turian <turian@gmail.com>
parents:
diff changeset
3 Each instance is represented as dict with key dimension.
269d5c5a4209 Cleaned up, added sparse_instance
Joseph Turian <turian@gmail.com>
parents:
diff changeset
4 Dimensions not present in the dict have value 0.
269d5c5a4209 Cleaned up, added sparse_instance
Joseph Turian <turian@gmail.com>
parents:
diff changeset
5 """
269d5c5a4209 Cleaned up, added sparse_instance
Joseph Turian <turian@gmail.com>
parents:
diff changeset
6
269d5c5a4209 Cleaned up, added sparse_instance
Joseph Turian <turian@gmail.com>
parents:
diff changeset
7 from numpy import zeros
269d5c5a4209 Cleaned up, added sparse_instance
Joseph Turian <turian@gmail.com>
parents:
diff changeset
8
269d5c5a4209 Cleaned up, added sparse_instance
Joseph Turian <turian@gmail.com>
parents:
diff changeset
9 def to_vector(instances, dimensions):
269d5c5a4209 Cleaned up, added sparse_instance
Joseph Turian <turian@gmail.com>
parents:
diff changeset
10 """
269d5c5a4209 Cleaned up, added sparse_instance
Joseph Turian <turian@gmail.com>
parents:
diff changeset
11 Convert sparse instances to vectors.
269d5c5a4209 Cleaned up, added sparse_instance
Joseph Turian <turian@gmail.com>
parents:
diff changeset
12 @type instances: list of sparse instances
269d5c5a4209 Cleaned up, added sparse_instance
Joseph Turian <turian@gmail.com>
parents:
diff changeset
13 @param dimensions: The number of dimensions in each instance.
269d5c5a4209 Cleaned up, added sparse_instance
Joseph Turian <turian@gmail.com>
parents:
diff changeset
14 @rtype: numpy matrix (instances x dimensions)
401
217c8789284b Small updates
Joseph Turian <turian@gmail.com>
parents: 400
diff changeset
15 @todo: Allow this function to convert SINGLE instances (not lists).
400
269d5c5a4209 Cleaned up, added sparse_instance
Joseph Turian <turian@gmail.com>
parents:
diff changeset
16 """
269d5c5a4209 Cleaned up, added sparse_instance
Joseph Turian <turian@gmail.com>
parents:
diff changeset
17 v = zeros((len(instances), dimensions))
269d5c5a4209 Cleaned up, added sparse_instance
Joseph Turian <turian@gmail.com>
parents:
diff changeset
18 l = len(instances)
269d5c5a4209 Cleaned up, added sparse_instance
Joseph Turian <turian@gmail.com>
parents:
diff changeset
19 for i in range(l):
269d5c5a4209 Cleaned up, added sparse_instance
Joseph Turian <turian@gmail.com>
parents:
diff changeset
20 for idx in instances[i].keys():
269d5c5a4209 Cleaned up, added sparse_instance
Joseph Turian <turian@gmail.com>
parents:
diff changeset
21 v[i][idx] = instances[i][idx]
269d5c5a4209 Cleaned up, added sparse_instance
Joseph Turian <turian@gmail.com>
parents:
diff changeset
22 return v