Python 循环,每次迭代都会将数据帧中的一列替换为零

Python 循环,每次迭代都会将数据帧中的一列替换为零,python,pandas,dataframe,loops,Python,Pandas,Dataframe,Loops,我想用Python对分类模型进行敏感性分析 因此,我想检查缺少每一列将如何影响度量。 我准备了从原始测试集中返回度量的函数 def score_metrics(model, X_test, y_test): y_pred = model.predict(X_test) #predicted values from oryginal dataset cm_orig = confusion_matrix(y_test, y

我想用Python对分类模型进行敏感性分析

因此,我想检查缺少每一列将如何影响度量。 我准备了从原始测试集中返回度量的函数

def score_metrics(model, 
                  X_test,
                  y_test):

y_pred = model.predict(X_test) #predicted values from oryginal dataset

cm_orig = confusion_matrix(y_test, y_pred)

tp = cm_orig[1, 1]
fp = cm_orig[0, 1]
fn = cm_orig[1, 0]
tn = cm_orig[0, 0]

score_orig_precision = precision_score(y_test, y_pred)
score_orig_accuracy = accuracy_score(y_test, y_pred)
score_orig_recall = recall_score(y_test, y_pred)
score_orig_specificity = tn/(tn+fp)
score_orig_F1 = f1_score(y_test, y_pred)

results = {'Feature': 'orginal',
           'Precision': score_orig_precision,
           'Accuracy': score_orig_accuracy,
           'Recall': score_orig_recall,
           'Specificity': score_orig_specificity,
           'F1 score': score_orig_F1}

return results
我希望执行相同的测试,但是对于X_测试,每次迭代都会有一列值替换为0

例如,如果这是X_测试:

    A   B   C   D   E
    5   7   11  12  6
   11   32  11  13  6
我想检查这些指标:

    A   B   C   D   E
    0   7  11  12   6
    0  32  11  13   6

    A   B   C   D   E
    5   0  11  12   6
   11   0  11  13   6

    A   B   C   D   E
    5   7   0   12  6
   11   32  0   13  6
等等。 我的问题是编辑上面的代码(或提出其他建议),帮助我实现它。
稍后,我希望在Pandas DataFrame中得到这个结果,但这足以让我看到字典状态。

因此,从您的示例来看,您实际上不想删除列,只需在迭代过程中给出0值。然后您可以使用:

for c in df.columns:
    newDF = df.copy(deep=True)
    newDF[c] = 0
    # Here you opperate with the new DF in this instance
在现有代码中对其进行整型的一个选项:

def getting_results(y_pred, y_test):
    cm_orig = confusion_matrix(y_test, y_pred)

    tp = cm_orig[1, 1]
    fp = cm_orig[0, 1]
    fn = cm_orig[1, 0]
    tn = cm_orig[0, 0]

    score_orig_precision = precision_score(y_test, y_pred)
    score_orig_accuracy = accuracy_score(y_test, y_pred)
    score_orig_recall = recall_score(y_test, y_pred)
    score_orig_specificity = tn/(tn+fp)
    score_orig_F1 = f1_score(y_test, y_pred)

    results = {'Feature': 'orginal',
               'Precision': score_orig_precision,
               'Accuracy': score_orig_accuracy,
               'Recall': score_orig_recall,
               'Specificity': score_orig_specificity,
               'F1 score': score_orig_F1}

    return results


def score_metrics(model, X_test, y_test):

    for c in X_test.columns:
        newX_test = X_test.copy(deep=True)
        newX_test[c] = 0
        
        y_pred = model.predict(newX_test) #predicted values from oryginal dataset
        getting_results(y_pred, y_test)

这回答了你的问题吗?谢谢,是的,我需要这个,但主要的问题是创建临时数据帧并对其进行操作,并在多次迭代中保存结果:)@Aly此解决方案已经在使用dataframe。这是你需要的吗?是的,谢谢。非常有用。我现在有一个问题,如何附加这个解决方案?在我的函数中,我将返回作为字典的度量。我应该在这个循环之前/之后添加什么来获取所有列的指标?我在dict中添加了
results={'Feature':列
,这很好,但它只显示了c在建模列中的最后结果
full\u output=[]:output=score\u sensitivity\u没有(model=acrylamide\u model,column=c,X\u test=X\u test,y\u test=y\u test)full\u output.append(output)
我已经解决了,非常感谢:)@Aly如何对其进行整型,取决于您,不过我给了您一个可能的方法