diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sparse_instance.py	Tue Jul 08 23:59:57 2008 -0400
@@ -0,0 +1,22 @@
+"""
+Sparse instances.
+Each instance is represented as dict with key dimension.
+Dimensions not present in the dict have value 0.
+"""
+
+from numpy import zeros
+
+def to_vector(instances, dimensions):
+    """
+    Convert sparse instances to vectors.
+    @type instances: list of sparse instances
+    @param dimensions: The number of dimensions in each instance.
+    @rtype: numpy matrix (instances x dimensions)
+    """
+    assert isinstance(instances, list)
+    v = zeros((len(instances), dimensions))
+    l = len(instances)
+    for i in range(l):
+        for idx in instances[i].keys():
+            v[i][idx] = instances[i][idx]
+    return v