如何将X的预测插入BernoulliNB.predict_proba?

时间:2018-10-22 17:02:11

标签: python scikit-learn

我有一个Python脚本,给定一个观察到的类(X)和一些二进制列(Y),可以预测一个类(Pred_X)。然后,它预测每个类的概率(Prob(1)等)。我如何才能只获得观察到的类(Prob(X))的概率?

import pandas as pd
from sklearn.naive_bayes import BernoulliNB
BNB = BernoulliNB()

# Data
df_1 = pd.DataFrame({'X' : [1,2,1,1,1,2,1,2,2,1],
                     'Y1': [1,0,0,1,0,0,1,1,0,1],
                     'Y2': [0,0,1,0,0,1,0,0,1,0],
                     'Y3': [1,0,0,0,0,0,1,0,0,0]})

# Split the data
df_I = df_1 .loc[ : , ['Y1', 'Y2', 'Y3']]
S_O  = df_1['X']

# Bernoulli Naive Bayes Classifier
A_F = BNB.fit(df_I, S_O)

# Predict X
A_P = BNB.predict(df_I)
df_P = pd.DataFrame(A_P)
df_P.columns = ['Pred_X']

# Predict Probability
A_R = BNB.predict_proba(df_I)
df_R = pd.DataFrame(A_R)
df_R.columns = ['Prob_1', 'Prob_2']

# Join
df_1 = df_1.join(df_P)
df_1 = df_1.join(df_R)

1 个答案:

答案 0 :(得分:0)

感谢@jezrael:

# Rename the columns after the classes of X
classes = df_1['X'].unique()
df_R.columns = [classes]

# Look up the predicted probability of X
df_1['Prob_X'] = df_R.lookup(df_R.index, df_1.X)