How do I create a list of true and false values when comparing two numpy arrays?
<p>I'm creating a list of true and false values out of the following array using list comprehension:</p>

<pre><code>array([[ True,  True, False, ..., False,  True, False],
   [ True,  True, False, ..., False,  True,  True],
   [ True, False,  True, ..., False,  True, False],
   ..., 
   [ True,  True, False, ...,  True,  True, False],
   [ True,  True, False, ...,  True,  True, False],
   [ True,  True, False, ...,  True,  True, False]], dtype=bool)
</code></pre>

<p>(It is has 50 X 85 dimensions)</p>

<p>This is my list comprehension:</p>

<pre><code>list_1 = [features[index] &amp; features[i] for i in features]
</code></pre>

<p>where <code>index</code> is an integer, (in my case, it's <code>14</code>).</p>

<p>This is what <code>print(features[index])</code> looks like:</p>

<pre><code>[ True  True False False False False False False  True False False  True
  True False False False False  True  True False False False  True False
  True  True  True  True False False  True  True  True  True  True False
 False False False False False False False False  True  True False False
 False  True  True False  True False  True False  True False  True  True
 False  True  True  True False False  True  True False False  True False
 False  True  True False False  True  True False]
</code></pre>

<p>and an example of <code>print(features[i])</code> looks like:</p>

<pre><code>[False False False False  True False  True False  True False False False
 False False False False False False False  True False False False False
 False  True False  True False False  True False False False False  True
 False False False False  True  True  True False False  True  True  True
 False False False  True  True False False False False False False False
  True  True  True False False  True False  True False  True False False
  True  True  True False False  True False False]
</code></pre>

<p>Thus, both arrays seem to be of the same length. However, when I compare the index array to all of the other arrays in the feature array, I get the following error:</p>

<pre><code>IndexError: index 54 is out of bounds for axis 0 with size 50
</code></pre>

<p><strong>Question:</strong> how do I create a list of true and false values when comparing the <code>features[index]</code> array to <strong>ALL</strong> other arrays in the "features" array? 
Ultimately, the list should be composed of true values where the elements in <code>features[index]</code> matches the other elements in <strong>ALL</strong> of the other arrays. Thus, it's unlikely that there'll be many true values in the list. The list should be composed of false values where the elements in <code>features[index]</code> doesn't match the elements in <strong>ANY</strong> of the arrays of the features array. </p>

<p>With your array excerpt I get the same sort of error with just the indexing:</p>

<pre><code>In [726]: features
Out[726]: 
array([[ True,  True, False,  True, False,  True, False],
       [ True,  True, False,  True, False,  True,  True],
       [ True, False,  True,  True, False,  True, False],
       [ True,  True, False,  True,  True,  True, False],
       [ True,  True, False,  True,  True,  True, False],
       [ True,  True, False,  True,  True,  True, False]], dtype=bool)
In [727]: [features[i] for i in features]
/usr/local/bin/ipython3:1: VisibleDeprecationWarning: boolean index did not match indexed array along dimension 0; dimension is 6 but corresponding boolean dimension is 7
...
IndexError: index 6 is out of bounds for axis 0 with size 6
</code></pre>

<p><code>[... for i in features]</code> iterates on the rows of <code>features</code>.  So <code>features[i]</code> is trying to index the rows of <code>features</code> with the i'th row of features.</p>

<p>The correct iteration is:</p>

<pre><code>In [730]: [features[0] &amp; features[i] for i in range(features.shape[0])]
Out[730]: 
[array([ True,  True, False,  True, False,  True, False], dtype=bool),
 array([ True,  True, False,  True, False,  True, False], dtype=bool),
 array([ True, False, False,  True, False,  True, False], dtype=bool),
 array([ True,  True, False,  True, False,  True, False], dtype=bool),
 array([ True,  True, False,  True, False,  True, False], dtype=bool),
 array([ True,  True, False,  True, False,  True, False], dtype=bool)]
</code></pre>

<p>or even <code>[features[0] &amp; i for i in features]</code>.</p>

<p>But you don't need to do this in a list comprehension.  <code>features[i]</code> has shape (7,) which can be broadcast against the whole features <code>(6,7)</code></p>

<pre><code>features[0] &amp; features
</code></pre>

<p>This addresses the error, but not the question of how you want to compare <code>features[index]</code> with the rest.  I haven't fully digested that.  Show how <code>features[i]</code> should be compared with <code>features[j]</code>.</p>

python arrays numpy