无法将sklearn的Bundle对象转换为Pandas系列对象

时间:2018-07-10 22:20:43

标签: python pandas numpy

这是我一直在努力的代码。

ThisWorkbook

错误描述如下

import numpy as np
import pandas as pd
from sklearn.datasets import load_boston
housing = load_boston()
housing = pd.DataFrame(housing.data, columns = housing.feature_names)
housing['target'] = pd.Series(housing.target)

AttributeError:“ DataFrame”对象没有属性“ target”

1 个答案:

答案 0 :(得分:1)

之所以会这样,是因为您将pd.DataFrame命名为与数据集完全相同,因此在housing.target中,housing引用了DataFrame而不是数据集。将其更改为类似

housing = load_boston()
housing_df = pd.DataFrame(housing.data, columns = housing.feature_names)
housing_df['target'] = pd.Series(housing.target)

你很好。