Search code examples
pythonlogistic-regressionone-hot-encoding

Transforming Dataframe for logistic regression (one hot encoding)


I'm trying to run a logistic regression in python. My data consists of both numeric and categorical data. I would like to use gender, age and food preference to predict if someone likes cats.

I'm thinking that I would need to do one hot encoding on Food_preference (see below) but not sure exactly how to do it. Could you please help? thanks!

Original dataframe

Name    Gender  Age Like_cats   Food_preference
John    M   30  Yes Apple
John    M   30  Yes Orange
John    M   30  Yes Steak
Amy F   20  No  Apple
Amy F   20  No  Grape

Desired dataframe

Name    Gender  Age Like_cats   Apple   Orange  Steak   Grape
John    M   30  Yes 1   1   1   0
Amy F   20  No  1   0   0   1

Solution

  • You can use LabelEncoder to transform your string features to numeric features.

    https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.LabelEncoder.html#sklearn.preprocessing.LabelEncoder

    Here's a working code with same data structure as yours:

    from sklearn.linear_model import LogisticRegression
    import pandas as pd
    from sklearn import preprocessing
    import numpy as np
    
    
    
    X = pd.DataFrame([['a', 0], ['b', 1], ['a', 5], ['b', 100]])
    y = [0, 1, 0, 1]
    
    X_n = [[]]*len(X.columns)
    
    i = 0
    for c in X.columns:
        if type(X[c].iloc[0]) == str: # if features are string encode them
            le = preprocessing.LabelEncoder()
            le.fit( list(set(X[c])) )
            X_n[i] = le.transform(X[c]) 
        else: # already numeric features
            X_n[i] = list(X[c])
        i += 1
    
    X_n = np.array(X_n).T # transposing to make rows as per sample feature
    print(X_n)
    
    clf = LogisticRegression(random_state=0).fit(X_n, y)