comparison sparse_instance.py @ 400:269d5c5a4209

Cleaned up, added sparse_instance
author Joseph Turian <turian@gmail.com>
date Tue, 08 Jul 2008 23:59:57 -0400
parents
children 217c8789284b
comparison
equal deleted inserted replaced
399:8796b91a9f09 400:269d5c5a4209
1 """
2 Sparse instances.
3 Each instance is represented as dict with key dimension.
4 Dimensions not present in the dict have value 0.
5 """
6
7 from numpy import zeros
8
9 def to_vector(instances, dimensions):
10 """
11 Convert sparse instances to vectors.
12 @type instances: list of sparse instances
13 @param dimensions: The number of dimensions in each instance.
14 @rtype: numpy matrix (instances x dimensions)
15 """
16 assert isinstance(instances, list)
17 v = zeros((len(instances), dimensions))
18 l = len(instances)
19 for i in range(l):
20 for idx in instances[i].keys():
21 v[i][idx] = instances[i][idx]
22 return v