我有一个线性回归,我想有一个拟合斜率,这将是在一个图形,其自变量将在日期范围内,请问我如何修改下面的代码,使日期将在以下范围内
2022-01- 30、2022 -02-14、2022-03-15,即范围将在每三天内。
这是我的密码
....
from sklearn.linear_model import LinearRegression
dict_data_confirm = {'2022-01-30': 50, '2022-01-31': 152, '2022-02-01': 41, '2022-02-02': 50,'2022-02-03': 50, '2022-02-04': 50,
'2022-02-05': 50, '2022-02-06': 50, '2022-02-07': 50, '2022-02-08': 50, '2022-02-09': 50, ......., '2022-09-30': 50, }
df = pd.Series(dict_data_confirm, name='Amount').rename_axis('Date').reset_index()
y=df.Amount
lr = LinearRegression()
datePd = pd.DataFrame({'time':df.Date, 'count':df.Amount})
x = pd.to_datetime(datePd.time)
lr.fit(x.values.reshape(-1,1), y)
plt.scatter(datePd.time, y, color='black')
lrValues = x.values.astype("float64").reshape(-1,1)
plt.plot(lrValues, lr.predict(lrValues), color='blue', linewidth=1)
plt.title('Purchase Data')
plt.ylabel('Amount')
plt.xlabel('Date')
plt.show()
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%y-%m-%d'))
# Change the tick interval
plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=15))
# Puts x-axis labels on an angle
plt.gca().xaxis.set_tick_params(rotation = 30)
plt.gca().set_xbound('2020-01-30', '2020-05-11')
误差输出
setattr(getattr(self.axes, lim_name), attr_name, (vmin, vmax))
File "C:\Users\my_system\AppData\Local\Programs\Python\Python38\lib\site-packages\matplotlib\transforms.py", line 990, in intervalx
self._points[:, 0] = interval
ValueError: setting an array element with a sequence. The requested array would exceed the maximum number of dimension of 1.
1条答案
按热度按时间juud5qan1#
使用
datetime
来训练线性回归模型可能不是最好的主意。相反,我们可以将datetime
转换为epoch timestamp用于模型训练和预测,但在绘图过程中,为了可读性,我们坚持使用datetime
。示例代码如下所示(注意:仅使用原始数据的开始部分)。