ee8291
ee8291

Reputation: 555

Expected 2D array, got 1D array instead when attempting to invert scaled data

I tried solving this for hours and am not able to do so when I am attempting to invert scaled data.

 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.

My data column are all either integer or floats (no categorical). Also, I dropped the date column.

What am I doing wrong?

Upvotes: 1

Views: 3828

Answers (1)

Stev
Stev

Reputation: 1140

The answer is right in front of you. You are using a 1D array as an input but the data always has to be 2D in scikit-learn:

Reshape your data either using array.reshape(-1, 1) if your data has a single feature.

Try something like:

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

Upvotes: 3

Related Questions