Calculate probability in a dataset

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.

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

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

df.groupby(["feature"]).mean().reset_index()


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