Skip to content

Commit 9b60be6

Browse files
[mypy] fix small folders 2 (#4293)
* Update perceptron.py * Update binary_tree_traversals.py * fix machine_learning * Update build.yml * Update perceptron.py * Update machine_learning/forecasting/run.py Co-authored-by: Christian Clauss <[email protected]>
1 parent 9595079 commit 9b60be6

File tree

6 files changed

+22
-13
lines changed

6 files changed

+22
-13
lines changed

Diff for: .github/workflows/build.yml

+3
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,13 @@ jobs:
3838
genetic_algorithm
3939
geodesy
4040
knapsack
41+
machine_learning
4142
networking_flow
43+
neural_network
4244
quantum
4345
scheduling
4446
sorts
47+
traversals
4548
- name: Run tests
4649
run: pytest --doctest-modules --ignore=project_euler/ --ignore=scripts/ --cov-report=term-missing:skip-covered --cov=. .
4750
- if: ${{ success() }}

Diff for: machine_learning/forecasting/run.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ def linear_regression_prediction(
2929
>>> abs(n - 5.0) < 1e-6 # Checking precision because of floating point errors
3030
True
3131
"""
32-
x = [[1, item, train_mtch[i]] for i, item in enumerate(train_dt)]
33-
x = np.array(x)
32+
x = np.array([[1, item, train_mtch[i]] for i, item in enumerate(train_dt)])
3433
y = np.array(train_usr)
3534
beta = np.dot(np.dot(np.linalg.inv(np.dot(x.transpose(), x)), x.transpose()), y)
3635
return abs(beta[0] + test_dt[0] * beta[1] + test_mtch[0] + beta[2])

Diff for: machine_learning/k_means_clust.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def kmeans(
200200

201201

202202
def ReportGenerator(
203-
df: pd.DataFrame, ClusteringVariables: np.array, FillMissingReport=None
203+
df: pd.DataFrame, ClusteringVariables: np.ndarray, FillMissingReport=None
204204
) -> pd.DataFrame:
205205
"""
206206
Function generates easy-erading clustering report. It takes 2 arguments as an input:

Diff for: machine_learning/word_frequency_functions.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def term_frequency(term: str, document: str) -> int:
6161
return len([word for word in tokenize_document if word.lower() == term.lower()])
6262

6363

64-
def document_frequency(term: str, corpus: str) -> int:
64+
def document_frequency(term: str, corpus: str) -> tuple[int, int]:
6565
"""
6666
Calculate the number of documents in a corpus that contain a
6767
given term

Diff for: neural_network/perceptron.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@
1111

1212

1313
class Perceptron:
14-
def __init__(self, sample, target, learning_rate=0.01, epoch_number=1000, bias=-1):
14+
def __init__(
15+
self,
16+
sample: list[list[float]],
17+
target: list[int],
18+
learning_rate: float = 0.01,
19+
epoch_number: int = 1000,
20+
bias: float = -1,
21+
) -> None:
1522
"""
1623
Initializes a Perceptron network for oil analysis
1724
:param sample: sample dataset of 3 parameters with shape [30,3]
@@ -46,7 +53,7 @@ def __init__(self, sample, target, learning_rate=0.01, epoch_number=1000, bias=-
4653
self.bias = bias
4754
self.number_sample = len(sample)
4855
self.col_sample = len(sample[0]) # number of columns in dataset
49-
self.weight = []
56+
self.weight: list = []
5057

5158
def training(self) -> None:
5259
"""
@@ -94,7 +101,7 @@ def training(self) -> None:
94101
# if epoch_count > self.epoch_number or not error:
95102
break
96103

97-
def sort(self, sample) -> None:
104+
def sort(self, sample: list[float]) -> None:
98105
"""
99106
:param sample: example row to classify as P1 or P2
100107
:return: None
@@ -221,11 +228,11 @@ def sign(self, u: float) -> int:
221228
print("Finished training perceptron")
222229
print("Enter values to predict or q to exit")
223230
while True:
224-
sample = []
231+
sample: list = []
225232
for i in range(len(samples[0])):
226-
observation = input("value: ").strip()
227-
if observation == "q":
233+
user_input = input("value: ").strip()
234+
if user_input == "q":
228235
break
229-
observation = float(observation)
236+
observation = float(user_input)
230237
sample.insert(i, observation)
231238
network.sort(sample)

Diff for: traversals/binary_tree_traversals.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def pre_order_iter(node: TreeNode) -> None:
188188
"""
189189
if not isinstance(node, TreeNode) or not node:
190190
return
191-
stack: List[TreeNode] = []
191+
stack: list[TreeNode] = []
192192
n = node
193193
while n or stack:
194194
while n: # start from root node, find its left child
@@ -218,7 +218,7 @@ def in_order_iter(node: TreeNode) -> None:
218218
"""
219219
if not isinstance(node, TreeNode) or not node:
220220
return
221-
stack: List[TreeNode] = []
221+
stack: list[TreeNode] = []
222222
n = node
223223
while n or stack:
224224
while n:

0 commit comments

Comments
 (0)