【发布时间】:2021-02-26 17:37:32
【问题描述】:
我有一个数组,其中包含这样的汽车价格
| Date | Price($) |
| -------- | -------------- |
| 2019-09-01| NaN |
| 2019-09-02| NaN |
| 2019-09-03| 250 |
| 2019-09-04| 200 |
| 2019-09-05| 300 |
这里的问题是我想做一个线性回归来预测这辆车在下个月的价格(例如:2019-10-01 的汽车价格是...$)。但是当我尝试将输入拟合到线性回归模型时,我遇到了这个错误:ValueError: Input contains NaN, infinity or a value too large for dtype('float64'). 代码如下:
data = mydata #load my data
X = data.iloc[:, 0].values.reshape(-1, 1) # values converts it into a numpy array
Y = data.iloc[:, 1].values.reshape(-1, 1) # -1 means that calculate the dimension of rows, but have 1 column
linear_regressor = LinearRegression() # create object for the class
linear_regressor.fit(X, Y) # perform linear regression
Y_pred = linear_regressor.predict(X) # make predictions
【问题讨论】: