【发布时间】:2018-04-04 16:50:32
【问题描述】:
当我运行这段代码时:
from sklearn.tree import DecisionTreeRegressor
melbourne_model = DecisionTreeRegressor()
melbourne_model.fit(X, y)
我得到这个输出:
ValueError: Input contains NaN, infinity or a value too large for dtype('float32').
此错误指向显示melbourne_model.fit(X, y) 的行。
我希望代码与X 和y 匹配模型,这样我就可以根据我输入的一些变量(例如建造年份、土地面积、房间/卧室、位置等)对墨尔本的房屋进行未来预测。对现在我不能这样做,因为这个错误。
我认为这是因为 X 和 y 不是 NumPy 数组,当我使用 np.asarray() 并将我想要转换的内容放入 NumPy 数组时,它不起作用。我知道这一点,因为当我写type(X) 或type(y) 时,我得到pandas.core.series.Series。
我的文件的整个代码:
import pandas as pd
import numpy as np
melbourne_file_path = 'melb_data.csv\\melb_data.csv'
melbourne_data = pd.read_csv(melbourne_file_path)
np.asarray(melbourne_data.Price)
y = melbourne_data.Price
melbourne_predictors = ['Rooms', 'Bathroom', 'Landsize', 'BuildingArea',
'YearBuilt', 'Lattitude', 'Longtitude']
np.asarray(melbourne_data[melbourne_predictors])
X = melbourne_data[melbourne_predictors]
from sklearn.tree import DecisionTreeRegressor
melbourne_model = DecisionTreeRegressor()
melbourne_model.fit(X, y)
我正在使用 Jupyter Notebook 作为 Anaconda 的一部分。
我使用的 CSV 文件可以下载here。
下载文件夹后,您需要提取文件,并且 csv 在文件夹内。您可以根据文件所在的位置创建自己的melbourne_file_path。
【问题讨论】:
-
错误很明显。您的数据集中有
float('inf')或np.nan。不过可能是np.nan。检查melbourne_data.isnull().values.any() -
我得到的输出是真的,那么我该如何处理这个空数据呢?
-
另外,如果我有
float('inf'),这是什么意思,我该如何解决这个问题?
标签: python python-3.x pandas machine-learning data-science