【问题标题】:Expected 2D array, got scalar array instead预期的二维数组,改为标量数组
【发布时间】:2019-07-29 04:50:18
【问题描述】:

我不知道为什么会出现错误,我尝试过重塑但没有运气。

感谢您的回答。

X_test = np.append(X_test, scaler.transform(working_data.iloc[-1][0]))

这是我收到的错误消息。

ValueError: Expected 2D array, got scalar array instead:
array=9583.994119999077.
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.

完整代码在这里:https://activewizards.com/blog/bitcoin-price-forecasting-with-deep-learning-algorithms/

非常感谢您的帮助。

【问题讨论】:

  • 大概,working_data.iloc[-1][0] 是一个标量,因此需要重塑。在scaler.transform 中尝试np.reshape(working_data.iloc[-1][0], (-1,1))

标签: python machine-learning


【解决方案1】:

这是在 Python 中使用数组时发生的常见错误。所以假设你有一个数组

a = [[1, 2, 3],
     [4, 5, 6],
     [7, 8, 9]]

这个数组的形状将是 (3,3)。现在,如果您访问a[-1,0],您将获得7,它没有形状,因为它是标量。如果你想把它附加到一个数组中说[[2, 3, 4]] 这将不起作用,因为后者是一个二维数组并且为了附加工作,你需要相同维度的数组。因此,如果您像这样调用 reshape:a[-1,0].reshape(-1,1),那么您将获得一个形状 (1,1) 的数组,而不是 7,您将获得 [[7]]。绕过这个的另一种方法是访问这样的值:a[-1,:1],这基本上与a[-1,0] 做同样的事情,但它表示您希望得到的数组是二维的数组。 另一件需要注意的事情是检查您尝试附加到的轴,因为我不知道 X_test 的形状,我不能说您要添加新行或新列,所以请记住这一点以后会导致错误的情况。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-20
    • 2020-05-11
    • 2019-08-17
    • 1970-01-01
    • 2018-06-06
    • 2021-03-01
    • 2019-10-07
    • 2019-07-30
    相关资源
    最近更新 更多