在 CNN(Google Colab)中重塑图像时出错

Got error in Reshaping Images In CNN( Googel Colab)

我有一个数字识别的CNN项目。我正在 colab 中尝试。我有一个 784 像素的一维矢量,在将其传递给 CNN 之前,我必须将其整形为 (28x28x1)。但我无法重塑它。我得到一个错误。 这是我的代码-

 x_train = x_train.reshape(-1, 28, 28, 1)
    x_test = x_test.reshape(-1, 28, 28, 1)
    test_df=test_df.reshape(-1,28,28,1)

我得到了错误,它是 -

AttributeError                            Traceback (most recent call last)
<ipython-input-31-fc5920daac40> in <module>()
      9 # reshape(examples, height, width, channels)
     10 x_train = x_train.reshape(-1, 28, 28, 1)
---> 11 x_test = x_test.reshape(-1, 28, 28, 1)
     12 test_df=test_df.reshape(-1,28,28,1)

/usr/local/lib/python3.6/dist-packages/pandas/core/generic.py in __getattr__(self, name)
   5139             if self._info_axis._can_hold_identifiers_and_holds_name(name):
   5140                 return self[name]
-> 5141             return object.__getattribute__(self, name)
   5142 
   5143     def __setattr__(self, name: str, value) -> None:

AttributeError: 'DataFrame' object has no attribute 'reshape'

我读了 并且当我使用这段代码时-

x_train = x_train.values.reshape(-1, 28, 28, 1)
x_test = x_test.values.reshape(-1, 28, 28, 1)
test_df=test_df.values.reshape(-1,28,28,1)

我也遇到了错误,它是 -

AttributeError                            Traceback (most recent call last)
<ipython-input-32-93a2a0b4627a> in <module>()
      8 ##first param in reshape is number of examples. We can pass -1 here as we want numpy to figure that out by itself ###################
      9 # reshape(examples, height, width, channels)
---> 10 x_train = x_train.values.reshape(-1, 28, 28, 1)
     11 x_test = x_test.values.reshape(-1, 28, 28, 1)
     12 test_df=test_df.values.reshape(-1,28,28,1)

AttributeError: 'numpy.ndarray' object has no attribute 'values'

有时会出错,有时会运行。我不知道问题出在哪里,我该怎么办?

我的数据集来自 kaggle,数据集是 here

x_train 是一个 numpy 数组,而 x_test 是一个 pandas 数据帧。

看到在您的第一个版本中 x_train 重塑有效,而在第二个版本中失败。

x_test 转换为 numpy 数组而不是数据帧,您的第一个版本应该可以正常工作。