我正在尝试预测时间序列:给定50个先前的值,我想预测下5个值.
为此,我正在使用该skflow
包(基于TensorFlow),这个问题与Github repo中提供的Boston示例相对接近.
我的代码如下:
%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))
这导致 :
ValueError:y_true和y_pred具有不同的输出数(1!= 5)
在培训结束时.
当我试图预测时,我遇到了同样的问题
ypred = regressor.predict(X_test) print ypred.shape, y_test.shape
(200,1)(200,5)
因此,我们可以看到该模型以某种方式仅预测1个值而不是5个想要/希望的值.
我如何使用相同的模型来预测多个值的值?
我刚刚加入多输出回归到支持skflow
,因为这#e443c734,所以请重新安装包被重试.如果它不起作用,请跟进Github.
我还在examples文件夹中添加了多输出回归的示例:
# 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))