Esempio n. 1
0
def p1_plots(q, dataSet):
    fin = open("{}.pkl".format(dataSet), "rb")
    train, test = pickle.load(fin)
    X_tr, y_tr = train
    X_te, y_te = test
    X_te_ones = np.array([X_te[i] for i in range(len(X_te)) if y_te[i] == 1])
    X_te_nils = np.array([X_te[i] for i in range(len(X_te)) if y_te[i] == 0])
    X_tr_ones = np.array([X_tr[i] for i in range(len(X_tr)) if y_tr[i] == 1])
    X_tr_nils = np.array([X_tr[i] for i in range(len(X_tr)) if y_tr[i] == 0])

    k = []
    if (q == 1):
        k = [1, 2, 3, 4, 5]
    elif (q == 2):
        k = [1, 2, 3, 4, None]
    elif (q == 3):
        k = ["linear", "rbf", "poly"]

    for i in k:
        fig = plt.figure()
        ax = fig.add_subplot(111)
        if (q == 1):
            ax.set_title("n_neighbors = %d" % (i))
        elif (q == 2):
            if (i is not None):
                ax.set_title("max_depth = %d" % (i))
            else:
                ax.set_title("max_depth = None")
        elif (q == 3):
            ax.set_title("kernel = %s" % (i))
        ax.scatter(X_tr_ones[:, 0], X_tr_ones[:, 1], c='b')
        ax.scatter(X_tr_nils[:, 0], X_tr_nils[:, 1], c='r')
        ax.scatter(X_te_ones[:, 0], X_te_ones[:, 1], c='b', marker="*")
        ax.scatter(X_te_nils[:, 0], X_te_nils[:, 1], c='r', marker="*")
        if (q == 1):
            clf = KNeighborsClassifier(n_neighbors=i)
        elif (q == 2):
            clf = DecisionTreeClassifier(criterion='entropy', max_depth=i)
        elif (q == 3):
            clf = SVC(kernel=i)
        clf.fit(X_tr, y_tr)
        y_tr_pred = clf.predict(X_tr)
        y_te_pred = clf.predict(X_te)
        tr_loss = len(
            [y_tr[i]
             for i in range(len(y_tr)) if y_tr[i] != y_tr_pred[i]]) / len(y_tr)
        te_loss = len(
            [y_te[i]
             for i in range(len(y_te)) if y_te[i] != y_te_pred[i]]) / len(y_te)
        ax.set_xlabel("Tr Loss: {} \n Te Loss: {}".format(tr_loss, te_loss))
        x_min, x_max, y_min, y_max = hlp.get_bounds(X_tr)
        hlp.plot_decision_boundary(ax,
                                   clf,
                                   x_min=x_min,
                                   x_max=x_max,
                                   y_min=y_min,
                                   y_max=y_max)
        #plt.savefig("knn_plot_n%d.pdf" % (n))
        # or if that takes too long: plot_decision_boundary(ax, clf, res=.1)
    plt.show()
Esempio n. 2
0
def decision_tree(pickle_file):

    fin = open(pickle_file, "rb")
    train, test = pickle.load(fin)
    X_tr, y_tr = train
    X_te, y_te = test

    n = [1,2,3,4,0]

    figure = plt.figure()
    for spot in range(len(n)):
        if n[spot] != 0:
            dtc = DecisionTreeClassifier(criterion = "entropy", max_depth=n[spot])
        else:
            dtc = DecisionTreeClassifier(criterion = "entropy")
        dtc.fit(X_tr, y_tr)
        predicted_tr = (dtc.predict(X_tr))
        predicted_te = (dtc.predict(X_te))

        axis = figure.add_subplot(1,5,spot+1)

        xtr_1 = []
        xtr_2 = []

        for pair in X_tr:
            xtr_1.append(pair[0])
            xtr_2.append(pair[1])

        xte_1 = []
        xte_2 = []

        for pair in X_te:
            xte_1.append(pair[0])
            xte_2.append(pair[1])

        colors = ListedColormap(['#FF0000', '#0000FF'])

        axis.scatter(xtr_1,xtr_2, c = y_tr, cmap = colors, edgecolors = 'k')
        axis.scatter(xte_1,xte_2, marker="*", c = y_te, cmap = colors, edgecolors = 'k')
        x1min, x1max, x2min, x2max = helpers.get_bounds(X_tr)
        helpers.plot_decision_boundary(axis, dtc, x1min, x1max, x2min, x2max)

        if n[spot] != 0:
            axis.set_title("max_depth = " + str(n[spot]))
        else:
            axis.set_title("max_depth = none")

        tr_loss = round(zero_one_loss(y_tr,predicted_tr),2)
        te_loss = round(zero_one_loss(y_te,predicted_te),2)

        axis.set_xlabel("Tr loss: " + str(tr_loss)+"\n Te loss: " + str(te_loss))

    plt.show()
