2015-10-07 89 views
3

我试图预测使用SVR和GP的时间序列。 时间系列实际上是一个pandas.Seriespandas.DatetimeIndex作为索引。使用日期时间索引的Python中的时间序列预测

这两种算法都是在scikit-learn中实现的。而实际上SVR接受修改后X轴预测:

train_size = 100 
svr = GridSearchCV(SVR(kernel='rbf', gamma=0.001), cv=5, 
       param_grid={"C": [1e0, 1e1, 1e2, 1e3], 
          "gamma": np.logspace(-2, 2, 5)}) 

# Chose train_size items randomly in the data to predict 
data_train = ts.sample(n=train_size) 
data_train_y = data_train.values 
data_train_x = (data_train.index.values).reshape(train_size, 1) 

t0 = time.time() 
svr.fit(data_train_x, data_train_y) 
svr_fit = time.time() - t0 
print("SVR complexity and bandwidth selected and model fitted in %.3f s" 
    % svr_fit) 

sv_ratio = svr.best_estimator_.support_.shape[0]/train_size 
print("Support vector ratio: %.3f" % sv_ratio) 


min_date = min(ts.index.values) 
max_date = max(ts.index.values) + np.timedelta64('1','D') 

X_plot = pd.date_range(min_date, max_date, freq='H') 
X_plot = X_plot.to_series() 
X_plot = X_plot.reshape([len(X_plot),1]) 

t0 = time.time() 
y_svr = svr.predict(X_plot) 
svr_predict = time.time() - t0 
print("SVR prediction for %d inputs in %.3f s" 
    % (X_plot.shape[0], svr_predict)) 

高斯过程并不想对付它:

from sklearn import gaussian_process 
gp = gaussian_process.GaussianProcess(theta0=1e-2, thetaL=1e-4, thetaU=1e-1) 
gp.fit(data_train_x, data_train_y) 

它导致:

TypeError: ufunc add cannot use operands with types dtype('<M8[ns]') and dtype('<M8[ns]') 

回答

0

显然时间戳数据类型是不可接受的。他们应该转换为浮动。