[Matrix] Computation handles unknown values
authorSimon Chabot <simon.chabot@logilab.fr>
Wed, 24 Oct 2012 12:37:56 +0200
changeset 41 9a3e43aaa29b
parent 40 80b7baba500d
child 42 3bee9fcb2080
[Matrix] Computation handles unknown values
matrix.py
--- a/matrix.py	Wed Oct 24 11:51:57 2012 +0200
+++ b/matrix.py	Wed Oct 24 12:37:56 2012 +0200
@@ -38,16 +38,20 @@
                  +----+----+
     """
 
-    def __init__(self, input1, input2, distance):
+    def __init__(self, input1, input2, distance, defvalue = 1):
         self.distance = distance
         self._matrix = lil_matrix((len(input1), len(input2)), dtype='float32')
         self.size = self._matrix.get_shape()
         self._maxdist = 0
-        self._compute(input1, input2)
+        self._compute(input1, input2, defvalue)
 
-    def _compute(self, input1, input2):
+    def _compute(self, input1, input2, defvalue):
        for i in xrange(self.size[0]):
            for j in xrange(self.size[1]):
+               if not (input1[i] and input2[j]):
+                   self._matrix[i, j] = defvalue
+                   continue
+
                self._matrix[i,j] = self.distance(input1[i], input2[j])
                if self._matrix[i,j] > self._maxdist:
                    self._maxdist = self._matrix[i,j]