Tsaleem
Tsaleem

Reputation: 53

sklearn feature.Extraction 'DictVectorizer' object has no attribute 'feature_names_'

Whenever I call transform it keeps giving me the following error :

AttributeError:'DictVectorizer' object has no attribute 'feature_names_'

This is the function call:

vec = DictVectorizer()
x_test = vec.transform(X_features)

My python version is 2.7, Scipy 0.16.0, numpy 1.9.2+mkl, scikit-learn 0.16.1.

Upvotes: 5

Views: 7066

Answers (1)

Matt
Matt

Reputation: 17629

This means, that the DictVectorizer was not fitted prior to transforming X_features into it's corresponding matrix format.

You need to call vec.fit(X_features) followed by vec.transform(X_features), or more succintly X_test = vec.fit_transform(X_features). DictVectorizer needs to know the keys of all the passed dictionaries, so that the transformation of unseen data consistently yields the same number of columns and column order.

Upvotes: 8

Related Questions