Esempio n. 3
0
def svm(pickle_file):

    fin = open(pickle_file, "rb")
    train, test = pickle.load(fin)
    X_tr, y_tr = train
    X_te, y_te = test

    n = ["linear","rbf","poly"]

    figure = plt.figure()
    for spot in range(len(n)):
        svc = SVC(kernel = n[spot])
        svc.fit(X_tr, y_tr)
        predicted_tr = (svc.predict(X_tr))
        predicted_te = (svc.predict(X_te))

        axis = figure.add_subplot(1,5,spot+1)

        xtr_1 = []
        xtr_2 = []

        for pair in X_tr:
            xtr_1.append(pair[0])
            xtr_2.append(pair[1])

        xte_1 = []
        xte_2 = []

        for pair in X_te:
            xte_1.append(pair[0])
            xte_2.append(pair[1])

        colors = ListedColormap(['#FF0000', '#0000FF'])

        axis.scatter(xtr_1,xtr_2, c = y_tr, cmap = colors, edgecolors = 'k')
        axis.scatter(xte_1,xte_2, marker="*", c = y_te, cmap = colors, edgecolors = 'k')
        x1min, x1max, x2min, x2max = helpers.get_bounds(X_tr)
        helpers.plot_decision_boundary(axis, svc, x1min, x1max, x2min, x2max)

        if n[spot] != 0:
            axis.set_title("kernel = " + str(n[spot]))
        else:
            axis.set_title("kernel = none")

        tr_loss = round(zero_one_loss(y_tr,predicted_tr),2)
        te_loss = round(zero_one_loss(y_te,predicted_te),2)

        axis.set_xlabel("Tr loss: " + str(tr_loss)+"\n Te loss: " + str(te_loss))

    plt.show()
Esempio n. 4
0
def kNN(pickle_file):

    fin = open(pickle_file, "rb")
    train, test = pickle.load(fin)
    X_tr, y_tr = train
    X_te, y_te = test

    n = [1,3,5,7,9]

    figure = plt.figure()
    for spot in range(len(n)):
        knc = KNeighborsClassifier(n_neighbors = n[spot])
        knc.fit(X_tr, y_tr)
        predicted_tr = (knc.predict(X_tr))
        predicted_te = (knc.predict(X_te))

        axis = figure.add_subplot(1,5,spot+1)

        xtr_1 = []
        xtr_2 = []

        for pair in X_tr:
            xtr_1.append(pair[0])
            xtr_2.append(pair[1])

        xte_1 = []
        xte_2 = []

        for pair in X_te:
            xte_1.append(pair[0])
            xte_2.append(pair[1])

        colors = ListedColormap(['#FF0000', '#0000FF'])

        axis.scatter(xtr_1,xtr_2, c = y_tr, cmap = colors, edgecolors = 'k')
        axis.scatter(xte_1,xte_2, marker="*", c = y_te, cmap = colors, edgecolors = 'k')
        x1min, x1max, x2min, x2max = helpers.get_bounds(X_tr)
        helpers.plot_decision_boundary(axis, knc, x1min, x1max, x2min, x2max)
        axis.set_title("n_neighbors = " + str(n[spot]))

        tr_loss = round(zero_one_loss(y_tr,predicted_tr),2)
        te_loss = round(zero_one_loss(y_te,predicted_te),2)

        axis.set_xlabel("Tr loss: " + str(tr_loss)+"\n Te loss: " + str(te_loss))

    plt.show()
Esempio n. 5
0
        lr=0.01
    ),  # increase learning rate from 0.001 to 0.01 for faster learning
    metrics=['accuracy'])

# Fit the model
history = model.fit(X_train, y_train, epochs=25)

# Evaluate our model on the test set
loss, accuracy = model.evaluate(X_test, y_test)
print(f"Model loss on the test set: {loss}")
print(f"Model accuracy on the test set: {100*accuracy:.2f}%")

# Plot the decision boundaries for the training and test sets
plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.title("Train")
plot_decision_boundary(model, X=X_train, y=y_train)
plt.subplot(1, 2, 2)
plt.title("Test")
plot_decision_boundary(model, X=X_test, y=y_test)
plt.show()

# You can access the information in the history variable using the .history attribute
pd.DataFrame(history.history)

# Plot the loss curves
pd.DataFrame(history.history).plot()
plt.title("Model training curves")

model.save('models/my_model.h5')