我在将日期数据输入 sklearn 线性回归函数时遇到了一些麻烦。我知道我需要将日期数据转换为某种形式的序数,但我对 python 不够熟悉如何这样做! 这是我所拥有的:
import matplotlib.pyplot as plt
import numpy as np
from sklearn import linear_model
data_time = np.asarray(['2017-05-24','2017-05-25','2017-05-26','2017-05-27','2017-05-28','2017-05-29','2017-05-30','2017-05-31','2017-06-01','2017-06-02','2017-06-03','2017-06-04','2017-06-05','2017-06-06','2017-06-07','2017-06-08','2017-06-09','2017-06-10','2017-06-11','2017-06-12','2017-06-13','2017-06-14','2017-06-15','2017-06-16','2017-06-17','2017-06-18','2017-06-19','2017-06-20','2017-06-21']).reshape(-1, 1)
data_count = np.asarray([300.000,301.000,302.000,303.000,304.000,305.000,306.000,307.000,308.000,309.000,310.000,311.000,312.000,230.367,269.032,258.867,221.645,222.323,212.357,198.516,230.133,243.903,244.320,207.451,192.710,212.033,216.677,222.333,208.710]).reshape(-1, 1)
regr = linear_model.LinearRegression()
regr.fit(data_time, data_count)
# Make predictions using the testing set
y_pred = regr.predict(data_time)
plt.title('My Title')
plt.xlabel('Date')
plt.ylabel('Metric')
plt.scatter(data_time, data_count, color='black')
plt.plot(data_time, y_pred, color='orange', linewidth=3)
plt.show()
自然这会得到错误
ValueError: could not convert string to float: '2017-05-24'
任何帮助表示赞赏!旁注:如果可能的话,我不想偏离使用这种 numpy 数组格式,因为我已经编写了一个 C++ GUI 包装器,它在后台生成 python 代码。
相关分类