如何为CNN重塑我的数据? ValueError:无法将大小为267的数组重塑为形状(267,2)

时间:2020-05-23 15:58:57

标签: python input conv-neural-network

#Input 13 features
#Output Binary
# 297 data points    
x = x.iloc[:,[0,1,2,3,4,5,6,7,8,9,10,11,12]].values
y1= y['Target'}     

# Stratified K fold cross Validation
kf = StratifiedKFold(n_splits=10,random_state=None)

num_features=13
num_predictions=2

#Splitting data     
for train_index, test_index in kf.split(x,y1):

     x_train, x_test = x[train_index], x[test_index]
     y_train, y_test = y1[train_index], y1[test_index]

     # Standardization of data
     sc=StandardScaler(0,1)
     X_train = sc.fit_transform(x_train)
     X_test = sc.transform(x_test)

     print(X_train.shape) # o/p: (267,13)
     Print(y_train.shape) # o/p: (267)

     X_train = X_train.reshape((X_train.shape[0], X_train.shape[1], -1))
     X_test = X_test.reshape((X_test.shape[0], X_test.shape[1], -1))

     # Convert class vectors to binary class matrices.
     y_train = np.reshape(y_train, (y_train.shape[0], num_predictions))
     y_test = np.reshape(y_test, (y_test.shape[0], num_predictions))

     verbose, epochs, batch_size = 1, 10, 32

     n_timesteps, n_features, n_outputs = X_train.shape[1],X_train.shape[2],y_train.shape[1]

     model = Sequential()
     model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape (n_timesteps,n_features)))
     model.add(Conv1D(filters=64, kernel_size=3, activation='relu'))
     model.add(Dropout(0.5))
     model.add(MaxPooling1D(pool_size=2))
     model.add(Flatten())
     model.add(Dense(100, activation='relu'))
     model.add(Dense(297, activation='softmax'))
     model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
     # fit network
     model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size, verbose=verbose)
     # evaluate model
     accuracy = model.evaluate(X_test, y_test, batch_size=batch_size, verbose=0)
     print(accuracy)

我如何输入数据以输入需要3维数据的CNN。如何解决问题

ValueError:无法将大小为267的数组重塑为形状(267,2)。

1 个答案:

答案 0 :(得分:0)

想象一下,您有100个正方形的线,并且想要使其成为矩形。您可以将其设为2x100变成矩形吗?不,但是您可以将其设置为50x2。

简而言之,您不能使矩形的值大于原始矩形。