Пример #1
0
    test_set = 0  #define test_set
    #for each folds treat one fold as test set and 9 fols at train set
    for y in range(len(folds)):
        X_train = pd.DataFrame()
        #if not test-set append fold in the train set
        for x in range(len(folds)):
            if x == test_set:
                y_test = folds[x]['class'].values
                X_test = folds[x].drop(['class'], axis=1)
            else:
                X_train = X_train.append(folds[x])

        y_train = X_train['class'].values
        X_train = X_train.drop(['class'], axis=1)
        nb = NaiveBayes()  #initialize Naive Bayes Classifier
        nb.fit(X_train, y_train)  #train model with train data
        y_pred = nb.predict(X_test)  #test model with test set
        #find error with respect to zero-one loss function
        error = nb.zero_one_loss_function(y_test, y_pred)
        printstr = "\nAccuracy of 0-1 loss for fold {0} ::: {1}".format(
            y, (1 - error))
        print_both(file, printstr)
        accuracy_list.append((1 - error))
        #get mean square error
        acc, precision, recall = nb.confusion_matrix(y_test, y_pred)
        printstr = "\nCF for fold {0} ::: acc:: {1} :: precision:: {2} :: recall :: {3}".format(
            y, acc, precision, recall)
        print_both(file, printstr)
        CF_accuracy_list.append(acc)
        CF_precision_list.append(precision)
        CF_recall_list.append(recall)