nolwww
nolwww

Reputation: 1725

Convert a single dictionary to array with a fitted OnehotEncoder

I have fitted a Onehotencoder on a pandas dataframe for some columns:

{ 'country', 'female', 'at_weekend' }

Now, I want to use this Onehotencoder on a single dictionary

{ 'country': 'US', 'female': True, 'at_weekend': True } 

The constraint is that I cannot use pandas to convert this dictionary. However, I can of course use Numpy, and Scikit learn.

Here is what I tried, which does not work:

object_dict = { 'country': 'US', 'female': True, 'at_weekend': True } 
a = np.array(object_dict)
b = one_hot_encoder.transform(a.reshape(1,-1))

I get this error

TypeError: unhashable type: 'dict'

Upvotes: 0

Views: 290

Answers (1)

Chris
Chris

Reputation: 29742

Extract the values from object_dict (in the trained order) then use transform:

import pandas as pd
import numpy as np

from sklearn.preprocessing import OneHotEncoder

df = pd.DataFrame({'country':['US', 'UK'], 'female': [True, False], 'at_weekend':[True,False]})

   at_weekend country  female
0        True      US    True
1       False      UK   False

ohe = OneHotEncoder(sparse=False)
ohe.fit(df)

object_dict = {'country': 'US', 'female': True, 'at_weekend': True} 
arr = np.array([object_dict[k] for k in df.columns], dtype=object)
ohe.transform(arr.reshape(1, -1))

Output:

array([[0., 1., 0., 1., 0., 1.]])

Upvotes: 1

Related Questions