Esempio n. 1
0
def test_perceptron():
    iris = DataSet(name="iris")
    classes = ["setosa", "versicolor", "virginica"]

    iris.classes_to_numbers()

    perceptron = PerceptronLearner(iris)
    # PerceptronLearner might be wrong. Just check if prediction is in range
    assert perceptron([5, 3, 1, 0.1]) in range(len(classes))
Esempio n. 2
0
def test_neural_network_learner():
    iris = DataSet(name="iris")
    classes = ["setosa", "versicolor", "virginica"]

    iris.classes_to_numbers()

    nNL = NeuralNetLearner(iris)
    # NeuralNetLearner might be wrong. Just check if prediction is in range
    assert nNL([5, 3, 1, 0.1]) in range(len(classes))
Esempio n. 3
0
def test_perceptron():
    iris = DataSet(name="iris")
    iris.classes_to_numbers()

    classes_number = len(iris.values[iris.target])

    perceptron = PerceptronLearner(iris)
    tests = [([5, 3, 1, 0.1], 0), ([6, 3, 4, 1.1], 1), ([7.5, 4, 6, 2], 2)]

    assert grade_learner(perceptron, tests) >= 2
Esempio n. 4
0
def test_neural_network_learner():
    iris = DataSet(name="iris")

    classes = ["setosa", "versicolor", "virginica"]
    iris.classes_to_numbers(classes)

    nNL = NeuralNetLearner(iris, [5], 0.15, 75)
    tests = [([5, 3, 1, 0.1], 0), ([6, 3, 3, 1.5], 1), ([7.5, 4, 6, 2], 2)]

    assert grade_learner(nNL, tests) >= 2
Esempio n. 5
0
def test_means_and_deviation():
    iris = DataSet(name="iris")

    means, deviations = iris.find_means_and_deviations()

    assert round(means["setosa"][0], 3) == 5.006
    assert round(means["versicolor"][0], 3) == 5.936
    assert round(means["virginica"][0], 3) == 6.588

    assert round(deviations["setosa"][0], 3) == 0.352
    assert round(deviations["versicolor"][0], 3) == 0.516
    assert round(deviations["virginica"][0], 3) == 0.636
Esempio n. 6
0
def test_means_and_deviation():
    iris = DataSet(name="iris")

    means, deviations = iris.find_means_and_deviations()

    assert round(means["setosa"][0], 3) == 5.006
    assert round(means["versicolor"][0], 3) == 5.936
    assert round(means["virginica"][0], 3) == 6.588

    assert round(deviations["setosa"][0], 3) == 0.352
    assert round(deviations["versicolor"][0], 3) == 0.516
    assert round(deviations["virginica"][0], 3) == 0.636
Esempio n. 7
0
def test_perceptron():
    iris = DataSet(name="iris")
    iris.classes_to_numbers()

    classes_number = len(iris.values[iris.target])

    perceptron = PerceptronLearner(iris)
    tests = [([5, 3, 1, 0.1], 0),
             ([5, 3.5, 1, 0], 0),
             ([6, 3, 4, 1.1], 1),
             ([6, 2, 3.5, 1], 1),
             ([7.5, 4, 6, 2], 2),
             ([7, 3, 6, 2.5], 2)]

    assert grade_learner(perceptron, tests) > 1/2
    assert err_ratio(perceptron, iris) < 0.4
def test_naive_bayes():
    iris = DataSet(name='iris')
    # discrete
    nbd = NaiveBayesLearner(iris, continuous=False)
    assert nbd([5, 3, 1, 0.1]) == 'setosa'
    assert nbd([6, 3, 4, 1.1]) == 'versicolor'
    assert nbd([7.7, 3, 6, 2]) == 'virginica'
    # continuous
    nbc = NaiveBayesLearner(iris, continuous=True)
    assert nbc([5, 3, 1, 0.1]) == 'setosa'
    assert nbc([6, 5, 3, 1.5]) == 'versicolor'
    assert nbc([7, 3, 6.5, 2]) == 'virginica'
    # simple
    data1 = 'a' * 50 + 'b' * 30 + 'c' * 15
    dist1 = CountingProbDist(data1)
    data2 = 'a' * 30 + 'b' * 45 + 'c' * 20
    dist2 = CountingProbDist(data2)
    data3 = 'a' * 20 + 'b' * 20 + 'c' * 35
    dist3 = CountingProbDist(data3)
    dist = {
        ('First', 0.5): dist1,
        ('Second', 0.3): dist2,
        ('Third', 0.2): dist3
    }
    nbs = NaiveBayesLearner(dist, simple=True)
    assert nbs('aab') == 'First'
    assert nbs(['b', 'b']) == 'Second'
    assert nbs('ccbcc') == 'Third'
Esempio n. 9
0
def test_decision_tree_learner():
    iris = DataSet(name="iris")

    dTL = DecisionTreeLearner(iris)
    assert dTL([5, 3, 1, 0.1]) == "setosa"
    assert dTL([6, 5, 3, 1.5]) == "versicolor"
    assert dTL([7.5, 4, 6, 2]) == "virginica"
Esempio n. 10
0
def test_k_nearest_neighbors():
    iris = DataSet(name="iris")

    kNN = NearestNeighborLearner(iris, k=3)
    assert kNN([5, 3, 1, 0.1]) == "setosa"
    assert kNN([6, 5, 3, 1.5]) == "versicolor"
    assert kNN([7.5, 4, 6, 2]) == "virginica"
