Пример #1
0
def number_pred(save, show):
    y_true = [600, 200, 200, 200, 200, 200, 200, 200, 500, 500, 500, 200, 200, 200, 200, 200, 200, 200, 200, 200]
    y_pred = [100, 200, 200, 100, 100, 200, 200, 200, 100, 200, 500, 100, 100, 100, 100, 100, 100, 100, 500, 200]
    cm = ConfusionMatrix(y_true, y_pred)

    # print(cm.binarize(100).P)
    # cm.enlarge(300)
    # cm.enlarge([300, 400])

    print(cm)

    cm.plot()
    filename = 'numbers.png'
    if save:
        plt.savefig(os.path.join(basepath, '..', 'screenshots', filename))
    if show:
        plt.show()

    # print("")

    # print(cm.classes)

    # print("")

    # cm.print_stats(None)
    cm.print_stats()
Пример #2
0
def size_pred(save, show):
    df = pd.DataFrame({
        'Height': [150, 150, 151, 151, 152, 155, 155, 157, 157, 157, 157, 158, 158, 159, 159, 159, 160, 160, 162, 162, 163, 164, 165, 168, 169, 169, 169, 170, 171, 171, 173, 173, 174, 176, 177, 177, 179, 179, 179, 179, 179, 181, 181, 182, 183, 184, 186, 190, 190],
        'Weight': [54, 55, 55, 47, 58, 53, 59, 60, 56, 55, 62, 56, 55, 55, 64, 61, 59, 59, 63, 66, 64, 62, 66, 66, 72, 65, 75, 71, 70, 70, 75, 65, 79, 78, 83, 75, 84, 78, 74, 75, 74, 90, 80, 81, 90, 81, 91, 87, 100],
        'Size': ['S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'XL', 'XL', 'XL', 'XL', 'XL', 'XL', 'XL', 'XL'],
        'SizePred': ['S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'L', 'M', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'XL', 'L', 'L', 'XL', 'L', 'XL', 'XL', 'XL'],
    })
    cm = ConfusionMatrix(df["Size"], df["SizePred"])
    print(cm)

    cm.print_stats()

    cm.plot()
    filename = 'size.png'
    if save:
        plt.savefig(os.path.join(basepath, '..', 'screenshots', filename))
    if show:
        plt.show()
def get_confusion_matrix(_results_file):
    df = pd.read_csv(results_file, sep='\t', header=None)
    true_lbls = df[1]
    pred_lbls = df[2]
    confusion_matrix = ConfusionMatrix(true_lbls, pred_lbls)
    confusion_matrix.plot()
    cm_file = _results_file.replace('.txt', '_cm.jpg')
    plt.savefig(cm_file)

    print()
    print(confusion_matrix)
    print()
    cm = confusion_matrix.to_dataframe()
    correct = 0
    for i in range(cm.shape[0]):
        correct += cm.iloc[i][i]
        recall = cm.iloc[i][i] * 100 / cm.sum(axis=0)[i]
        prec = cm.iloc[i][i] * 100 / cm.sum(axis=1)[i]
        print('Class %s recall = %.4f precision = %.4f' %
              (cm.columns[i], recall, prec))
    print('Overall accuracy = %.4f' %
          float(correct * 100 / sum(cm.sum(axis=0))))
def evaluateModel(pp, preds, fold):
    # ************ PROCESSING THE PREDICTIONS
    preds[preds >= 0.5] = 1
    preds[preds < 0.5] = 0
    print("F1 SCORE:")
    print(f1_score(pp.test_y, preds, average=None))
    print("Hamming Loss:")
    print(hamming_loss(pp.test_y, preds))
    print("Zero-one loss:")
    print(zero_one_loss(pp.test_y, preds))
    # I reach here in plus_one_hot_encode, I want to transform it in one hot
    y_test_preds = utils.from_plus_to_one_hot(np.array(pp.test_y))
    preds_transf = utils.from_plus_to_one_hot(np.array(preds))
    cm = ConfusionMatrix(
        np.array(y_test_preds).argmax(1),
        np.array(preds_transf).argmax(1))
    print(cm)
    ax = cm.plot()
    ax.set_xticklabels(classes, rotation="vertical")
    ax.set_yticklabels(classes)
    plt.savefig("cmpre{0}.png".format(fold))
Пример #5
0
  from pandas_confusion import ConfusionMatrix

  cm=ConfusionMatrix(y_test,pred)

  cm.plot(cmap=c.get_cmap('PuBu'))
def main(save, show):
    basepath = os.path.dirname(__file__)

    # y_true = [2, 0, 2, 2, 0, 1, 1, 2, 2, 0, 1, 2]
    # y_pred = [0, 0, 2, 1, 0, 2, 1, 0, 2, 0, 2, 2]
    # cm = ConfusionMatrix(y_true, y_pred)
    # cm = ConfusionMatrix(y_true, y_pred, labels=["ant", "bird", "cat"])

    # y_true = [2, 0, 2, 2, 0, 1]
    # y_pred = [0, 0, 2, 2, 0, 2]
    # cm = ConfusionMatrix(y_true, y_pred)
    # cm = ConfusionMatrix(y_true, y_pred, labels=["ant", "bird", "cat"])

    y_true = ['rabbit', 'cat', 'rabbit', 'rabbit', 'cat', 'dog', 'dog', 'rabbit', 'rabbit', 'cat', 'dog', 'rabbit']
    y_pred = ['cat', 'cat', 'rabbit', 'dog', 'cat', 'rabbit', 'dog', 'cat', 'rabbit', 'cat', 'rabbit', 'rabbit']
    cm = ConfusionMatrix(y_true, y_pred)

    # y_true = ["cat", "ant", "cat", "cat", "ant", "bird"]
    # y_pred = ["ant", "ant", "cat", "cat", "ant", "cat"]
    # >>> cm(y_true, y_pred, labels=["ant", "bird", "cat"])
    # array([[2, 0, 0],
    #       [0, 0, 1],
    #       [1, 0, 2]])
    # cm = ConfusionMatrix(y_true, y_pred)

    print("Confusion matrix:\n%s" % cm)
    df = cm.to_dataframe()
    print(df)
    print(df.dtypes)

    cm.plot()
    filename = 'cm.png'
    if save:
        plt.savefig(os.path.join(basepath, '..', 'screenshots', filename))
    if show:
        plt.show()

    cm.plot(normalized=True)
    filename = 'cm_norm.png'
    if save:
        plt.savefig(os.path.join(basepath, '..', 'screenshots', filename))
    if show:
        plt.show()

    cm.print_stats()
    print(cm.classification_report)

    print("sklearn confusion_matrix:\n%s" % confusion_matrix(y_true, y_pred))
    print(classification_report(y_true, y_pred))

    # stat = 'precision'
    # print(cm._avg_stat(stat))
    # print(cm.ACC)

    # import seaborn as sns
    # cm.plot(normalized=True, backend=Backend.Seaborn)
    # sns.plt.show()

    print("Binarize a confusion matrix")
    y_true = ["cat", "ant", "cat", "cat", "ant", "bird"]
    y_pred = ["ant", "ant", "cat", "cat", "ant", "cat"]
    cm = ConfusionMatrix(y_true, y_pred)
    print(cm)
    binary_cm = cm.binarize(['ant', 'cat'])
    # A bird is not a "land_animal"
    print(binary_cm)
Пример #7
0
def main(save, show):
    basepath = os.path.dirname(__file__)

    # y_true = [2, 0, 2, 2, 0, 1, 1, 2, 2, 0, 1, 2]
    # y_pred = [0, 0, 2, 1, 0, 2, 1, 0, 2, 0, 2, 2]
    # cm = ConfusionMatrix(y_true, y_pred)
    # cm = ConfusionMatrix(y_true, y_pred, labels=["ant", "bird", "cat"])

    # y_true = [2, 0, 2, 2, 0, 1]
    # y_pred = [0, 0, 2, 2, 0, 2]
    # cm = ConfusionMatrix(y_true, y_pred)
    # cm = ConfusionMatrix(y_true, y_pred, labels=["ant", "bird", "cat"])

    y_true = [
        'rabbit', 'cat', 'rabbit', 'rabbit', 'cat', 'dog', 'dog', 'rabbit',
        'rabbit', 'cat', 'dog', 'rabbit'
    ]
    y_pred = [
        'cat', 'cat', 'rabbit', 'dog', 'cat', 'rabbit', 'dog', 'cat', 'rabbit',
        'cat', 'rabbit', 'rabbit'
    ]
    cm = ConfusionMatrix(y_true, y_pred)

    # y_true = ["cat", "ant", "cat", "cat", "ant", "bird"]
    # y_pred = ["ant", "ant", "cat", "cat", "ant", "cat"]
    # >>> cm(y_true, y_pred, labels=["ant", "bird", "cat"])
    # array([[2, 0, 0],
    #       [0, 0, 1],
    #       [1, 0, 2]])
    # cm = ConfusionMatrix(y_true, y_pred)

    print("Confusion matrix:\n%s" % cm)
    df = cm.to_dataframe()
    print(df)
    print(df.dtypes)

    cm.plot()
    filename = 'cm.png'
    if save:
        plt.savefig(os.path.join(basepath, '..', 'screenshots', filename))
    if show:
        plt.show()

    cm.plot(normalized=True)
    filename = 'cm_norm.png'
    if save:
        plt.savefig(os.path.join(basepath, '..', 'screenshots', filename))
    if show:
        plt.show()

    cm.print_stats()
    print(cm.classification_report)

    print("sklearn confusion_matrix:\n%s" % confusion_matrix(y_true, y_pred))
    print(classification_report(y_true, y_pred))

    # stat = 'precision'
    # print(cm._avg_stat(stat))
    # print(cm.ACC)

    # import seaborn as sns
    # cm.plot(normalized=True, backend=Backend.Seaborn)
    # sns.plt.show()

    print("Binarize a confusion matrix")
    y_true = ["cat", "ant", "cat", "cat", "ant", "bird"]
    y_pred = ["ant", "ant", "cat", "cat", "ant", "cat"]
    cm = ConfusionMatrix(y_true, y_pred)
    print(cm)
    binary_cm = cm.binarize(['ant', 'cat'])
    # A bird is not a "land_animal"
    print(binary_cm)
plt.plot(c, cv_scores, '-o')
plt.xscale('log')


# In[ ]:

predicted = clf.predict(X_test)
expected = y_test
print(accuracy_score(expected, predicted))


# In[ ]:

predicted_probs = clf.predict_proba(X_test)
print(log_loss(y_test, predicted_probs))


# In[ ]:

cm = ConfusionMatrix(expected, predicted)
cm_stats = cm.to_dataframe().apply(lambda x: x/sum(x), axis=1)
cm_stats.to_csv('data/confusion_matrix_stats.csv')


# In[ ]:

mpl.rcParams['figure.figsize'] = (10.0, 5.0)
cm.plot(normalized=True)