ValueError:操作数无法与numpy中的不同形状一起广播?

时间:2015-08-29 12:41:57

标签: python numpy cross-validation

我正在尝试使用k-fold cross validation,为此我需要相应地使用训练集。我实现如下:

num_folds = 5
subset_size = num_training/num_folds
validation_accuracies = []

    for i in range(num_folds):
        Xcross_valid_set = X_train[i*subset_size:][:subset_size].shape
       # print X_train[:i*subset_size].shape,X_train[(i+1)*subset_size:]
        Xtrain_set = X_train[:i*subset_size] + X_train[(i+1)*subset_size:]
        #Xtrain_set=np.concatenate(X_train[:i*subset_size] , X_train[(i+1)*subset_size:])
        Ycross_valid_set=y_train[i*subset_size:][:subset_size]
        Ytrain_set=y_train[:i*subset_size]+y_train[(i+1)*subset_size:]

问题是,X_train[:i*subset_size]的形状(0,3072)X_train[(i+1)*subset_size:]的形状为(40000,3072),如果是i=0。我曾尝试使用numpy.concatenate但不起作用。

结果形状将为(40000,3072),因为第一项为0行。因此,如果第一项给出(10,3072),第二项给出(30,3072),那么结果形状将是(40,3072),即40行。如何将两个不同的形状合并为一个训练集?

1 个答案:

答案 0 :(得分:1)

np.concatenate需要作为第一个参数的数组列表:

Xtrain_set = np.concatenate([X_train[:i*subset_size], X_train[(i+1)*subset_size:]])