Esempio n. 11
0
def test_neural_network_learner():
    iris = DataSet(name="iris")

    classes = ["setosa","versicolor","virginica"]
    iris.classes_to_numbers(classes)

    nNL = NeuralNetLearner(iris, [5], 0.15, 75)
    tests = [([5, 3, 1, 0.1], 0),
             ([5, 3.5, 1, 0], 0),
             ([6, 3, 4, 1.1], 1),
             ([6, 2, 3.5, 1], 1),
             ([7.5, 4, 6, 2], 2),
             ([7, 3, 6, 2.5], 2)]

    assert grade_learner(nNL, tests) >= 2/3
    assert err_ratio(nNL, iris) < 0.25
Esempio n. 12
0
def show_iris(i=0, j=1, k=2):
    """Plots the iris dataset in a 3D plot.
    The three axes are given by i, j and k,
    which correspond to three of the four iris features."""
    from mpl_toolkits.mplot3d import Axes3D

    plt.rcParams.update(plt.rcParamsDefault)

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')

    iris = DataSet(name="iris")
    buckets = iris.split_values_by_classes()

    features = ["Sepal Length", "Sepal Width", "Petal Length", "Petal Width"]
    f1, f2, f3 = features[i], features[j], features[k]

    a_setosa = [v[i] for v in buckets["setosa"]]
    b_setosa = [v[j] for v in buckets["setosa"]]
    c_setosa = [v[k] for v in buckets["setosa"]]

    a_virginica = [v[i] for v in buckets["virginica"]]
    b_virginica = [v[j] for v in buckets["virginica"]]
    c_virginica = [v[k] for v in buckets["virginica"]]

    a_versicolor = [v[i] for v in buckets["versicolor"]]
    b_versicolor = [v[j] for v in buckets["versicolor"]]
    c_versicolor = [v[k] for v in buckets["versicolor"]]


    for c, m, sl, sw, pl in [('b', 's', a_setosa, b_setosa, c_setosa),
                             ('g', '^', a_virginica, b_virginica, c_virginica),
                             ('r', 'o', a_versicolor, b_versicolor, c_versicolor)]:
        ax.scatter(sl, sw, pl, c=c, marker=m)

    ax.set_xlabel(f1)
    ax.set_ylabel(f2)
    ax.set_zlabel(f3)

    plt.show()
Esempio n. 13
0
def show_iris(i=0, j=1, k=2):
    """Plots the iris dataset in a 3D plot.
    The three axes are given by i, j and k,
    which correspond to three of the four iris features."""
    from mpl_toolkits.mplot3d import Axes3D

    plt.rcParams.update(plt.rcParamsDefault)

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')

    iris = DataSet(name="iris")
    buckets = iris.split_values_by_classes()

    features = ["Sepal Length", "Sepal Width", "Petal Length", "Petal Width"]
    f1, f2, f3 = features[i], features[j], features[k]

    a_setosa = [v[i] for v in buckets["setosa"]]
    b_setosa = [v[j] for v in buckets["setosa"]]
    c_setosa = [v[k] for v in buckets["setosa"]]

    a_virginica = [v[i] for v in buckets["virginica"]]
    b_virginica = [v[j] for v in buckets["virginica"]]
    c_virginica = [v[k] for v in buckets["virginica"]]

    a_versicolor = [v[i] for v in buckets["versicolor"]]
    b_versicolor = [v[j] for v in buckets["versicolor"]]
    c_versicolor = [v[k] for v in buckets["versicolor"]]


    for c, m, sl, sw, pl in [('b', 's', a_setosa, b_setosa, c_setosa),
                             ('g', '^', a_virginica, b_virginica, c_virginica),
                             ('r', 'o', a_versicolor, b_versicolor, c_versicolor)]:
        ax.scatter(sl, sw, pl, c=c, marker=m)

    ax.set_xlabel(f1)
    ax.set_ylabel(f2)
    ax.set_zlabel(f3)

    plt.show()
Esempio n. 14
0
def test_naive_bayes():
    iris = DataSet(name="iris")

    # Discrete
    nBD = NaiveBayesLearner(iris)
    assert nBD([5, 3, 1, 0.1]) == "setosa"
    assert nBD([6, 5, 3, 1.5]) == "versicolor"
    assert nBD([7, 3, 6.5, 2]) == "virginica"

    # Continuous
    nBC = NaiveBayesLearner(iris, continuous=True)
    assert nBC([5, 3, 1, 0.1]) == "setosa"
    assert nBC([6, 5, 3, 1.5]) == "versicolor"
    assert nBC([7, 3, 6.5, 2]) == "virginica"
Esempio n. 15
0
def test_k_nearest_neighbors():
    iris = DataSet(name="iris")

    kNN = NearestNeighborLearner(iris, k=3)
    assert kNN([5, 3, 1, 0.1]) == "setosa"
Esempio n. 16
0
def test_naive_bayes():
    iris = DataSet(name="iris")

    nB = NaiveBayesLearner(iris)
    assert nB([5, 3, 1, 0.1]) == "setosa"
Esempio n. 17
0
def test_plurality_learner():
    zoo = DataSet(name="zoo")

    pL = PluralityLearner(zoo)
    assert pL([]) == "mammal"
Esempio n. 18
0
def test_decision_tree_learner():
    iris = DataSet(name="iris")

    dTL = DecisionTreeLearner(iris)
    assert dTL([5, 3, 1, 0.1]) == "setosa"
Esempio n. 19
0
def test_exclude():
    iris = DataSet(name='iris', exclude=[3])
    assert iris.inputs == [0, 1, 2]