Mehdi Mehdi - 1 year ago 95
Python Question

How to get number of dimensions in OneHotEncoder in Scikit-learn

I am using the

from Scikit-learn in my project. And I need to know what would be the size of each one-hot vector when the
is set to be
. I thought
would show that but it seems I have no way other than trying out training samples. I made this toy example code to show the problem. Do you know any other solution?

from sklearn.preprocessing import OneHotEncoder

data = [[1], [3], [5]] # 3 different features

encoder = OneHotEncoder()

print(len(encoder.transform([data[0]]).toarray()[0])) # 3 number of dimensions in one-hot-vector
print(encoder.n_values_) # [6] == len(range(5))

Answer Source

Is this what you are looking for?

>>> encoder.active_features_
array([1, 3, 5])

>>> len(encoder.active_features_)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download