Binary Cross Entropy | Manual scalars I am wanting to make print statements &quot;showing my working out&quot; of Binary Cross Entropy loss function, that works with scalar inputs, not arrays.
import math as m
import numpy as np

y_ans = 1
y_pred = 1

print(&quot;Cost(y,y^) = -yln(y^) - (1-y)-y^ln(1-y^)&quot;)
print('Cost('+str(y_ans)+', '+str(y_pred)+') = -'+str(y_ans)+'ln('+str(y_pred)+') -('+str(1-y_ans)+')ln('+str(1-y_pred)+')')
#ans = round((-y_ans*m.log(y_pred)) - ((1-y_ans)*m.log(1-y_pred)), 2)
ans = round((-y_ans*np.log(y_pred)) - ((1-y_ans)*np.log(1-y_pred)), 2)
print('Cost('+str(y_ans)+', '+str(y_pred)+') = '+str(ans))

The answer should be 0, but I get NaN
Cost(y,y^) = -yln(y^) - (1-y)-y^ln(1-y^)
Cost(1, 1) = -1ln(1) -(0)ln(0)
/home/runner/MalwareAnalysis/BinaryCrossEntropy.py:10: RuntimeWarning: divide by zero encountered in log
  ans = round((-y_ans*np.log(y_pred)) - ((1-y_ans)*np.log(1-y_pred)), 2)
/home/runner/MalwareAnalysis/BinaryCrossEntropy.py:10: RuntimeWarning: invalid value encountered in double_scalars
  ans = round((-y_ans*np.log(y_pred)) - ((1-y_ans)*np.log(1-y_pred)), 2)
Cost(1, 1) = nan

print((-y_ans*np.log(y_pred)))
-0.0
print((1-y_ans)*np.log(1-y_pred))
nan

I'm fairly new to Python.
np.nan_to_num() does the trick, as @Oxbowerce pointed out.
import numpy as np
np.nan_to_num(np.log(1-y_pred))