Skip to content

Commit

Permalink
Move from xrange to range
Browse files Browse the repository at this point in the history
  • Loading branch information
anassinator committed Sep 21, 2016
1 parent 5b7f5b7 commit 645273d
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion samples/machine_learning/iris.py
Expand Up @@ -53,7 +53,7 @@ def main():
N = 0
for _ in open(IRIS_PATH):
N += 1
testindexes = set(random.sample(xrange(N), N / 10))
testindexes = set(random.sample(range(N), N / 10))

dataset = IrisDataset(IRIS_PATH, lambda i: i not in testindexes)
testset = IrisDataset(IRIS_PATH, lambda i: i in testindexes)
Expand Down
2 changes: 1 addition & 1 deletion samples/machine_learning/language_classification.py
Expand Up @@ -85,7 +85,7 @@ def __iter__(self):

# Choose test set, either 10% or 10000 examples, whatever is less
M = min(N / 10, 10000)
testindexes = set(random.sample(xrange(N), M))
testindexes = set(random.sample(range(N), M))
print("Keeping {} examples for testing".format(M))

problem = LanguageClassificationProblem()
Expand Down
2 changes: 1 addition & 1 deletion samples/machine_learning/opinion.py
Expand Up @@ -100,7 +100,7 @@ def main():

# Choose test set, either 10% or 10000 examples, whatever is less
M = min(N / 10, 1000)
testindexes = set(random.sample(xrange(N), M))
testindexes = set(random.sample(range(N), M))

corpus = ProConsCorpus(input_files, lambda i: i not in testindexes)
test = ProConsCorpus(input_files, lambda i: i in testindexes)
Expand Down
2 changes: 1 addition & 1 deletion simpleai/environments.py
Expand Up @@ -10,7 +10,7 @@ def __init__(self, agents, initial_state):

def run(self, steps=10000, viewer=None):
self.state = self.initial_state
for step in xrange(steps):
for step in range(steps):
if self.is_completed(self.state):
return
self.step(viewer=viewer)
Expand Down
2 changes: 1 addition & 1 deletion simpleai/machine_learning/evaluation.py
Expand Up @@ -46,7 +46,7 @@ def kfold(dataset, problem, method, k=10):

trials = 0
positive = 0
for i in xrange(k):
for i in range(k):
train = [x for j, x in enumerate(dataset) if j % k != i]
test = [x for j, x in enumerate(dataset) if j % k == i]
classifier = method(train, problem)
Expand Down
2 changes: 1 addition & 1 deletion simpleai/machine_learning/models.py
Expand Up @@ -165,7 +165,7 @@ def __init__(self, dataset, target_index):
self.target_index = N + self.target_index
if self.target_index < 0 or N <= self.target_index:
raise ValueError("Target index is out of range")
for i in xrange(N):
for i in range(N):
if i == self.target_index:
continue
attribute = VectorIndexAttribute(i, "data at index {}".format(i))
Expand Down
2 changes: 1 addition & 1 deletion simpleai/search/local.py
Expand Up @@ -290,7 +290,7 @@ def _local_search(problem, fringe_expander, iterations_limit=0, fringe_size=1,

fringe = BoundedPriorityQueue(fringe_size)
if random_initial_states:
for _ in xrange(fringe_size):
for _ in range(fringe_size):
s = problem.generate_random_state()
fringe.append(SearchNodeValueOrdered(state=s, problem=problem))
else:
Expand Down
6 changes: 3 additions & 3 deletions tests/machine_learning/test_classifiers.py
Expand Up @@ -205,9 +205,9 @@ def setup_dataset(self):
n = 100

dataset = []
for i in xrange(n):
for i in range(n):
# Pseudo random generation of bits
bits = [(((i + j) * 1223) % (n + 1)) % 2 for j in xrange(k)]
bits = [(((i + j) * 1223) % (n + 1)) % 2 for j in range(k)]
bits.append(sum(bits) % 2)
dataset.append(bits)

Expand All @@ -226,7 +226,7 @@ def setup_dataset(self):
# patched in Dtree Pseudo (with modifing the pseudocode).

dataset = []
for i in xrange(size):
for i in range(size):
dataset.append([
i % 2 == 0,
i % 3 == 0,
Expand Down
12 changes: 6 additions & 6 deletions tests/machine_learning/test_metrics.py
Expand Up @@ -18,19 +18,19 @@ def test_total_starts_in_zero(self):

def test_add_elements(self):
counter = Counter(lambda x: None)
for i in xrange(20):
for i in range(20):
counter.add("something")
self.assertEqual(counter.total, 20)

def test_target_values(self):
counter = Counter(lambda x: x % 2 == 0)
for i in xrange(25):
for i in range(25):
counter.add(i)
self.assertEqual(counter[0], 12)
self.assertEqual(counter[1], 13)

counter = Counter(lambda x: None)
for i in xrange(50):
for i in range(50):
counter.add(i)
self.assertEqual(counter[None], 50)

Expand All @@ -42,7 +42,7 @@ def test_starts_in_zero(self):

def test_valid_values(self):
entropy = OnlineEntropy(lambda x: x % 10)
for i in xrange(150):
for i in range(150):
entropy.add(i)
self.assertGreaterEqual(entropy.get_entropy(), 0.0)

Expand All @@ -57,15 +57,15 @@ def test_starts_in_zero(self):
def test_no_gain(self):
f = lambda x: None
gain = OnlineInformationGain(f, f)
for i in xrange(30):
for i in range(30):
gain.add(i)
self.assertEqual(gain.get_gain(), 0)

def test_full_gain(self):
target = lambda x: x % 7
gain = OnlineInformationGain(target, target)
entropy = OnlineEntropy(target)
for i in xrange(50):
for i in range(50):
gain.add(i)
entropy.add(i)
self.assertEqual(gain.get_gain(), entropy.get_entropy())
Expand Down

0 comments on commit 645273d

Please sign in to comment.