What is a good way to calculate probabilities in a dataset of samples? Each sample is a measurement, that is usually 1 or 0. The goal is to calculate probabilities based on all feature rows.

Simple example dataset:

    feature   label
    dog       1
    dog       0
    dog       0
    dog       0
    cat       1
    cat       0

Expected Output:

    feature  result
    dog      0.25
    cat      0.5

The real dataset has around 50 features.

  • $\begingroup$ For your dog label, would you expect 0.75 or 0.25? $\endgroup$
    – Oxbowerce
    Apr 1, 2020 at 19:41
  • $\begingroup$ Thanks, 0.25 is correct! $\endgroup$
    – hfdev
    Apr 2, 2020 at 5:19
  • $\begingroup$ See my answer, let me know if it works for you. $\endgroup$
    – Oxbowerce
    Apr 2, 2020 at 10:33

2 Answers 2


For the example you've could simply calculate the mean for each feature, however I am not sure if that is exactly what you want. If you have already loaded your data into a pandas dataframe this would be as simple as


Assume that X are features and y - labels

#Put algorithm as you wish 
from sklearn.ensemble import RandomForestClassifier
clf = RandomForestClassifier(max_depth=2, random_state=0)
clf.fit(X_train, y_train )

clf.predict_proba(X_test) # there you will get probabilities of the class


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.