Search code examples
pythonnumpymachine-learningfloating-pointlogistic-regression

Sigmoid going out of bound


def sigmoid(z):
# complete the code
z = np.asarray(z)
if z.ndim == 0:
    return(1/(1+np.exp(-z)))
else:
    d = np.array([])
    for item in z:
        d= np.append(d,1/(1+np.exp(-item)))
    return d

print(sigmoid([(1,3), (4,30)]))

why is this returning [ 0.73105858 0.95257413 0.98201379 1. ] as the function is bound from 0 to 1 for example [q= 1/1+np.exp(-30)][1] returns 1.0000000000000935

why is this happening and how to correct it? for example image of a weird looking output


Solution

  • Your sigmoid implementation looks fine.

    The reason print(1 / 1 + np.exp(-30)) returns 1.0000000000000935 is due to operator precedence.

    # Your example
    1 / 1 + np.exp(-30)
    
    # How it will be computed
    (1 / 1) + np.exp(-30)
    
    # What you actually wanted
    1 / (1 + np.exp(-30))
    

    P.S. numpy supports broadcasting. Your function can be simplified into:

    def sigmoid(z):
        z = np.asarray(z)
        return 1 / (1 + np.exp(-z))
    

    Use ravel function to flatten the multi-dimensional array, if it's what you want.