在python图形中显示日期范围

fslejnso  于 2023-03-24  发布在  Python
关注(0)|答案(1)|浏览(120)

我有一个线性回归,我想有一个拟合斜率,这将是在一个图形,其自变量将在日期范围内,请问我如何修改下面的代码,使日期将在以下范围内
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.
juud5qan

juud5qan1#

使用datetime来训练线性回归模型可能不是最好的主意。相反,我们可以将datetime转换为epoch timestamp用于模型训练和预测,但在绘图过程中,为了可读性,我们坚持使用datetime。示例代码如下所示(注意:仅使用原始数据的开始部分)。

from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
import pandas as pd

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}

# prepare data
df = pd.Series(dict_data_confirm, name='Amount').rename_axis('Date').reset_index()
df.Date = pd.to_datetime(df.Date)

# prepare linear regression model
lr = LinearRegression()
y= df.Amount
x = df.Date.apply(lambda x: x.timestamp())
lr.fit(x.to_numpy().reshape(-1, 1), y)

# plot
fig, ax = plt.subplots(1, 1, figsize=(10, 8))
ax.scatter(df.Date, y, color='black')
ax.plot(df.Date, lr.predict(x.to_numpy().reshape(-1, 1)), color='blue', linewidth=1)

# config x-axis
ax.set_xticks(df.Date)
ax.tick_params(axis='x', rotation=45)

# set labels
ax.set_title('Purchase Data')
ax.set_ylabel('Amount')
ax.set_xlabel('Date')

fig.tight_layout()
plt.show()

相关问题