具有日期数据的Sklearn线性回归

时间:2019-01-24 17:48:22

标签: python numpy scikit-learn

在将日期数据输入到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代码。

1 个答案:

答案 0 :(得分:1)

您可以使用熊猫(pd.to_datetime())进行日期转换,如下所示:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

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'])
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])

df = pd.DataFrame({'time': data_time, 'count': data_count})
df.time = pd.to_datetime(df.time)

regr = linear_model.LinearRegression()
regr.fit(df.time.values.reshape(-1, 1), df['count'].reshape(-1, 1))

# Make predictions using the testing set
y_pred = regr.predict(df.time.values.astype(float).reshape(-1, 1))
df['pred'] = y_pred

ax = df.plot(x='time', y='count', color='black', style='.')
df.plot(x='time', y='pred', color='orange', linewidth=3, ax=ax, alpha=0.5)
ax.set_title('My Title')
ax.set_xlabel('Date')
ax.set_ylabel('Metric')

plt.show()

enter image description here