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

Skip to content

Learning: Grade Learner #496

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,16 @@ def score(learner, size):
return [(size, mean([score(learner, size) for t in range(trials)]))
for size in sizes]


def grade_learner(predict, tests):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be better if it returned the ratio (i..e. percent) of correct tests, rather than the total.
Then we can call it with grade_learner(predicter, tests) > 2/3, regardless of length of tests.

Also, for t in tests should be for (X, y) in tests
Or the whole thing could be return mean(int(predict(X) == y) for X, y in tests)

"""Grades the given learner based on how many tests it passes.
tests is a list with each element in the form: (values, output)."""
correct = 0
for t in tests:
if predict(t[0]) == t[1]:
correct += 1
return correct

# ______________________________________________________________________________
# The rest of this file gives datasets for machine learning problems.

Expand Down
48 changes: 21 additions & 27 deletions tests/test_learning.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
from learning import parse_csv, weighted_mode, weighted_replicate, DataSet, \
PluralityLearner, NaiveBayesLearner, NearestNeighborLearner, \
NeuralNetLearner, PerceptronLearner, DecisionTreeLearner, \
euclidean_distance
euclidean_distance, grade_learner
from utils import DataFile



def test_euclidean():
distance = euclidean_distance([1,2], [3,4])
distance = euclidean_distance([1, 2], [3, 4])
assert round(distance, 2) == 2.83

distance = euclidean_distance([1,2,3], [4,5,6])
distance = euclidean_distance([1, 2, 3], [4, 5, 6])
assert round(distance, 2) == 5.2

distance = euclidean_distance([0,0,0], [0,0,0])
distance = euclidean_distance([0, 0, 0], [0, 0, 0])
assert distance == 0


Expand All @@ -24,7 +24,7 @@ def test_exclude():

def test_parse_csv():
Iris = DataFile('iris.csv').read()
assert parse_csv(Iris)[0] == [5.1,3.5,1.4,0.2,'setosa']
assert parse_csv(Iris)[0] == [5.1, 3.5, 1.4, 0.2,'setosa']


def test_weighted_mode():
Expand All @@ -47,25 +47,25 @@ def test_naive_bayes():

# Discrete
nBD = NaiveBayesLearner(iris)
assert nBD([5,3,1,0.1]) == "setosa"
assert nBD([5, 3, 1, 0.1]) == "setosa"


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"
assert kNN([5, 3, 1, 0.1]) == "setosa"
assert kNN([6, 5, 3, 1.5]) == "versicolor"
assert kNN([7.5, 4, 6, 2]) == "virginica"


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"
assert dTL([5, 3, 1, 0.1]) == "setosa"
assert dTL([6, 5, 3, 1.5]) == "versicolor"
assert dTL([7.5, 4, 6, 2]) == "virginica"


def test_neural_network_learner():
Expand All @@ -75,14 +75,11 @@ def test_neural_network_learner():
iris.classes_to_numbers(classes)

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

# NeuralNetLearner might be wrong. If it is, check if prediction is in range.
assert pred1 == 0 or pred1 in range(len(classes))
assert pred2 == 1 or pred2 in range(len(classes))
assert pred3 == 2 or pred3 in range(len(classes))
assert grade_learner(nNL, tests) >= 2


def test_perceptron():
Expand All @@ -92,11 +89,8 @@ def test_perceptron():
classes_number = len(iris.values[iris.target])

perceptron = PerceptronLearner(iris)
pred1 = perceptron([5,3,1,0.1])
pred2 = perceptron([6,3,4,1])
pred3 = perceptron([7.5,4,6,2])

# PerceptronLearner might be wrong. If it is, check if prediction is in range.
assert pred1 == 0 or pred1 in range(classes_number)
assert pred2 == 1 or pred2 in range(classes_number)
assert pred3 == 2 or pred3 in range(classes_number)
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