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

Skip to content

Commit 50e8f0f

Browse files
committed
Added scalar clip function.
1 parent 06a94a2 commit 50e8f0f

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

learning.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ def train(dataset):
408408
error = sum(weight for example, weight in zip(examples, w)
409409
if example[target] != h_k(example))
410410
# Avoid divide-by-0 from either 0% or 100% error rates:
411-
error = max(epsilon, min(error, 1-epsilon))
411+
error = clip(error, epsilon, 1-epsilon)
412412
for j, example in enumerate(examples):
413413
if example[target] == h_k(example):
414414
w[j] *= error / (1. - error)

utils.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,15 @@ def normalize(numbers):
525525
total = float(sum(numbers))
526526
return [n / total for n in numbers]
527527

528+
def clip(x, lowest, highest):
529+
"""Return x clipped to the range [lowest..highest].
530+
>>> clip(-1, 0, 1)
531+
0
532+
>>> clip(10, 0, 1)
533+
1
534+
"""
535+
return max(lowest, min(x, highest))
536+
528537
#______________________________________________________________________________
529538
## OK, the following are not as widely useful utilities as some of the other
530539
## functions here, but they do show up wherever we have 2D grids: Wumpus and
@@ -549,14 +558,15 @@ def distance2((ax, ay), (bx, by)):
549558
"The square of the distance between two (x, y) points."
550559
return (ax - bx)**2 + (ay - by)**2
551560

552-
def clip(vector, lowest, highest):
561+
def vector_clip(vector, lowest, highest):
553562
"""Return vector, except if any element is less than the corresponding
554563
value of lowest or more than the corresponding value of highest, clip to
555564
those values.
556-
>>> clip((-1, 10), (0, 0), (9, 9))
565+
>>> vector_clip((-1, 10), (0, 0), (9, 9))
557566
(0, 9)
558567
"""
559-
return type(vector)(map(min, map(max, vector, lowest), highest))
568+
return type(vector)(map(clip, vector, lowest, highest))
569+
560570
#______________________________________________________________________________
561571
# Misc Functions
562572

0 commit comments

Comments
 (0)