1
$\begingroup$

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.

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

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()
$\endgroup$
0
$\begingroup$

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

$\endgroup$

Your Answer

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

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