【问题标题】:Expected 2D array, got 1D array instead when attempting to invert scaled data预期 2D 数组,在尝试反转缩放数据时得到 1D 数组
【发布时间】:2018-11-26 19:35:41
【问题描述】:

我尝试了几个小时来解决这个问题,但当我尝试反转缩放数据时却无法解决。

 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)和带小数的数字(如 23.567)组成。
  • 请编辑您的问题以修复您的帖子。这比发表评论更有帮助,因为不是每个人都会通过 cmets 阅读
  • Alamoot,我不确定你的意思。我应该在主题中修正什么以使其更清晰?
  • 我在谈论您的评论:Sorry, I mean my data consists of integers (whole binary numbers like 1 or 0) and numbers with decimals (like 23.567) 点击问题底部的edit 按钮并修复问题。
  • 很遗憾,我的帖子没有编辑选项。不知道为什么。

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


【解决方案1】:

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

如果您的数据有 单一功能。

尝试类似:

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

【讨论】:

    猜你喜欢
    • 2020-11-05
    • 2018-09-16
    • 2020-12-18
    • 2018-12-11
    • 2020-11-02
    • 2019-06-06
    • 2021-05-14
    • 2021-12-28
    相关资源
    最近更新 更多