This may do what you want.

function mapeBase_v3(actuals::Vector{Float64}, forecasts::Vector{Float64})
# actuals - actual target values
# forecasts - forecasts (model estimations)

  sum_reldiffs = sumabs((x - y) / x for (x, y) in zip(actuals, forecasts) if x 
!= 0.0)  # Generator

  count_zeros = sum( map(x->(x==0.0), actuals) )
  count_nonzeros = length(actuals) - count_zeros
  sum_reldiffs, count_nonzeros
end




On Tuesday, October 25, 2016 at 3:15:54 AM UTC-4, Martin Florek wrote:
>
> Hi all,
> I'm new in Julia and I'm doing refactoring. I have the following function:
>
> function mapeBase_v1(A::Vector{Float64}, F::Vector{Float64})
>   s = 0.0
>   count = 0
>   for i in 1:length(A)
>     if(A[i] != 0.0)
>       s += abs( (A[i] - F[i]) / A[i])
>       count += 1
>     end
>   end
>
>   s, count 
>
> end   
>
> I'm looking for a simpler variant which is as follows:
>
> function mapeBase_v2(A::Vector{Float64}, F::Vector{Float64})
> # A - actual target values
> # F - forecasts (model estimations)
>
>   s = sumabs((x - y) / x for (x, y) in zip(A, F) if x != 0) # Generator
>
>   count = length(A) # ???
>   s, countend
>
>
> However with this variant can not determine the number of non-zero elements. 
> I found option with length(A[A .!= 0.0]), but it has a large allocation. 
> Please, someone knows a solution with generator, or variant v1 is very good 
> choice?
>
>
> Thanks in advance,
> Martin
>
>

Reply via email to