title: Implementing zero mean and unit variance in numpy
body: <p>I am given a definition of a function and asked to implement it as follows:</p>
  
  <pre><code># Problem 1 - Apply zero mean and zero variance scale to the image features
  def normalize(data):
      pass
  </code></pre>
  
  <p>Then provided with a unit test using <code>numpy</code> that would assert the success of my implementation</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>This <strong>not</strong> my unit test, but assigned by the instructor of the course.</p>
  
  <pre><code>np.testing.assert_array_almost_equal(
      normalize(np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])),
      np.array([-0.4, -0.3, -0.2, -0.099, 0.0, 0.099, 0.199, 0.3, 0.4, 0.5]),
      decimal=3)
  </code></pre>
  
  <p>My solution is </p>
  
  <pre><code>def normalize(data):
      return ((data - data.mean()) / data.max() - data.min())
  </code></pre>
  
  <p>But there must be a gap in my understanding because I'm getting</p>
  
  <pre><code>AssertionError: 
  Arrays are not almost equal to 3 decimals
  
  (mismatch 100.0%)
   x: array([-1.45, -1.35, -1.25, -1.15, -1.05, -0.95, -0.85, -0.75, -0.65, -0.55])
   y: array([-0.4  , -0.3  , -0.2  , -0.099,  0.   ,  0.099,  0.199,  0.3  ,
          0.4  ,  0.5  ])
  </code></pre>
  
  <p>With googling I'm also finding</p>
  
  <pre><code>(data - data.mean()) / data.std()
  </code></pre>
  
  <p>After more searching I attempted</p>
  
  <pre><code>(data - data.mean()) / data.var()
  </code></pre>
  
  <p>But neither solution asserts correctly.</p>
  
  <p>So what's the correct implementation here?  </p>