朴素分类器使用字典的键遍历 df 的列?

Naive Classifier using keys of dict to iterate through the columns of df?

我目前正在创建我的朴素贝叶斯算法,我正在努力避免对所有名称进行硬编码。我想知道如何使用 dict 键遍历 df 的列而不实际提及列名,因为这会使算法对不同的数据集无用。谢谢!

train = pd.read_csv("train.csv")
test = {"feature_1":1, "feature_2":1, "feature_3":0, "feature_4":1}  
def naive_bayes_predict(train, test):  
    probs1= {}

    for keys in test:
        probs1[keys] = len(train[(train.target == 1) & (train.keys == test[keys])]) / len(train[train.target == 1])

要遍历数据框的列并仅使用 pandas,您可以使用以下内容:

for i in range(df.shape[1]):
    # df.iloc[:, i] would have the values of the ith feature with i starting from 0