Exemple #1
0
def read_data(only_2_features=True):
    iris = datasets.load_iris()
    X, y = iris.data, iris.target
    if only_2_features:
        X = X[:, :2]
    return X, y


if __name__ == '__main__':
    X, y = read_data()
    fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
    svm1 = svm.SVC()
    svm1.fit(X, y)
    ax1.set_title("SVC linear")
    plot_areas(lambda x: svm1.predict(x), 0.1, X, ax1)
    plot_2d_classes(X, y, 'ryb', ax1)
    svm2 = svm.SVC(kernel='poly', degree=2)
    svm2.fit(X, y)
    ax2.set_title("SVC polynomial, deg: 2")
    plot_areas(lambda x: svm2.predict(x), 0.1, X, ax2)
    plot_2d_classes(X, y, 'ryb', ax2)
    svm3 = svm.SVC(kernel='poly', degree=3)
    svm3.fit(X, y)
    ax3.set_title("SVC polynomial, deg: 3")
    plot_areas(lambda x: svm3.predict(x), 0.1, X, ax3)
    plot_2d_classes(X, y, 'ryb', ax3)
    svm4 = svm.SVC(kernel='poly', degree=6)
    svm4.fit(X, y)
    ax4.set_title("SVC polynomial, deg: 6")
    plot_areas(lambda x: svm4.predict(x), 0.1, X, ax4)
Exemple #2
0
        cnn_percents = []
        for i in range(10):
            X_training, y_training, X_test, y_test = split_dataset(X, y, 0.7)
            if cnn:
                len_before = X_training.shape[0]
                X_training, y_training = cnn_transform(X_training, y_training, k, metric)
                len_after = X_training.shape[0]
                cnn_percents.append(float(len_after) / float(len_before) * 100.0)
            predictions = []
            for i in range(X_test.shape[0]):
                predictions.append(kNN(X_training, X_training, y_training, X_test[i, :], k, metric))
            if SHOW_PREDICTIONS_AND_REAL_VALUES:
                print('Prediction, actual:')
                for i in range(X_test.shape[0]):
                    print(predictions[i], y_test[i])
            correct = 0
            for i in range(len(predictions)):
                if y_test[i] == predictions[i]:
                    correct += 1
            accuracies.append(float(correct) / float(len(predictions)) * 100.0)
        print("Accuracy:", str(np.mean(accuracies)) + '%', 'StdDev:', np.std(accuracies))
        if cnn:
            print("CNN:", str(np.mean(cnn_percents)) + '%', 'StdDev:', np.std(cnn_percents))
    else:
        if cnn:
            X, y = cnn_transform(X, y, k, metric)
        plot_areas(lambda x: kNN(X, X, y, x, k, metric), 0.1, X)
        plot_2d_classes(X, y, 'ryb')
        cnn_str = '_cnn' if cnn else ''
        plt.savefig('plots/k' + str(k) + '_' + metric.__name__ + cnn_str + '.png')