Search code examples
pythontensorflowkerasconv-neural-networkreshape

ValueError: cannot reshape array of size 3 into shape (1,80)


While fitting cnn model to my data, it caught error:

    161         X = X.reshape([X.shape[0], X.shape[1],1])
    162         X_train_1 = X[:,0:10080,:]
--> 163         X_train_2 = X[:,10080:10160,:].reshape(1,80)

ValueError: cannot reshape array of size 3 into shape (1,80)

The input data consists of X_train_1(each sample of shape 1, 10080) and X_train_2(each sample of shape 1, 80). X_train_1 and X_train_2 join to form a sample size of shape 1, 10160. What is the size 3 referring to?


Solution

  • try the following with the two different values for n:

    import numpy as np
    n = 10160
    #n = 10083
    X = np.arange(n).reshape(1,-1)
    np.shape(X)
    
    X = X.reshape([X.shape[0], X.shape[1],1])
    X_train_1 = X[:,0:10080,:]
    X_train_2 = X[:,10080:10160,:].reshape(1,80)
    np.shape(X_train_2)
    

    If you cannot make sure that X is 10160 long I suggest one of the following solutions:

    X_train_1 with 10080 samples, X_train_2 with the rest:

    X = X.reshape([X.shape[0], X.shape[1],1])
    X_train_1 = X[:,0:10080,:] # X_train_1 with 10080 samples
    X_train_2 = X[:,10080:,:].reshape(1,-1) # X_train_2 with the remaining samples
    

    Or X_train_2 with 80 samples, X_train_1 with the rest:

    X = X.reshape([X.shape[0], X.shape[1],1])
    X_train_1 = X[:,0:-80,:] # X_train_1 with the remaining samples
    X_train_2 = X[:,-80:,:].reshape(1,80) # X_train_2 with 80 samples