0
votes

One of my features is a categorical variable that can take one 29 different states. I am trying to use one hot encoding to transform this so that I can build prediction models using this feature. The following is my code:

enc = preprocessing.OneHotEncoder()
enc.fit([[0], [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [20], [21], [22], [23], [24], [25], [26], [27], [28]]) 

subredditCategory = []
        if row[1] == 'Art':
            subredditCategory.append(0)
        elif row[1] == 'AskReddit':
            subredditCategory.append(1)
        elif row[1] == 'askscience':
            subredditCategory.append(2)
        elif row[1] == 'aww':
            subredditCategory.append(3)
        elif row[1] == 'books':
            subredditCategory.append(4)
        elif row[1] == 'creepy':
            subredditCategory.append(5)
        elif row[1] == 'dataisbeautiful':
            subredditCategory.append(6)
        elif row[1] == 'DIY':
            subredditCategory.append(7)
        elif row[1] == 'Documentaries':
            subredditCategory.append(8)
        elif row[1] == 'EarthPorn':
            subredditCategory.append(9)
        elif row[1] == 'explainlikeimfive':
            subredditCategory.append(10)
        elif row[1] == 'food':
            subredditCategory.append(11)
        elif row[1] == 'funny':
            subredditCategory.append(12)
        elif row[1] == 'gaming':
            subredditCategory.append(13)
        elif row[1] == 'gifs':
            subredditCategory.append(14)
        elif row[1] == 'history':
            subredditCategory.append(15)
        elif row[1] == 'jokes':
            subredditCategory.append(16)
        elif row[1] == 'LifeProTips':
            subredditCategory.append(17)
        elif row[1] == 'movies':
            subredditCategory.append(18)
        elif row[1] == 'music':
            subredditCategory.append(19)
        elif row[1] == 'pics':
            subredditCategory.append(20)
        elif row[1] == 'science':
            subredditCategory.append(21)
        elif row[1] == 'ShowerThoughts':
            subredditCategory.append(22)
        elif row[1] == 'space':
            subredditCategory.append(23)
        elif row[1] == 'sports':
            subredditCategory.append(24)
        elif row[1] == 'tifu':
            subredditCategory.append(25)
        elif row[1] == 'todayilearned':
            subredditCategory.append(26)
        elif row[1] == 'videos':
            subredditCategory.append(27)
        elif row[1] == 'worldnews':
            subredditCategory.append(28)

sub = enc.transform([subredditCategory]).toarray()

        features.append([row[2], row[3], row[6], row[8], sub])
        labels.append(row[9])

But when I then try and use the features and labels to train a model like so:

clf = tree.DecisionTreeClassifier()
clf = clf.fit(features, labels)

I get the following run time crash erro:

ValueError: setting an array element with a sequence.

That is generated the clf.fit line. Not sure what I am doing wrong - any thoughts?

1

1 Answers

2
votes

I believe as you have categorical data you need to also utilize the LabelBinarizer or the LabelEncoder.

You can use the LabelEncoder as follows:

encoder = sklearn.preprocessing.OneHotEncoder()
label_encoder = sklearn.preprocessing.LabelEncoder()
data_labels_encoded = label_encoder.fit_transform(data['category_feature'])
data['category_feature'] = data_label_encoded
feature = encoder.fit_transform(data[['category_feature']].as_matrix())

You can use the LabelBinarizer as follows:

lb = preprocessing.LabelBinarizer()
feature = lb.fit_transform(data['category_feature'])

I feel like the latter is a superior method, but that may be situation.