What does 1. mean here?

here the author wrote cost = (1./m) * (-np.dot(Y,np.log(AL).T) - np.dot(1-Y, np.log(1-AL).T)) what does 1. do here instead of just 1 ?

1 Like

1. or 1.0 is a float and 1 is an integer. Python stuff. If your data is integer but your calculation includes decimal points, you will get error.

1 Like

Exactly. It used to be the case that python (back in the 2.x days) required you to be very careful about that. In python 2.x, if you wrote this:

m = 5
x = 1 / m

The answer would end up being integer 0. The reason is that both of the operands to / are integers, so the result is coerced to an integer also. I forget whether they did “floor” or “round”, but you get an integer result.

They fixed this in python 3.x by changing the rules. But a lot of people got into the habit of being safe by making sure that the constants you use are float, when that’s what you mean. So even in python 2.x, writing this will give 0.2:

m = 5.
x = 1. / m

Might as well be safe and write it in a way that works whatever version of python you are running.

1 Like