Fix ruff errors (#8936)

* Fix ruff errors

Renamed neural_network/input_data.py to neural_network/input_data.py_tf
because it should be left out of the directory for the following
reasons:

1. Its sole purpose is to be used by neural_network/gan.py_tf, which is
   itself left out of the directory because of issues with TensorFlow.

2. It was taken directly from TensorFlow's codebase and is actually
   already deprecated. If/when neural_network/gan.py_tf is eventually
   re-added back to the directory, its implementation should be changed
   to not use neural_network/input_data.py anyway.

* updating DIRECTORY.md

* Change input_data.py_tf file extension

Change input_data.py_tf file extension because algorithms-keeper bot is being picky about it

---------

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
Tianyi Zheng
2023-08-09 00:55:30 -07:00
committed by GitHub
parent 842d03fb2a
commit ae0fc85401
21 changed files with 121 additions and 94 deletions

View File

@ -141,7 +141,7 @@ class Matrix:
@property
def order(self) -> tuple[int, int]:
return (self.num_rows, self.num_columns)
return self.num_rows, self.num_columns
@property
def is_square(self) -> bool:
@ -315,7 +315,7 @@ class Matrix:
]
)
def __mul__(self, other: Matrix | int | float) -> Matrix:
def __mul__(self, other: Matrix | float) -> Matrix:
if isinstance(other, (int, float)):
return Matrix(
[[int(element * other) for element in row] for row in self.rows]

View File

@ -47,7 +47,7 @@ def subtract(matrix_a: list[list[int]], matrix_b: list[list[int]]) -> list[list[
raise TypeError("Expected a matrix, got int/list instead")
def scalar_multiply(matrix: list[list[int]], n: int | float) -> list[list[float]]:
def scalar_multiply(matrix: list[list[int]], n: float) -> list[list[float]]:
"""
>>> scalar_multiply([[1,2],[3,4]],5)
[[5, 10], [15, 20]]
@ -189,9 +189,7 @@ def main() -> None:
matrix_c = [[11, 12, 13, 14], [21, 22, 23, 24], [31, 32, 33, 34], [41, 42, 43, 44]]
matrix_d = [[3, 0, 2], [2, 0, -2], [0, 1, 1]]
print(f"Add Operation, {add(matrix_a, matrix_b) = } \n")
print(
f"Multiply Operation, {multiply(matrix_a, matrix_b) = } \n",
)
print(f"Multiply Operation, {multiply(matrix_a, matrix_b) = } \n")
print(f"Identity: {identity(5)}\n")
print(f"Minor of {matrix_c} = {minor(matrix_c, 1, 2)} \n")
print(f"Determinant of {matrix_b} = {determinant(matrix_b)} \n")

View File

@ -1,9 +1,7 @@
from __future__ import annotations
def search_in_a_sorted_matrix(
mat: list[list[int]], m: int, n: int, key: int | float
) -> None:
def search_in_a_sorted_matrix(mat: list[list[int]], m: int, n: int, key: float) -> None:
"""
>>> search_in_a_sorted_matrix(
... [[2, 5, 7], [4, 8, 13], [9, 11, 15], [12, 17, 20]], 3, 3, 5)

View File

@ -22,7 +22,7 @@ class Matrix:
"""
self.row, self.column = row, column
self.array = [[default_value for c in range(column)] for r in range(row)]
self.array = [[default_value for _ in range(column)] for _ in range(row)]
def __str__(self) -> str:
"""
@ -54,15 +54,15 @@ class Matrix:
def __repr__(self) -> str:
return str(self)
def validate_indicies(self, loc: tuple[int, int]) -> bool:
def validate_indices(self, loc: tuple[int, int]) -> bool:
"""
<method Matrix.validate_indicies>
Check if given indices are valid to pick element from matrix.
Example:
>>> a = Matrix(2, 6, 0)
>>> a.validate_indicies((2, 7))
>>> a.validate_indices((2, 7))
False
>>> a.validate_indicies((0, 0))
>>> a.validate_indices((0, 0))
True
"""
if not (isinstance(loc, (list, tuple)) and len(loc) == 2):
@ -81,7 +81,7 @@ class Matrix:
>>> a[1, 0]
7
"""
assert self.validate_indicies(loc)
assert self.validate_indices(loc)
return self.array[loc[0]][loc[1]]
def __setitem__(self, loc: tuple[int, int], value: float) -> None:
@ -96,7 +96,7 @@ class Matrix:
[ 1, 1, 1]
[ 1, 1, 51]
"""
assert self.validate_indicies(loc)
assert self.validate_indices(loc)
self.array[loc[0]][loc[1]] = value
def __add__(self, another: Matrix) -> Matrix:
@ -145,7 +145,7 @@ class Matrix:
def __sub__(self, another: Matrix) -> Matrix:
return self + (-another)
def __mul__(self, another: int | float | Matrix) -> Matrix:
def __mul__(self, another: float | Matrix) -> Matrix:
"""
<method Matrix.__mul__>
Return self * another.
@ -233,7 +233,7 @@ class Matrix:
v_t = v.transpose()
numerator_factor = (v_t * self * u)[0, 0] + 1
if numerator_factor == 0:
return None # It's not invertable
return None # It's not invertible
return self - ((self * u) * (v_t * self) * (1.0 / numerator_factor))