1
$\begingroup$

I'm getting an assertion error for the test case: Y = np.array([0.,1.,0.,1.]), X = np.array([1.,1.,4.,4.])

The code executes when assert np.allclose(ce,0.,atol =1e-3)and doesn't pass when assert np.allclose(ce,1.,atol=1e-3)

Can you please correct the code to sort this out?

Code:

def conditional_entropy(Y,X):
    eps = np.finfo(float).eps
    attribute = X
    target_variables = np.unique(Y)
    variables = np.unique(X)
    entropy_attribute = 0

    for variable in variables:
        entropy_each_feature = 0
        for target_variable in target_variables:
            temp =0
            for i in range(len(X)):
                if(X[i]==variable and Y[i]==target_variable):
                    temp = temp+1
             num = temp
             print(temp)
             den = np.count_nonzero(Y==target_variable)
             print(den)
             fraction = num/(den+eps)
             entropy_each_feature += - fraction*(np.log(fraction+eps))
             print(entropy_each_feature)

    fraction2 = den/len(X)

    entropy_attribute += -fraction2*entropy_each_feature

    ce = entropy_attribute






    print(ce)
    return ce 
$\endgroup$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.