def test_split_data():

    X = np.ones((12, 3))
    y = np.ones(12)
    X_learn, X_test = split_data(X, frac=2/3)
    assert len(X_learn) == 8 and len(X_test) == 4
    X_learn, y_learn, X_test, y_test = split_data(X, y, frac=0.75)
    assert (len(X_learn) == 9 and len(X_test) == 3 and
            len(y_learn) == 9 and len(y_test) == 3)

    y = np.ones(11)
    try:
        split_data(X, y)
    except (TypeError, ValueError):
        assert True
    else:
        assert False, ("'X' and 'y' have different lengths - " +
                       "an exception should have been raised.")
Beispiel #2
0
    import matplotlib.pyplot as plt
except ImportError:
    plt = None


if __name__ == '__main__':

    np.random.seed(0)

    # load the data
    iris = datasets.load_iris()
    print(iris)
    X, y = iris

    # split into a training and a testing set
    X_learn, y_learn, X_test, y_test = cv.split_data(X, y, frac=3/4)

    # train the (Gaussian) naive Bayes classifier
    naive = ml.NaiveBayesGaussian()
    naive.learn(X_learn, y_learn)
    print(naive)

    # make predictions on the testing set
    y_hat = naive.predict(X_test)
    print("Incorrect predictions on the testing set: {0:.2f} %"
          .format(100. * (y_hat != y_test).sum() / len(y_test)))

    if plt is not None:
        # plot learning data
        colord = {}
        for c in naive.classes: