Answers.Body: <p>I fixed the problem as follows:</p>
  
  <pre><code>import numpy as np
  
  def binarize(FV):
      return np.where(FV &gt; 0, 1, 0).astype(int)
  
  def Hamming_Distance(ref, query):
      b_rf, b_qu = binarize(ref), binarize(query)
      H = np.count_nonzero(b_qu[:, None, :] != b_rf, axis=2)
      return H
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-02-12T13:52:54.380Z
Answers.LastActivityDate: 2020-02-12T13:52:54.380Z
Answers.UserDisplayName: doplano
Answers.Body: <p>If you mean to binarize the vector such that all positive values become 1 and the rest becoming 0, then you can do this.</p>
  
  <pre><code>bin_arr = np.zeros_like(FV_R)
  bin_arr[FV_R &gt; 0] = 1.
  </code></pre>
  
  <p>As an example,</p>
  
  <pre><code>In [7]: arr                                                                                                                                                                                                        
  Out[7]: 
  array([[ 0.15, -0.52],
         [ 1.  , -0.43]])
  
  In [8]: bin_arr = np.zeros_like(arr)                                                                                                                                                                               
  
  In [9]: bin_arr[arr &gt; 0] = 1                                                                                                                                                                                       
  
  In [10]: bin_arr                                                                                                                                                                                                   
  Out[10]: 
  array([[1., 0.],
         [1., 0.]])
  <span class="math-container">```</span>
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-02-10T21:07:08.097Z
Answers.LastActivityDate: 2020-02-10T21:07:08.097Z
Answers.UserDisplayName: Gautam Sreekumar
Body: <p>Given a pre-trained CNN model, I extract feature vector of <em>3450</em> reference images <code>FV_R</code> as follows:</p>
  
  <pre><code>FV_R = [       [-8.2, -52.2, 9.07, -1.1, -0.08, -9.1, ........, -4.11], 
                 [7.8, -3.8, 6.4, -4.27, -2.2, -5.0, ............., 3.6], 
                 [-1.2, -0.8, 49.3, 1.73, -1.74, -7.1, ..........., 2.41],
                 [-1.2, -.8, 49.3, 0.6, -1.24, -1.04, .........., -2.06],
                 .
                 .
                 .
                 [-1.2, -.8, 49.3, 12.77. -2.2, -5.0, .........., -51.1]
         ]
  </code></pre>
  
  <p>and <code>FV_Q</code> for <em>1200</em> query images : </p>
  
  <pre><code>FV_Q = [       [-0.13, 2.6, -3.7, -0.5, -1.02, -0.6, ........, -0.11], 
                 [0.3, -3.8, 6.4, -1.6, -2.2, -5.0, ............., 0.97], 
                 [-6.4, -0.08, 8.0, 7.3, -8.07, -5.6, ..........., 0.01],
                 [-6.09, -.8, 0.5, -8.9, -0.74, -0.08, .........., -8.9],
                 .
                 .
                 .
                 [-1.2, -.8, 49.3, 12.77. -2.2, -5.0, .........., -51.1]
         ]
  </code></pre>
  
  <p>The size info:</p>
  
  <pre><code>&gt;&gt;&gt; FV_R.shape
  (3450, 64896)
  </code></pre>
  
  <p>Query images:</p>
  
  <pre><code>&gt;&gt;&gt; FV_Q.shape
  (1200, 64896)
  </code></pre>
  
  <p>I would like to <em>binarize</em> the CNN feature vectors (descriptors) and calculate Hamming Distance. I am already aware of <a href="https://stackoverflow.com/questions/32730202/fast-hamming-distance-computation-between-binary-numpy-arrays">this</a> answer to probably use <code>np.count_nonzero(a!=b)</code>(<code>if a.shape == b.shape</code>) but does anyone know a  method to binarize a feature vector with different size?</p>
  
  <p>Cheers,</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-06-02T22:16:37.490Z
Comments.Text: Programming issues are off-topic here. Please, read https://ai.stackexchange.com/help/on-topic for more details and to understand which questions are on-topic here. If you have a programming issue, the best place to ask your question is Stack Overflow or, alternatively, Data Science SE.
Comments.UserDisplayName: nbro
Tags: <convolutional-neural-networks><python><feature-selection>
Title: Binarize ConvNet Feature vector
UserDisplayName: doplano
ThreadText: # Binarize ConvNet Feature vector
  
  (Asked by: username_0 on 2020-02-10)
  
  Given a pre-trained CNN model, I extract feature vector of *3450* reference images `FV_R` as follows:
  
  ```
  FV_R = [       [-8.2, -52.2, 9.07, -1.1, -0.08, -9.1, ........, -4.11], 
                 [7.8, -3.8, 6.4, -4.27, -2.2, -5.0, ............., 3.6], 
                 [-1.2, -0.8, 49.3, 1.73, -1.74, -7.1, ..........., 2.41],
                 [-1.2, -.8, 49.3, 0.6, -1.24, -1.04, .........., -2.06],
                 .
                 .
                 .
                 [-1.2, -.8, 49.3, 12.77. -2.2, -5.0, .........., -51.1]
         ]
  
  ```
  
  and `FV_Q` for *1200* query images :
  
  ```
  FV_Q = [       [-0.13, 2.6, -3.7, -0.5, -1.02, -0.6, ........, -0.11], 
                 [0.3, -3.8, 6.4, -1.6, -2.2, -5.0, ............., 0.97], 
                 [-6.4, -0.08, 8.0, 7.3, -8.07, -5.6, ..........., 0.01],
                 [-6.09, -.8, 0.5, -8.9, -0.74, -0.08, .........., -8.9],
                 .
                 .
                 .
                 [-1.2, -.8, 49.3, 12.77. -2.2, -5.0, .........., -51.1]
         ]
  
  ```
  
  The size info:
  
  ```
  >>> FV_R.shape
  (3450, 64896)
  
  ```
  
  Query images:
  
  ```
  >>> FV_Q.shape
  (1200, 64896)
  
  ```
  
  I would like to *binarize* the CNN feature vectors (descriptors) and calculate Hamming Distance. I am already aware of [this](https://stackoverflow.com/questions/32730202/fast-hamming-distance-computation-between-binary-numpy-arrays) answer to probably use `np.count_nonzero(a!=b)`(`if a.shape == b.shape`) but does anyone know a method to binarize a feature vector with different size?
  
  Cheers,
  
  **Tags:** convolutional-neural-networks, python, feature-selection
  
  > **Comments:**
  > **username_1**: Programming issues are off-topic here. Please, read https://ai.stackexchange.com/help/on-topic for more details and to understand which questions are on-topic here. If you have a programming issue, the best place to ask your question is Stack Overflow or, alternatively, Data Science SE.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2020-02-12. Score: 0 | Accepted answer
  
  I fixed the problem as follows:
  
  ```
  import numpy as np
  
  def binarize(FV):
      return np.where(FV > 0, 1, 0).astype(int)
  
  def Hamming_Distance(ref, query):
      b_rf, b_qu = binarize(ref), binarize(query)
      H = np.count_nonzero(b_qu[:, None, :] != b_rf, axis=2)
      return H
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2020-02-10. Score: 0
  
  If you mean to binarize the vector such that all positive values become 1 and the rest becoming 0, then you can do this.
  
  ```
  bin_arr = np.zeros_like(FV_R)
  bin_arr[FV_R > 0] = 1.
  
  ```
  
  As an example,
  
  ```
  In [7]: arr                                                                                                                                                                                                        
  Out[7]: 
  array([[ 0.15, -0.52],
         [ 1.  , -0.43]])
  
  In [8]: bin_arr = np.zeros_like(arr)                                                                                                                                                                               
  
  In [9]: bin_arr[arr > 0] = 1                                                                                                                                                                                       
  
  In [10]: bin_arr                                                                                                                                                                                                   
  Out[10]: 
  array([[1., 0.],
         [1., 0.]])
  ```
  
  ```
  
  
  ---
  
  