Search code examples
pythonloopsdictionaryiterationnaivebayes

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


I am currently working on creating my naive bayes algorithm and I am trying to avoid hard coding all the names. I was wondering how I can use a dict key to iterate through the columns of the df without actually mentioning the column name as that would make the algorithm useless for different data sets. Thanks!

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])

Solution

  • For iterating through columns of dataframe and by just using pandas, you can use following:

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