Logistic Regression cost function giving math domain error

def cost(X_train,Y_train,B):
    total_cost=0
    for i in range(len(X_train)):
        mtx=np.dot(B,X_train[i])
        hx=sigmoid(mtx)
        print(Y_train[i],hx)
        if(Y_train[i]==0):
            total_cost+=(1/len(X_train))*(-((1-Y_train[i])*math.log(1-hx)))
        elif(Y_train[i]==1):
            total_cost+=(1/len(X_train))*(-(Y_train[i]*math.log(hx)))
    return total_cost

This is the function I am using to calculate cost of logisitc regression. It keep giving this error:

<ipython-input-20-1e1d3dfb4ad0> in cost(X_train, Y_train, B)
      8             total_cost+=(1/len(X_train))*(-((1-Y_train[i])*math.log(1-hx)))
      9         elif(Y_train[i]==1):
---> 10             total_cost+=(1/len(X_train))*(-(Y_train[i]*math.log(hx)))
     11     return total_cost

ValueError: math domain error

This is the sigmoid function I am using:

def sigmoid(x) :
    return 1.0/(1+np.exp(-x))

I tried to use np.log instead of math.log but all it did was give me NaN. Can you help me figure out what is wrong with the cost function?