我的data包含订单日期和销售总额。我有一个问题,xticks
显示X的POSIX时间,这对于人类是不可读的。您是否知道如何以可读方式绘制它?我必须转换为POSIX才能适应我的模型。
from sklearn import linear_model
def load_data():
df = pd.read_csv('data.csv', usecols=['created', 'total_gross'], parse_dates=['created'])
# The following line could be replaced with parse_dates=['created'] above
# df['created'] = pd.to_datetime(df.created)
return df.set_index('created').resample('D').sum().fillna(0)
df = load_data()
df.info()
# Create and Fit a Linear Regression Model
regr = linear_model.LinearRegression()
# convert to POSIX time by dividing by 10**9
X = df.index.astype('int64').values.reshape(-1,1) // 10**9
regr.fit(X, y)
y_predict = regr.predict(X)
plt.plot(X, y_predict)
plt.show()
答案 0 :(得分:0)
要使用POSIX时间和matplotlib,可以使用numpy
和datetime
import numpy as np
import datetime as dt
dateconv = np.vectorize(dt.datetime.fromtimestamp)
date = dateconv(X)
然后您可以将绘图例程更改为:
import matplotlib.dates as dates
fig,ax = plt.subplots()
ax.plot_date(date, y_predict)
xfmt = dates.DateFormatter('%Y-%m-%d %H:%M:%S')
ax.xaxis.set_major_formatter(xfmt)
fig.autofmt_xdate()
plt.show()
哪个重播:
我鼓励您使用matplotlib来绘制时间序列,而不是使用pandas df.plot()
,因为在尝试控制日期格式时,pandas时间戳和matplotlib会变得很奇怪。