Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit efa5628

Browse files
antmarakisnorvig
authored andcommitted
Update test_learning.py (#376)
Add DecisionTreeLearner, NeuralNetLearner and PerceptronLearner tests
1 parent 2b07ba9 commit efa5628

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

tests/test_learning.py

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
from learning import parse_csv, weighted_mode, weighted_replicate, DataSet, \
2-
PluralityLearner, NaiveBayesLearner, NearestNeighborLearner
2+
PluralityLearner, NaiveBayesLearner, NearestNeighborLearner, \
3+
NeuralNetLearner, PerceptronLearner, DecisionTreeLearner
34
from utils import DataFile
45

56

7+
68
def test_parse_csv():
79
Iris = DataFile('iris.csv').read()
8-
assert parse_csv(Iris)[0] == [5.1, 3.5, 1.4, 0.2, 'setosa']
10+
assert parse_csv(Iris)[0] == [5.1,3.5,1.4,0.2,'setosa']
911

1012

1113
def test_weighted_mode():
@@ -20,18 +22,46 @@ def test_plurality_learner():
2022
zoo = DataSet(name="zoo")
2123

2224
pL = PluralityLearner(zoo)
23-
assert pL([]) == "mammal"
25+
assert pL([1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 4, 1, 0, 1]) == "mammal"
2426

2527

2628
def test_naive_bayes():
2729
iris = DataSet(name="iris")
2830

2931
nB = NaiveBayesLearner(iris)
30-
assert nB([5, 3, 1, 0.1]) == "setosa"
32+
assert nB([5,3,1,0.1]) == "setosa"
3133

3234

3335
def test_k_nearest_neighbors():
3436
iris = DataSet(name="iris")
3537

36-
kNN = NearestNeighborLearner(iris, k=3)
37-
assert kNN([5, 3, 1, 0.1]) == "setosa"
38+
kNN = NearestNeighborLearner(iris,k=3)
39+
assert kNN([5,3,1,0.1]) == "setosa"
40+
41+
def test_decision_tree_learner():
42+
iris = DataSet(name="iris")
43+
44+
dTL = DecisionTreeLearner(iris)
45+
assert dTL([5,3,1,0.1]) == "setosa"
46+
47+
48+
def test_neural_network_learner():
49+
iris = DataSet(name="iris")
50+
classes = ["setosa","versicolor","virginica"]
51+
52+
iris.classes_to_numbers()
53+
54+
nNL = NeuralNetLearner(iris)
55+
# NeuralNetLearner might be wrong. Just check if prediction is in range
56+
assert nNL([5,3,1,0.1]) in range(len(classes))
57+
58+
59+
def test_perceptron():
60+
iris = DataSet(name="iris")
61+
classes = ["setosa","versicolor","virginica"]
62+
63+
iris.classes_to_numbers()
64+
65+
perceptron = PerceptronLearner(iris)
66+
# PerceptronLearner might be wrong. Just check if prediction is in range
67+
assert perceptron([5,3,1,0.1]) in range(len(classes))

0 commit comments

Comments
 (0)