Not getting correct accuracy in "Mushroom Classification - Prediction using Posterior Prob"

I wrote the same code and put up the same mushroom.csv file still I am getting an accuracy of 50%. Why so?

Hey,
You must be doing something wrong, may be in preprocessing of data, or while writing the algorithm.
Try to double check the code as shown in video,
If you still face the issue, send me the link for the code at cb.lk/ide

Thanks :slight_smile:

plz sir check my code https://ide.codingblocks.com/s/153500

Please add the complete code.
functions like prior, and conditional

I have added .

Hey,
your pred function should be like this, Correct your errors in the code…


def predict(x_train,y_train,x_test):
    """x_test is a single testing point ,n features"""
    #Compute Posterior_probability for each class
    n_features=x_train.shape[1]
    
    classes=np.unique(y_train)
    posterior_probability=[]#List of probabilities for all classes given a single testing point
    
    
    for label in classes:
    #posterior_probability=(likelihood*prior_probability)/marginal_probabilty
    #But we are not going to take marginal as we want argmax
        likelihood=1.0
        for f in range(n_features):
            cond_probability=conditional_probability(x_train,y_train,f,x_test[f],label)
            likelihood*=cond_probability
            
        prior=prior_probability(y_train,label)
        posterior=likelihood*prior
        posterior_probability.append(posterior)
    
    prediction=np.argmax(posterior_probability)
    
    return prediction

Thanks :slight_smile:

Got 99% accuracy.Thank you

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.