如何从 Sklearn 逻辑预测中提取行特定信息
How to Extract Rows Specific info from Sklearn Logistic Predictions
我有一个逻辑回归来预测哪些客户会流失。我似乎无法在任何地方找到提取预计会流失的帐户的代码。 Account Name 是一个字符串对象,所以我没有将它输入逻辑模型,但我需要将预测的流失行映射回原始 table
这是我的数据的样子,但是我无法在较小的样本量中复制这个问题:
import random
random_data = [['ABC', 'yes'],['AAA','yes'],
['BBB','no'],['XTZ','no'],['ADB','no']]
df = pd.DataFrame(random_data,columns=['Account','Target'])
df['height'] = random.sample(xrange(10), len(df))
df['weight'] = random.sample(xrange(10), len(df))
X_train_pd = df.drop(['Account','Target'], axis=1)
y_train_pd = df['Target']
logreg = LogisticRegression()
logreg.fit(X_train_pd, y_train_pd)
y_pred_train = logreg.predict(X_train_pd)
这是我试过的方法。它的 Hacky 和 Bug 如下所示
"Extract Account Names Predicted to Churn"
y_pred_prob_df = pd.DataFrame(logreg.predict_proba(X_test))
data = np.array([y_test_pd, y_pred_test ])
data_y = pd.DataFrame({'y_test':data[0],'y_pred_test':data[1]} )
ID = test[['Account Name', 'Status']]
Accounts=pd.concat([ID, data_y, y_pred_prob_df], axis=1)
这是错误:当我连接实际 y、预测 y、概率、原始数据集 (ID) 时,我得到了额外的几行。
如果我取出 ID,这将解决该错误。
print ID.shape #(250, 2)
print data_y.shape #(250, 2)
print y_pred_prob_df.shape #(250, 2)
print Accounts.shape, "(267, 6) <-- BUG "
s=pd.concat([data_y, y_pred_prob_df], axis=1)
print s.shape, "(250, 4) <-- Resolves BUG: ID is the issue"
Hacky 方法不起作用...我们只想提取预计会流失的帐户
我正在寻找的结果是一个数据框,其中包含我的所有特征、目标、预测流失率和预测概率。具体来说,Account Name 'ABC' 是否预计会流失?可能是那个预测?以及进入模型的所有字段
Seems like I can't use loc to find only the accounts predicted to churn
要获取预计会流失的帐户,您只需编写:
df.loc[y_pred_train == "yes"]
并获得概率:
y_pred_prob_df.loc[y_pred_train == "yes"]
我有一个逻辑回归来预测哪些客户会流失。我似乎无法在任何地方找到提取预计会流失的帐户的代码。 Account Name 是一个字符串对象,所以我没有将它输入逻辑模型,但我需要将预测的流失行映射回原始 table
这是我的数据的样子,但是我无法在较小的样本量中复制这个问题:
import random
random_data = [['ABC', 'yes'],['AAA','yes'],
['BBB','no'],['XTZ','no'],['ADB','no']]
df = pd.DataFrame(random_data,columns=['Account','Target'])
df['height'] = random.sample(xrange(10), len(df))
df['weight'] = random.sample(xrange(10), len(df))
X_train_pd = df.drop(['Account','Target'], axis=1)
y_train_pd = df['Target']
logreg = LogisticRegression()
logreg.fit(X_train_pd, y_train_pd)
y_pred_train = logreg.predict(X_train_pd)
这是我试过的方法。它的 Hacky 和 Bug 如下所示 "Extract Account Names Predicted to Churn"
y_pred_prob_df = pd.DataFrame(logreg.predict_proba(X_test))
data = np.array([y_test_pd, y_pred_test ])
data_y = pd.DataFrame({'y_test':data[0],'y_pred_test':data[1]} )
ID = test[['Account Name', 'Status']]
Accounts=pd.concat([ID, data_y, y_pred_prob_df], axis=1)
这是错误:当我连接实际 y、预测 y、概率、原始数据集 (ID) 时,我得到了额外的几行。 如果我取出 ID,这将解决该错误。
print ID.shape #(250, 2)
print data_y.shape #(250, 2)
print y_pred_prob_df.shape #(250, 2)
print Accounts.shape, "(267, 6) <-- BUG "
s=pd.concat([data_y, y_pred_prob_df], axis=1)
print s.shape, "(250, 4) <-- Resolves BUG: ID is the issue"
Hacky 方法不起作用...我们只想提取预计会流失的帐户
我正在寻找的结果是一个数据框,其中包含我的所有特征、目标、预测流失率和预测概率。具体来说,Account Name 'ABC' 是否预计会流失?可能是那个预测?以及进入模型的所有字段
Seems like I can't use loc to find only the accounts predicted to churn
要获取预计会流失的帐户,您只需编写:
df.loc[y_pred_train == "yes"]
并获得概率:
y_pred_prob_df.loc[y_pred_train == "yes"]