I want to know if it is possible to get the churn prediction probability at individual customer level & how by random forest algorithm rather than class level provided by: predict_proba(X) => Predict class probabilities for X.
I think the predict_proba(X)
is actually the correct method to accomplish your task. For each instance (person) the function will display the probability for each class label. So, if you know which class is churn, I'll just assume class 1, you just need to slice the results on that column. For example:
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
from tabulate import tabulate
X, y = make_classification(n_samples=1000, n_features=50,
n_informative=2, n_redundant=0,
random_state=1, shuffle=False)
clf = RandomForestClassifier(n_estimators=100, max_depth=2,
random_state=0)
clf.fit(X, y)
train_class_probability = clf.predict_proba(X)
print(tabulate(train_class_probability[0:4], ['Churn', 'No Churn']))
NewData, y = make_classification(n_samples=10, n_features=50,
n_informative=2, n_redundant=0,
random_state=1000, shuffle=False)
class_prediction = clf.predict(NewData)
class_probability = clf.predict_proba(NewData)
print(tabulate(class_prediction.reshape(-1, 1), ['0 - Churn / 1 - No-Churn']))
print(tabulate(class_probability, ['Churn', 'No Churn']))
# Just print the first column, Churn
print(tabulate(class_probability[:, 0].reshape(-1, 1), ['Probability of Churn']))
Output will be:
Class Probabilities of the Training Data (First four instances)
Churn No Churn
-------- ----------
0.595298 0.404702
0.620975 0.379025
0.601251 0.398749
0.610663 0.389337
Class Prediction on New Data
0 - Churn / 1 - No-Churn
--------------------------
0
0
0
1
1
1
1
0
1
1
Class Probability Prediction on New Data
Churn No Churn
-------- ----------
0.553698 0.446302
0.602109 0.397891
0.587715 0.412285
0.423982 0.576018
0.419588 0.580412
0.419984 0.580016
0.369798 0.630202
0.572373 0.427627
0.414734 0.585266
0.40422 0.59578
Probability of Churn on New Data
Probability of Churn
----------------------
0.553698
0.602109
0.587715 <- new customer #3
0.423982 <- new customer #4
0.419588
0.419984
0.369798
0.572373
0.414734
0.40422
So, the probability that "new customer #3" will churn is ~59% and the probability that "new customer #4" will churn is ~42%.
Hopefully, I have understood your question.
HTH