dnn回归预测,skflow回归预测多个值

I'm trying to forecast a time series: given 50 previous values, I want to predict the 5 next values.

To do so, I'm using the skflow package (based on TensorFlow), and this problem is relatively close to the Boston example provided in the Github repo.

My code is as follows :

%matplotlib inline

import pandas as pd

import skflow

from sklearn import cross_validation, metrics

from sklearn import preprocessing

filepath = 'CSV/FILE.csv'

ts = pd.Series.from_csv(filepath)

nprev = 50

deltasuiv = 5

def load_data(data, n_prev = nprev, delta_suiv=deltasuiv):

docX, docY = [], []

for i in range(len(data)-n_prev-delta_suiv):

docX.append(np.array(data[i:i+n_prev]))

docY.append(np.array(data[i+n_prev:i+n_prev+delta_suiv]))

alsX = np.array(docX)

alsY = np.array(docY)

return alsX, alsY

X, y = load_data(ts.values)

# Scale data to 0 mean and unit std dev.

scaler = preprocessing.StandardScaler()

X = scaler.fit_transform(X)

X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y,

test_size=0.2, random_state=42)

regressor = skflow.TensorFlowDNNRegressor(hidden_units=[30, 50],

steps=5000, learning_rate=0.1, batch_size=1)

regressor.fit(X_train, y_train)

score = metrics.mean_squared_error(regressor.predict(X_test), y_test)

print('MSE: {0:f}'.format(score))

This leads to :

ValueError: y_true and y_pred have different number of output (1!=5)

at the end of the training.

And when I try to predict, I have the same kind of problem

ypred = regressor.predict(X_test)

print ypred.shape, y_test.shape

(200, 1) (200, 5)

We can therefore see that the model is somehow predicting only 1 value instead of the 5 wanted/hoped.

How could I use the same model to predict values for several values ?

解决方案

I've just added support for multi-output regression into skflow since this #e443c734, so please reinstall the package are try again. If it doesn't work, please follow up on Github.

I also added an example of multioutput regression to examples folder:

# Create random dataset.

rng = np.random.RandomState(1)

X = np.sort(200 * rng.rand(100, 1) - 100, axis=0)

y = np.array([np.pi * np.sin(X).ravel(), np.pi * np.cos(X).ravel()]).T

# Fit regression DNN model.

regressor = skflow.TensorFlowDNNRegressor(hidden_units=[5, 5])

regressor.fit(X, y)

score = mean_squared_error(regressor.predict(X), y)

print("Mean Squared Error: {0:f}".format(score))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值