预期的2D数组,在尝试反转缩放数据时获得了1D数组

时间:2018-06-17 23:38:29

标签: python python-3.x machine-learning scikit-learn

我尝试解决这个问题几个小时,当我试图反转缩放数据时,我无法这样做。

 In: print(yhat.shape), print(test_X[:, 0:].shape)
 Out:(1155, 1), (1155, 1, 37)

# invert scaling for forecast

inv_yhat=np.dstack((yhat, test_X[:, 0:])).shape
inv_yhat = scaler.inverse_transform(inv_yhat)
inv_yhat = inv_yhat[:,0]


---------------------------------------------------------------------------
ValueError: Traceback (most recent call last)
<ipython-input-334-779bdcd26d3e> in <module>()
      3 
      4 inv_yhat=np.dstack((yhat, test_X[:, 0:])).shape
----> 5 inv_yhat = scaler.inverse_transform(inv_yhat)
      6 inv_yhat = inv_yhat[:,0]

/anaconda3/lib/python3.6/site-packages/sklearn/preprocessing/data.py in inverse_transform(self, X)
    381         check_is_fitted(self, 'scale_')
    382 
--> 383         X = check_array(X, copy=self.copy, dtype=FLOAT_DTYPES)
    384 
    385         X -= self.min_

/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
    439                     "Reshape your data either using array.reshape(-1, 1) if "
    440                     "your data has a single feature or array.reshape(1, -1) "
--> 441                     "if it contains a single sample.".format(array))
    442             array = np.atleast_2d(array)
    443             # To ensure that array flags are maintained

ValueError: Expected 2D array, got 1D array instead:
array=[1.155e+03 1.000e+00 3.800e+01].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

我的数据列都是整数或浮点数(无分类)。另外,我删除了日期列。

我做错了什么?

1 个答案:

答案 0 :(得分:2)

答案就在你面前。您正在使用一维数组作为输入,但数据始终必须是scikit-learn中的2D:

  

如果您的数据有数据,请使用array.reshape(-1,1)重新整形数据   单一功能。

尝试类似:

inv_yhat = scaler.inverse_transform(inv_yhat.reshape(-1,1))