1
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'

12
  • try boston['feature_names'] Commented Mar 16, 2021 at 14:41
  • By the looks of the input , boston is a sklearn.utils.Bunch , try and create the df using df = pd.DataFrame(boston.data,columns=boston.feature_names) Commented Mar 16, 2021 at 14:42
  • Type in boston.columns.values to see an array of your column names Commented Mar 16, 2021 at 14:45
  • @DavidMeu ah it still says KeyError: 'feature_names' Commented Mar 16, 2021 at 14:45
  • @anky when I try that I get the ouput AttributeError: 'DataFrame' object has no attribute 'data' Commented Mar 16, 2021 at 14:45

2 Answers 2

3

To convert boston sklearn dataset to pandas Dataframe use:

df = pd.DataFrame(boston.data,columns=boston.feature_names)
df['target'] = pd.Series(boston.target)
Sign up to request clarification or add additional context in comments.

Comments

0

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!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.