title: MAPE calculation in Python
body: <p>I want to calculate Mean Absolute percentage error (MAPE) of predicted and true values. I found a solution from <a href="https://stackoverflow.com/questions/42250958/how-to-optimize-mape-code-in-python">here</a>, but this gives error and shows invalid syntax in the line <code>mask = a &lt;&gt; 0</code></p>
  
  <pre><code>    def mape_vectorized_v2(a, b): 
      mask = a &lt;&gt; 0
      return (np.fabs(a - b)/a)[mask].mean() 
  
     def mape_vectorized_v2(a, b): 
         File "&lt;ipython-input-5-afa5c1162e83&gt;", line 1
           def mape_vectorized_v2(a, b):
                                         ^
       SyntaxError: unexpected EOF while parsing
  </code></pre>
  
  <p>I am using spyder3. My predicted value is a type np.array and true value is dataframe</p>
  
  <pre><code>type(predicted)
  Out[7]: numpy.ndarray
  type(y_test)
  Out[8]: pandas.core.frame.DataFrame
  </code></pre>
  
  <p>How do i clear this error and proceed with MAPE Calculation ? </p>
  
  <p>Edit : </p>
  
  <pre><code>predicted.head()
  Out[22]: 
     Total_kWh
  0   7.163627
  1   6.584960
  2   6.638057
  3   7.785487
  4   6.994427
  
  y_test.head()
  Out[23]: 
       Total_kWh
  79         7.2
  148        6.7
  143        6.7
  189        7.2
  17         6.4
  
  np.abs(y_test[['Total_kWh']] - predicted[['Total_kWh']]).head()
  Out[24]: 
     Total_kWh
  0        NaN
  1        NaN
  2        NaN
  3        NaN
  4   0.094427
  </code></pre>