Samrat Alam
Samrat Alam

Reputation: 567

Got error in Reshaping Images In CNN( Googel Colab)

I have a CNN project of digit recognization. I am trying it in colab. I have a 1D vector with 784 pixels and I have to reshape it to (28x28x1) before passing it to the CNN. But I can not reshape it. I got an error. This is my code-

 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)

and I got the error and it was -

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'

and I read this question and when i used this code-

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)

i got error also and it was -

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'

Sometimes it gives error and sometimes it runs. I dont know where is the problem and what should i do?

My dataset is taken form kaggle and Dataset is here.

Upvotes: 2

Views: 215

Answers (1)

Dinari
Dinari

Reputation: 2557

x_train is a numpy array, while x_test is a pandas dataframe.

See that in your first version the x_train reshape works, while fails in the second.

Convert x_test to a numpy array instead of a dataframe, and your first version should work fine.

Upvotes: 1

Related Questions