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

Skip to content

Update learning.py mode function #323

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

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

def mode(data):
"""Return the most common data item. If there are ties, return any one of them."""
(item, count) = Counter(data).most_common(1)
(item, count) = Counter(data).most_common(1)[0]
return item

def rms_error(predictions, targets):
Expand Down
15 changes: 14 additions & 1 deletion tests/test_learning.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import pytest
from learning import parse_csv, weighted_mode, weighted_replicate, DataSet, \
PluralityLearner, NaiveBayesLearner, NearestNeighborLearner
PluralityLearner, NaiveBayesLearner, NearestNeighborLearner, mode
from utils import DataFile


def test_mode():
zoo = DataSet(name='zoo')
data = [e[zoo.target] for e in zoo.examples]

assert mode(data) == 'mammal'

zoo = DataSet(name='iris')
data = [e[zoo.target] for e in zoo.examples]
data.append('setosa')

assert mode(data) == 'setosa'


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