From ef32568f1725de24ed54da400960252737e45fb0 Mon Sep 17 00:00:00 2001 From: cclauss Date: Sun, 14 Jul 2019 07:59:35 +0200 Subject: [PATCH 1/4] Travis CI: Add pytest --doctest-modules neural_network Fixes #987 ``` neural_network/perceptron.py:123: in sample.insert(i, float(input('value: '))) ../lib/python3.7/site-packages/_pytest/capture.py:693: in read raise IOError("reading from stdin while output is captured") E OSError: reading from stdin while output is captured -------------------------------------------------------------------------------- Captured stdout -------------------------------------------------------------------------------- ('\nEpoch:\n', 399) ------------------------ value: ``` --- .travis.yml | 4 - machine_learning/perceptron.py | 124 ------------------ .../random_forest_classification.py | 8 +- .../random_forest_regression.py | 8 +- neural_network/perceptron.py | 15 +-- requirements.txt | 1 + 6 files changed, 18 insertions(+), 142 deletions(-) delete mode 100644 machine_learning/perceptron.py diff --git a/.travis.yml b/.travis.yml index a3ff22fb09b7..eb83e23eff99 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,10 +14,6 @@ script: --ignore=data_structures/stacks/infix_to_postfix_conversion.py --ignore=file_transfer_protocol/ftp_send_receive.py --ignore=file_transfer_protocol/ftp_client_server.py - --ignore=machine_learning/linear_regression.py - --ignore=machine_learning/perceptron.py - --ignore=machine_learning/random_forest_classification/random_forest_classification.py - --ignore=machine_learning/random_forest_regression/random_forest_regression.py --ignore=maths/abs_min.py --ignore=maths/binary_exponentiation.py --ignore=maths/lucas_series.py diff --git a/machine_learning/perceptron.py b/machine_learning/perceptron.py deleted file mode 100644 index fe1032aff4af..000000000000 --- a/machine_learning/perceptron.py +++ /dev/null @@ -1,124 +0,0 @@ -''' - - Perceptron - w = w + N * (d(k) - y) * x(k) - - Using perceptron network for oil analysis, - with Measuring of 3 parameters that represent chemical characteristics we can classify the oil, in p1 or p2 - p1 = -1 - p2 = 1 - -''' -from __future__ import print_function - -import random - - -class Perceptron: - def __init__(self, sample, exit, learn_rate=0.01, epoch_number=1000, bias=-1): - self.sample = sample - self.exit = exit - self.learn_rate = learn_rate - self.epoch_number = epoch_number - self.bias = bias - self.number_sample = len(sample) - self.col_sample = len(sample[0]) - self.weight = [] - - def trannig(self): - for sample in self.sample: - sample.insert(0, self.bias) - - for i in range(self.col_sample): - self.weight.append(random.random()) - - self.weight.insert(0, self.bias) - - epoch_count = 0 - - while True: - erro = False - for i in range(self.number_sample): - u = 0 - for j in range(self.col_sample + 1): - u = u + self.weight[j] * self.sample[i][j] - y = self.sign(u) - if y != self.exit[i]: - - for j in range(self.col_sample + 1): - - self.weight[j] = self.weight[j] + self.learn_rate * (self.exit[i] - y) * self.sample[i][j] - erro = True - #print('Epoch: \n',epoch_count) - epoch_count = epoch_count + 1 - # if you want controle the epoch or just by erro - if erro == False: - print(('\nEpoch:\n',epoch_count)) - print('------------------------\n') - #if epoch_count > self.epoch_number or not erro: - break - - def sort(self, sample): - sample.insert(0, self.bias) - u = 0 - for i in range(self.col_sample + 1): - u = u + self.weight[i] * sample[i] - - y = self.sign(u) - - if y == -1: - print(('Sample: ', sample)) - print('classification: P1') - else: - print(('Sample: ', sample)) - print('classification: P2') - - def sign(self, u): - return 1 if u >= 0 else -1 - - -samples = [ - [-0.6508, 0.1097, 4.0009], - [-1.4492, 0.8896, 4.4005], - [2.0850, 0.6876, 12.0710], - [0.2626, 1.1476, 7.7985], - [0.6418, 1.0234, 7.0427], - [0.2569, 0.6730, 8.3265], - [1.1155, 0.6043, 7.4446], - [0.0914, 0.3399, 7.0677], - [0.0121, 0.5256, 4.6316], - [-0.0429, 0.4660, 5.4323], - [0.4340, 0.6870, 8.2287], - [0.2735, 1.0287, 7.1934], - [0.4839, 0.4851, 7.4850], - [0.4089, -0.1267, 5.5019], - [1.4391, 0.1614, 8.5843], - [-0.9115, -0.1973, 2.1962], - [0.3654, 1.0475, 7.4858], - [0.2144, 0.7515, 7.1699], - [0.2013, 1.0014, 6.5489], - [0.6483, 0.2183, 5.8991], - [-0.1147, 0.2242, 7.2435], - [-0.7970, 0.8795, 3.8762], - [-1.0625, 0.6366, 2.4707], - [0.5307, 0.1285, 5.6883], - [-1.2200, 0.7777, 1.7252], - [0.3957, 0.1076, 5.6623], - [-0.1013, 0.5989, 7.1812], - [2.4482, 0.9455, 11.2095], - [2.0149, 0.6192, 10.9263], - [0.2012, 0.2611, 5.4631] - -] - -exit = [-1, -1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1] - -network = Perceptron(sample=samples, exit = exit, learn_rate=0.01, epoch_number=1000, bias=-1) - -network.trannig() - -while True: - sample = [] - for i in range(3): - sample.insert(i, float(input('value: '))) - network.sort(sample) diff --git a/machine_learning/random_forest_classification/random_forest_classification.py b/machine_learning/random_forest_classification/random_forest_classification.py index d5dde4b13822..81016387ecc7 100644 --- a/machine_learning/random_forest_classification/random_forest_classification.py +++ b/machine_learning/random_forest_classification/random_forest_classification.py @@ -1,17 +1,19 @@ # Random Forest Classification # Importing the libraries +import os import numpy as np import matplotlib.pyplot as plt import pandas as pd # Importing the dataset -dataset = pd.read_csv('Social_Network_Ads.csv') +script_dir = os.path.dirname(os.path.realpath(__file__)) +dataset = pd.read_csv(os.path.join(script_dir, 'Social_Network_Ads.csv')) X = dataset.iloc[:, [2, 3]].values y = dataset.iloc[:, 4].values # Splitting the dataset into the Training set and Test set -from sklearn.cross_validation import train_test_split +from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0) # Feature Scaling @@ -66,4 +68,4 @@ plt.xlabel('Age') plt.ylabel('Estimated Salary') plt.legend() -plt.show() \ No newline at end of file +plt.show() diff --git a/machine_learning/random_forest_regression/random_forest_regression.py b/machine_learning/random_forest_regression/random_forest_regression.py index fce58b1fe283..b984b02b6e6e 100644 --- a/machine_learning/random_forest_regression/random_forest_regression.py +++ b/machine_learning/random_forest_regression/random_forest_regression.py @@ -1,12 +1,14 @@ # Random Forest Regression # Importing the libraries +import os import numpy as np import matplotlib.pyplot as plt import pandas as pd # Importing the dataset -dataset = pd.read_csv('Position_Salaries.csv') +script_dir = os.path.dirname(os.path.realpath(__file__)) +dataset = pd.read_csv(os.path.join(script_dir, 'Position_Salaries.csv')) X = dataset.iloc[:, 1:2].values y = dataset.iloc[:, 2].values @@ -28,7 +30,7 @@ regressor.fit(X, y) # Predicting a new result -y_pred = regressor.predict(6.5) +y_pred = regressor.predict(6.5).reshape(1, -1) # Visualising the Random Forest Regression results (higher resolution) X_grid = np.arange(min(X), max(X), 0.01) @@ -38,4 +40,4 @@ plt.title('Truth or Bluff (Random Forest Regression)') plt.xlabel('Position level') plt.ylabel('Salary') -plt.show() \ No newline at end of file +plt.show() diff --git a/neural_network/perceptron.py b/neural_network/perceptron.py index 787ea8f73bf1..1083c5b43f79 100644 --- a/neural_network/perceptron.py +++ b/neural_network/perceptron.py @@ -113,13 +113,12 @@ def sign(self, u): exit = [-1, -1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1] -if __name__ == '__main__': - network = Perceptron(sample=samples, exit = exit, learn_rate=0.01, epoch_number=1000, bias=-1) +network = Perceptron(sample=samples, exit = exit, learn_rate=0.01, epoch_number=1000, bias=-1) - network.training() +network.training() - while True: - sample = [] - for i in range(3): - sample.insert(i, float(input('value: ').strip())) - network.sort(sample) +while True: + sample = [] + for i in range(3): + sample.insert(i, float(input('value: '))) + network.sort(sample) diff --git a/requirements.txt b/requirements.txt index 91d3df33323d..abc2fc545708 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,6 +6,7 @@ numpy opencv-python pandas pytest +requests sklearn sympy tensorflow From 4bdc619faaec8b82a15108c9b2cc3fd8562b609b Mon Sep 17 00:00:00 2001 From: cclauss Date: Sat, 20 Jul 2019 15:19:32 +0200 Subject: [PATCH 2/4] Adding fix from #1056 -- thanks @QuantumNovice --- .../random_forest_regression/random_forest_regression.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/machine_learning/random_forest_regression/random_forest_regression.py b/machine_learning/random_forest_regression/random_forest_regression.py index b984b02b6e6e..85ce0676b598 100644 --- a/machine_learning/random_forest_regression/random_forest_regression.py +++ b/machine_learning/random_forest_regression/random_forest_regression.py @@ -30,7 +30,7 @@ regressor.fit(X, y) # Predicting a new result -y_pred = regressor.predict(6.5).reshape(1, -1) +y_pred = regressor.predict([[6.5]]) # Visualising the Random Forest Regression results (higher resolution) X_grid = np.arange(min(X), max(X), 0.01) From db2ca600463213ad434610259738b64beb6cb0c2 Mon Sep 17 00:00:00 2001 From: cclauss Date: Sat, 20 Jul 2019 15:29:51 +0200 Subject: [PATCH 3/4] if __name__ == '__main__': --- neural_network/perceptron.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/neural_network/perceptron.py b/neural_network/perceptron.py index 1083c5b43f79..871eca20273b 100644 --- a/neural_network/perceptron.py +++ b/neural_network/perceptron.py @@ -117,8 +117,9 @@ def sign(self, u): network.training() -while True: - sample = [] - for i in range(3): - sample.insert(i, float(input('value: '))) - network.sort(sample) +if __name__ == '__main__': + while True: + sample = [] + for i in range(3): + sample.insert(i, float(input('value: '))) + network.sort(sample) From 448703162c591f38c479cc5093df33c57dea6f26 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 21 Jul 2019 12:43:13 +0200 Subject: [PATCH 4/4] pytest --ignore=virtualenv # do not test our dependencies --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 95368c52f165..fa9c4cd3e053 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,6 +14,7 @@ script: --ignore=data_structures/stacks/infix_to_postfix_conversion.py --ignore=file_transfer_protocol/ftp_send_receive.py --ignore=file_transfer_protocol/ftp_client_server.py + --ignore=virtualenv # do not test our dependencies after_success: - python scripts/build_directory_md.py - cat DIRECTORY.md