Mercurial > pylearn
annotate sparse_instance.py @ 450:117e5b09cf31
Added an XlogX op.
author | Joseph Turian <turian@gmail.com> |
---|---|
date | Thu, 04 Sep 2008 14:46:17 -0400 |
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 | 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 |