chiggywiggy
chiggywiggy

Reputation: 83

'DataFrame' object has no attribute 'feature_names'

import matplotlib.pyplot as plt
from sklearn.datasets import load_boston

%matplotlib inline

boston = load_boston()
print(boston.keys())

When I type this I get the output: dict_keys(['data', 'target', 'feature_names', 'DESCR', 'filename']) so I know that feature_names is an attribute. However, when I type

boston.columns = boston.feature_names

the ouput comes as 'DataFrame' object has no attribute 'feature_names'

Upvotes: 1

Views: 27075

Answers (2)

ThomasHaarlem
ThomasHaarlem

Reputation: 15

I had something similar. Also with scikitlearn to make a random forest with this tutorial: https://www.datacamp.com/tutorial/random-forests-classifier-python

I stumbled upon this line of code:

import pandas as pd
feature_imp = pd.Series(clf.feature_importances_,**index=iris.feature_names**).sort_values(ascending=False)
feature_imp

I got an error from the bold part (between the **). Thanks to the suggestions of #anky and #David Meu I tried:

feature_imp = pd.Series(clf.feature_importances_, index = dfNAN.columns.values).sort_values(ascending=False)

that results in the error:

ValueError: Length of values (4) does not match length of index (5)

so I tried:

feature_imp = pd.Series(clf.feature_importances_, index = dfNAN.columns.values[:4]).sort_values(ascending=False)

which works!

Upvotes: 0

Abhi_J
Abhi_J

Reputation: 2129

To convert boston sklearn dataset to pandas Dataframe use:

df = pd.DataFrame(boston.data,columns=boston.feature_names)
df['target'] = pd.Series(boston.target)

Upvotes: 3

Related Questions