Strange Bug in creating Co-occurrence Matrix in Python
<p>I am trying to create a co-occurrence matrix in Python that outputs the number which words in L1 appear in pears (cat dog, cat house, cat tree e.t.c.) in L2, my code so far is:</p>

<pre><code>co = np.zeros((5,5)) #the matrix
L1 = ['cat', 'dog', 'house', 'tree', 'car'] #tags
L2 = ['cat car dog', 'cat house dog', 'cat car', 'cat dog'] #photo text

n=0 # will hold the sum of each occurance

for i in range(len(L1)):
    for j in range(len(L1)):
        for s in range(len(L2)):
            #find occurrence but not on same words
            if L1[i] in L2[s] and L1[j] in L2[s] and L1[i] != L1[j]: 
                n+=1  # sum the number of occurances            
                #output = L1[i], L1[j] # L2[s]
                #print output
                co[i][j] = s #add to the matrix

print co
</code></pre>

<p>The output should be </p>

<pre><code>[[ 0.  3.  1.  0.  2.]
 [ 3.  0.  1.  0.  1.]
 [ 1.  1.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.]
 [ 2.  1.  0.  0.  0.]]
</code></pre>

<p>But instead:</p>

<pre><code>[[ 0.  3.  1.  0.  2.]
 [ 3.  0.  1.  0.  0.]
 [ 1.  1.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.]
 [ 2.  0.  0.  0.  0.]]
</code></pre>

<p>Every second row there is an error... The if part works well, I have checked the output:</p>

<pre><code>output = L1[i], L1[j] # L2[s]
print output
    ('cat', 'dog')
    ('cat', 'dog')
    ('cat', 'dog')
    ('cat', 'house')
    ('cat', 'car')
    ('cat', 'car')
    ('dog', 'cat')
    ('dog', 'cat')
    ('dog', 'cat')
    ('dog', 'house')
    ('dog', 'car')
    ('house', 'cat')
    ('house', 'dog')
    ('car', 'cat')
    ('car', 'cat')
    ('car', 'dog')
</code></pre>

<p>So I guess there is something going on when filing the matrix?:</p>

<pre><code>co[i][j] = s
</code></pre>

<p>Any suggestions???</p>

<p>It's giving a correct result because you have <code>car</code> and <code>dog</code> in first item of <code>L2</code> which is <code>0</code> index.</p>

<p>Here is a more pythonic approach that get the index based on first occurrence of the pairs in <code>L2</code>:</p>

<pre><code>In [158]: L2 = ['cat car dog', 'cat house dog', 'cat car', 'cat dog']

In [159]: L2 = [s.split() for s in L2]

In [160]: combinations = np.column_stack((np.repeat(L1, 5), np.tile(L1, 5))).reshape(5, 5, 2)
# with 0 as the start of the indices
In [162]: [[next((i for i, sub in enumerate(L2) if x in sub and y in sub), 0) for x, y in row] for row in combinations]
Out[162]: 
[[0, 0, 1, 0, 0],
 [0, 0, 1, 0, 0],
 [1, 1, 1, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0]]
# with 1 as the start of the indices
In [163]: [[next((i for i, sub in enumerate(L2, 1) if x in sub and y in sub), 0) for x, y in row] for row in combinations]
Out[163]: 
[[1, 1, 2, 0, 1],
 [1, 1, 2, 0, 1],
 [2, 2, 2, 0, 0],
 [0, 0, 0, 0, 0],
 [1, 1, 0, 0, 1]]
</code></pre>

python numpy matrix