Import required libraries


In [1]:
! sudo pip install matplotlib
%matplotlib inline
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from sklearn.externals import joblib


Requirement already satisfied (use --upgrade to upgrade): matplotlib in /usr/local/lib/python3.4/dist-packages
Requirement already satisfied (use --upgrade to upgrade): pytz in /root/.local/lib/python3.4/site-packages (from matplotlib)
Requirement already satisfied (use --upgrade to upgrade): numpy>=1.7.1 in /usr/lib/python3/dist-packages (from matplotlib)
Requirement already satisfied (use --upgrade to upgrade): pyparsing!=2.0.0,!=2.0.4,!=2.1.2,!=2.1.6,>=1.5.6 in /usr/local/lib/python3.4/dist-packages (from matplotlib)
Requirement already satisfied (use --upgrade to upgrade): cycler>=0.10 in /usr/local/lib/python3.4/dist-packages (from matplotlib)
Requirement already satisfied (use --upgrade to upgrade): six>=1.10 in /usr/local/lib/python3.4/dist-packages (from matplotlib)
Requirement already satisfied (use --upgrade to upgrade): python-dateutil in /usr/local/lib/python3.4/dist-packages (from matplotlib)
You are using pip version 8.1.2, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

Load persisted models (see modeling notebook)


In [2]:
models = joblib.load('models.pkl')

In [4]:
categories = ["spam", "OK", "vandalism", "attack"]

In [7]:
def plot_precision_recall_for_category(category):
    N = len(models)
    precision_values = [x["metrics_for_" + category][0] for x in models]
    recall_values = [x["metrics_for_" + category][1] for x in models]
    
    ind = np.arange(N)    # the x locations for the groups
    width = 0.35       # the width of the bars: can also be len(x) sequence

    p1 = plt.bar(ind, precision_values, width, color='#d62728')
    p2 = plt.bar(ind, recall_values, width,
             bottom=precision_values)

    plt.xlabel('Algorithms')
    plt.title('Precision and Recall per Model for ' + category)
    plt.xticks(ind, ([x["name"][0:4] for x in models]))
    plt.yticks(np.arange(0, 2, 0.1))
    plt.legend((p1[0], p2[0]), ('Precision', 'Recall'))

    plt.show()

In [ ]:
## Precision/Recall per category

In [8]:
for category in categories:
    plot_precision_recall_for_category(category)


Mean precision/recall


In [9]:
N = len(models)
precision_values = [x["mean_precision"] for x in models]
recall_values = [x["mean_recall"] for x in models]

In [10]:
ind = np.arange(N)    # the x locations for the groups
width = 0.35       # the width of the bars: can also be len(x) sequence

p1 = plt.bar(ind, precision_values, width, color='#d62728')
p2 = plt.bar(ind, recall_values, width,
             bottom=precision_values)

plt.xlabel('Algorithms')
plt.title('Precision and Recall per Model')
plt.xticks(ind, ([x["name"][0:4] for x in models]))
plt.yticks(np.arange(0, 1.7, 0.1))
plt.legend((p1[0], p2[0]), ('Precision', 'Recall'))

plt.show()



In [ ]: