def calc_acc(y_pred, y_test):
    """calculates accuracy between y_pred and y_test
    input: y_pred is a 1D array of length n_image
    input: y_test is a 2D array of shape n_image x 3
    output:  accuracy, float"""

    y_act = get_labels.get_label_1D(y_test)
    agree = np.where(y_pred == y_act)

    return len(agree[0]) / float(len(y_pred))
示例#2
0
def calc_acc(y_pred, y_test):
    '''calculates accuracy between y_pred and y_test
    input: y_pred is a 1D array of length n_image
    input: y_test is a 2D array of shape n_image x 3
    output:  accuracy, float'''

    y_act = get_labels.get_label_1D(y_test)
    agree = np.where(y_pred == y_act)

    return len(agree[0]) / float(len(y_pred))
    # An epoch is one pass through the whole training data set, batch size is number of data points used for each iteration of gradient descent
    NN_fit = model.fit(X_train_norm, y_train, 
          nb_epoch=20, 
          batch_size=150, 
          validation_data = (X_test_norm, y_test),
          show_accuracy = True, verbose=2)

    # Calculate metrics
    y_pred_train = model.predict_classes(X_train_norm)    # Predicted y_train classification
    print "Train Acc:"
    metrics.calc_acc(y_pred_train, y_train)
    y_pred_test = model.predict_classes(X_test_norm)      # Predicted y_test classification
    print "Train Acc:"
    calc_acc(y_pred_test, y_test)
    # Classification report
    y_act = get_labels.get_label_1D(y_test)
    print(classification_report(y_act, y_pred_test, target_names=['spiral', 'elliptical', 'uncertain']))
    # Plot confusion matrix
    cm = confusion_matrix(y_act, y_pred_test)
    plots.plot_confusion_matrix(cm, normed=True)
    # Get probabilities for each classification
    probas = model.predict_proba(X_test_norm)
    # Calculate true positive and false positive rates treating each classifier as binary
    sp_fpr, sp_tpr, _ = roc_curve(y_test20[:,0], probas[:,0])
    ell_fpr, ell_tpr, _ = roc_curve(y_test20[:,1], probas[:,1])
    unc_fpr, unc_tpr, _ = roc_curve(y_test20[:,2], probas[:,2])
    # Plot ROC curves
    plots.plot_ROC_curve(sp_tpr, sp_fpr, ell_tpr, ell_fpr, unc_tpr, unc_fpr)
    # Print AUC scores
    print "AUC for spirals:"
    roc_auc_score(y_test[:,0], probas[:,0])  
示例#4
0
                       batch_size=150,
                       validation_data=(X_test_norm, y_test),
                       show_accuracy=True,
                       verbose=2)

    # Calculate metrics
    y_pred_train = model.predict_classes(
        X_train_norm)  # Predicted y_train classification
    print "Train Acc:"
    metrics.calc_acc(y_pred_train, y_train)
    y_pred_test = model.predict_classes(
        X_test_norm)  # Predicted y_test classification
    print "Train Acc:"
    calc_acc(y_pred_test, y_test)
    # Classification report
    y_act = get_labels.get_label_1D(y_test)
    print(
        classification_report(
            y_act,
            y_pred_test,
            target_names=['spiral', 'elliptical', 'uncertain']))
    # Plot confusion matrix
    cm = confusion_matrix(y_act, y_pred_test)
    plots.plot_confusion_matrix(cm, normed=True)
    # Get probabilities for each classification
    probas = model.predict_proba(X_test_norm)
    # Calculate true positive and false positive rates treating each classifier as binary
    sp_fpr, sp_tpr, _ = roc_curve(y_test20[:, 0], probas[:, 0])
    ell_fpr, ell_tpr, _ = roc_curve(y_test20[:, 1], probas[:, 1])
    unc_fpr, unc_tpr, _ = roc_curve(y_test20[:, 2], probas[:, 2])
    # Plot ROC curves