From b53f627edb8d312da1d5a154bd276a4d3d607031 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 12 Apr 2020 15:43:19 +0200 Subject: [PATCH 1/6] Text exception conditions These are ValueErrors, not AttributeErrors. --- neural_network/perceptron.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/neural_network/perceptron.py b/neural_network/perceptron.py index 2a1c46b359e6..845f5827a3c9 100644 --- a/neural_network/perceptron.py +++ b/neural_network/perceptron.py @@ -19,15 +19,28 @@ def __init__(self, sample, target, learning_rate=0.01, epoch_number=1000, bias=- :param learning_rate: learning rate used in optimizing. :param epoch_number: number of epochs to train network on. :param bias: bias value for the network. + + >>> p = Perceptron([], (0, 1, 2)) + Traceback (most recent call last): + ... + ValueError: Sample data can not be empty + >>> p = Perceptron((0, 1, 2), []) + Traceback (most recent call last): + ... + ValueError: Target data can not be empty + >>> p = Perceptron((0, 1, 2), (0, 1)) + Traceback (most recent call last): + ... + ValueError: Sample data and Target data do not have matching lengths """ self.sample = sample if len(self.sample) == 0: - raise AttributeError("Sample data can not be empty") + raise ValueError("Sample data can not be empty") self.target = target if len(self.target) == 0: - raise AttributeError("Target data can not be empty") + raise ValueError("Target data can not be empty") if len(self.sample) != len(self.target): - raise AttributeError( + raise ValueError( "Sample data and Target data do not have matching lengths" ) self.learning_rate = learning_rate @@ -87,6 +100,10 @@ def sort(self, sample) -> None: """ :param sample: example row to classify as P1 or P2 :return: None + >>> Perceptron((0, 1, 2), (0, 1, 2)).sort([]) + Traceback (most recent call last): + ... + ValueError: Sample data can not be empty >>> data = [[2.0149, 0.6192, 10.9263]] >>> targets = [-1] >>> perceptron = Perceptron(data,targets) @@ -98,7 +115,7 @@ def sort(self, sample) -> None: classification: P... """ if len(self.sample) == 0: - raise AttributeError("Sample data can not be empty") + raise ValueError("Sample data can not be empty") sample.insert(0, self.bias) u = 0 for i in range(self.col_sample + 1): From 0537acdb287989ba29ca4f05958e9ddeff098551 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 12 Apr 2020 13:44:03 +0000 Subject: [PATCH 2/6] fixup! Format Python code with psf/black push --- neural_network/perceptron.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/neural_network/perceptron.py b/neural_network/perceptron.py index 845f5827a3c9..9a7022e144bd 100644 --- a/neural_network/perceptron.py +++ b/neural_network/perceptron.py @@ -40,9 +40,7 @@ def __init__(self, sample, target, learning_rate=0.01, epoch_number=1000, bias=- if len(self.target) == 0: raise ValueError("Target data can not be empty") if len(self.sample) != len(self.target): - raise ValueError( - "Sample data and Target data do not have matching lengths" - ) + raise ValueError("Sample data and Target data do not have matching lengths") self.learning_rate = learning_rate self.epoch_number = epoch_number self.bias = bias From 51d7502e7c1244c508d48e8f9b15e5037dacfe0a Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 12 Apr 2020 15:52:22 +0200 Subject: [PATCH 3/6] Update perceptron.py --- neural_network/perceptron.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neural_network/perceptron.py b/neural_network/perceptron.py index 9a7022e144bd..2d4b69ca4299 100644 --- a/neural_network/perceptron.py +++ b/neural_network/perceptron.py @@ -24,7 +24,7 @@ def __init__(self, sample, target, learning_rate=0.01, epoch_number=1000, bias=- Traceback (most recent call last): ... ValueError: Sample data can not be empty - >>> p = Perceptron((0, 1, 2), []) + >>> p = Perceptron(([0], 1, 2), []) Traceback (most recent call last): ... ValueError: Target data can not be empty From aafb5c8762fd4afd37132bcaa9122626584f20e8 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 12 Apr 2020 15:58:17 +0200 Subject: [PATCH 4/6] Update perceptron.py --- neural_network/perceptron.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neural_network/perceptron.py b/neural_network/perceptron.py index 2d4b69ca4299..c09d50bf87b0 100644 --- a/neural_network/perceptron.py +++ b/neural_network/perceptron.py @@ -28,7 +28,7 @@ def __init__(self, sample, target, learning_rate=0.01, epoch_number=1000, bias=- Traceback (most recent call last): ... ValueError: Target data can not be empty - >>> p = Perceptron((0, 1, 2), (0, 1)) + >>> p = Perceptron(([0], 1, 2), (0, 1)) Traceback (most recent call last): ... ValueError: Sample data and Target data do not have matching lengths From c77dc7dc477a9a5ac022e61cb6cb75288b73cefc Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 12 Apr 2020 16:06:20 +0200 Subject: [PATCH 5/6] Update perceptron.py --- neural_network/perceptron.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neural_network/perceptron.py b/neural_network/perceptron.py index c09d50bf87b0..3437040582c3 100644 --- a/neural_network/perceptron.py +++ b/neural_network/perceptron.py @@ -98,7 +98,7 @@ def sort(self, sample) -> None: """ :param sample: example row to classify as P1 or P2 :return: None - >>> Perceptron((0, 1, 2), (0, 1, 2)).sort([]) + >>> Perceptron(([0], 1, 2), (0, 1, 2)).sort([]) Traceback (most recent call last): ... ValueError: Sample data can not be empty From 41a7e81cbce2dca395101444375f17ba7544c7ca Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 12 Apr 2020 16:13:48 +0200 Subject: [PATCH 6/6] Revert the test --- neural_network/perceptron.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/neural_network/perceptron.py b/neural_network/perceptron.py index 3437040582c3..23b409b227c4 100644 --- a/neural_network/perceptron.py +++ b/neural_network/perceptron.py @@ -98,10 +98,6 @@ def sort(self, sample) -> None: """ :param sample: example row to classify as P1 or P2 :return: None - >>> Perceptron(([0], 1, 2), (0, 1, 2)).sort([]) - Traceback (most recent call last): - ... - ValueError: Sample data can not be empty >>> data = [[2.0149, 0.6192, 10.9263]] >>> targets = [-1] >>> perceptron = Perceptron(data,targets)