即使有相同的d子集也会得到不同的结果

2024-04-26 07:06:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我试过寻找可能的类似问题,但到目前为止还没有找到。我的问题是:

我想使用交叉验证,使用KFold使用非重叠的数据子集。我所做的是使用KFold创建子集,并通过将randome_state设置为某个整数来修复结果。当我多次打印出子集时,结果看起来不错。然而,问题是,当我使用model.predict(意思是多次运行代码)在模型上多次使用相同的子集时,得到的结果会不同。当然,我怀疑我对培训模式的实施有问题。但我不知道是什么。我非常感谢你的提示。这是我的密码:

random.seed(42)
# define K-fold cross validation test harness
kf = KFold(n_splits=3, random_state=42, shuffle=True)

for train_index, test_index in kf.split(data):l
    print ('Train', train_index, '\nTest ', test_index)
    # create model
    testX= data[test_index]
    trainX = data[train_index]

    testYcheck = labels[test_index]
    testP = Path[test_index]
    # convert the labels from integers to vectors
    trainY = to_categorical(labels[train_index], num_classes=2)
    testY = to_categorical(labels[test_index], num_classes=2)

    # construct the image generator for data augmentation
    aug = ImageDataGenerator(rotation_range=30, width_shift_range=0.1,
        height_shift_range=0.1, shear_range=0.2, zoom_range=0.2,
        horizontal_flip=True, fill_mode="nearest")

    # train the network
    print("[INFO] training network...")
    model.fit_generator(aug.flow(trainX, trainY, batch_size=BS),
        validation_data=(testX, testY), 
        steps_per_epoch=len(trainX) // BS, epochs=EPOCHS, verbose=1)

    #predict the test data
    y_pred = model.predict(testX)

    predYl = []

    for element in range(len(y_pred)):
        if y_pred[element,1] > y_pred[element,0]:
            predYl.append(1)
        else:
            predYl.append(0)


    pred_Y= np.array(predYl)

    # Compute confusion matrix
    cnf_matrix = confusion_matrix(testYcheck, pred_Y)
    np.set_printoptions(precision=2)

    print (cnf_matrix)

Tags: thetestfordataindexlabelsmodelrange