ValueError:输入数组应具有与目标数组相同数量的样本。找到1个输入样本和416个目标样本

时间:2019-01-04 10:31:56

标签: python arrays numpy keras deep-learning

我正计划与CNN一起对角膜进行2类分类。这是代码

import random
import pandas as pd
from pandas import Series,DataFrame

import numpy as np
import matplotlib.pyplot as plt

import keras
from keras.datasets import fashion_mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout,Activation ,Flatten, Dropout, 
Conv2D, MaxPooling2D, Reshape
from keras.optimizers import RMSprop
from keras.optimizers import Adam

df_list = []
for i in range(4):    
    cl_data_set = pd.read_csv("./csv/cl_" + str(i) +".csv",sep=",",header=0)
    cl_x = DataFrame(cl_data_set.drop("POINT",axis=1))
    df_list.append(cl_x)
    op_data_set = pd.read_csv("./csv/op_" + str(i) +".csv",sep=",",header=0)
    op_x = DataFrame(op_data_set.drop("POINT",axis=1))
    df_list.append(op_x)

close_1 = np.array(df_list[0])
open_1 = np.array(df_list[1])
close_2 = np.array(df_list[2])
open_2 = np.array(df_list[3])
close_3 = np.array(df_list[4])
open_3 = np.array(df_list[5])
close_4 = np.array(df_list[6])
open_4 = np.array(df_list[7])
train_dataset = []
test_dataset = []
for i in range(0,13000,100):
    if i >= 10400:
        test_dataset.append([close_1[i:i+100,:],1])
        test_dataset.append([close_2[i:i+100,:],1])
        test_dataset.append([open_1[i:i+100,:],0])
        test_dataset.append([open_2[i:i+100,:],0])

    else:
        train_dataset.append([close_1[i:i+100,:],1])
        train_dataset.append([close_2[i:i+100,:],1])
        train_dataset.append([open_1[i:i+100,:],0])
        train_dataset.append([open_2[i:i+100,:],0])

random.shuffle(train_dataset)
random.shuffle(test_dataset)

x_train_list = []
y_train_list = []
x_test_list = []
y_test_list = []
for i in range(416):
        x_train_list.append(train_dataset[i][0])
        y_train_list.append(train_dataset[i][1])

for i in range(104):
        x_test_list.append(test_dataset[i][0])
        y_test_list.append(test_dataset[i][1])

x_train = np.array(x_train_list)
x_test = np.array(x_test_list)
y_train = np.array(y_train_list)
y_test = np.array(y_test_list)
model = Sequential()

x_train = np.reshape(x_train,(-1,416,100,4))
model.add(Conv2D(32, kernel_size=(3,3), activation='relu', input_shape=(416,100,4)))
model.add(MaxPooling2D(pool_size=(1,2)))

model.add(Dropout(0.4))
model.add(Flatten())
model.add(Dense(128, activation='sigmoid'))
model.add(Dropout(0.4))
model.add(Dense(1, activation='sigmoid'))

model.summary()
print("\n")
model.compile(loss='binary_crossentropy',optimizer="Adam",metrics=['accuracy'])



history = model.fit(x_train, y_train,batch_size=200,epochs=2000,verbose=1,validation_data=(x_test, y_test))


score = model.evaluate(x_test,y_test,verbose=1)
print("\n")
print("Test loss:",score[0])
print("Test accuracy:",score[1])

接下来,我将发布错误的完整列表     使用TensorFlow后端     x_train.shape     (416,100,4)     x_test.shape     (104,100,4)     y_train.shape     (416,)     y_test.shape     (104,)

Traceback (most recent call last):
  File "CNN.py", line 121, in <module>
history = model.fit(x_train, y_train,batch_size=200,epochs=2000,verbose=1,validation_data=(x_test, y_test))
  File "C:\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\training.py", line 950, in fit
batch_size=batch_size)
  File "C:\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\training.py", line 802, in _standardize_user_data
check_array_length_consistency(x, y, sample_weights)
  File "C:\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\training_utils.py", line 236, in check_array_length_consistency
'and ' + str(list(set_y)[0]) + ' target samples.')
ValueError: Input arrays should have the same number of samples as target arrays. Found 1 input samples and 416 target samples.

样本总数为520件,除以火车:test = 8:2, x具有4通道数据除以100点,y具有标签(0或1)。 请配合错误解决方案和必要的纠正措施

1 个答案:

答案 0 :(得分:0)

您需要将目标变量重塑为(416,1)和(104,1)。

y_train = np.array(y_train_list).reshape(-1,1)
# y_train.shape = (416,1)
y_test = np.array(y_test_list).reshape(-1,1)
# y_test.shape = (104,1)

一种简单的验证尺寸正确的方法是将np.array的形状推断为(rows, cols)(如果是二维的)。