Skip to content

Commit

Permalink
fix: memperbaiki beberapa kode yang terbaca warning dan error pada LG…
Browse files Browse the repository at this point in the history
…TM security (#275)

Signed-off-by: slowy07 <[email protected]>
  • Loading branch information
slowy07 authored Jun 10, 2023
1 parent 350355a commit bf68975
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 49 deletions.
7 changes: 0 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ repos:
hooks:
- id: black

- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort
args:
- --profile=black

- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
hooks:
Expand Down
2 changes: 1 addition & 1 deletion Basic/08_perulangan/while_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@
e += 1
break
else:
print("Selamat, looping anda telah selesai !!") # lgtm [py/unreachable-statement]
print("Selamat, looping anda telah selesai !!")
26 changes: 16 additions & 10 deletions implementation/artificial_intelligence/k_means_clutser.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
# 2. menetapkan setiap titik data ke pusat k terdekat.
# titik yang dekat dengan pusat k tertentu
import warnings
from typing import Optional

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from sklearn import datasets
from sklearn.metrics import pairwise_distances

warnings.filterwarnings("ignore")
Expand Down Expand Up @@ -97,14 +99,19 @@ def plot_heteroginity(heterogenity, k):


def kmeans(
data, k, inisial_centroid, maxiter=500, var_hitung_heterogenity=None, verbose=False
data: np.ndarray,
k: int,
inisial_centroid: np.ndarray,
maxiter: int = 500,
var_hitung_heterogenity: Optional[list[float]] = None,
verbose: bool = False,
):
centroid = inisial_centroid[:]
prev_assign_clutser = None

for itr in range(maxiter):
if verbose:
print(itr, end="")
print(itr, end=" ")

# buat tugas clutser menggunakan centroid
# terdekat
Expand All @@ -115,9 +122,9 @@ def kmeans(
# poin yang ditetapkan ke klutser tersebut
centroid = fixing_centroid(data, k, assign_clutser)

if (prev_assign_clutser is not None) and (
prev_assign_clutser == assign_clutser
).all():
if prev_assign_clutser is not None and np.array_equal(
prev_assign_clutser, assign_clutser
):
break

# tampilkan jumlah dari assignment terbaru
Expand All @@ -128,16 +135,15 @@ def kmeans(
if var_hitung_heterogenity is not None:
skor = hitung_heterogenity(data, k, centroid, assign_clutser)
var_hitung_heterogenity.append(skor)
prev_assign_clutser = assign_clutser.copy()

return centroid, assign_clutser


if False:
from sklearn import dataset as ds

dataset = ds.load_iris()
k = 3
heterogenity = []
dataset = datasets.load_iris()
k: int = 3
heterogenity: list[float] = []
inisial_centroid = dapatkan_inisial_centroid(dataset["data"], k, seed=0)
centroid, assign_clutser = kmeans(
dataset["data"],
Expand Down
27 changes: 11 additions & 16 deletions implementation/linear_algebra/conjugate_gradient.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Any

import numpy as np


Expand All @@ -14,10 +12,10 @@ def is_matrix_spd(matrix: np.ndarray) -> bool:
>>> is_matrix_spd(set_matrix)
True
"""
assert np.shape(matrix)[0] == np.shape(matrix)[1]
assert matrix.shape[0] == matrix.shape[1]

# Jika matriks tidak simetris, exit
if np.allclose(matrix, matrix.T) is False:
if not np.allclose(matrix, matrix.T):
return False

# Dapatkan nilai eigen dan
Expand All @@ -27,7 +25,7 @@ def is_matrix_spd(matrix: np.ndarray) -> bool:
return bool(np.all(eigen_value > 0))


def create_spd_matrix(dimension: int) -> Any:
def create_spd_matrix(dimension: int) -> np.ndarray:
"""
Mengembalikan matriks definit positif
simetris yang diberi dimensi.
Expand All @@ -40,11 +38,11 @@ def create_spd_matrix(dimension: int) -> Any:


def conjugate_gradient(
spd_matrix,
load_vector,
max_iterations=1000,
tol=1e-8,
):
spd_matrix: np.ndarray,
load_vector: np.ndarray,
max_iterations: int = 1000,
tol: float = 1e-8,
) -> np.ndarray:
"""
return solusi linear sistem np.dot(spd_matrix, x) = b
>>> import numpy as np
Expand All @@ -61,18 +59,15 @@ def conjugate_gradient(
[-0.01561498],
[ 0.13979294]])
"""
assert np.shape(spd_matrix)[0] == np.shape(spd_matrix)[1]
assert np.shape(load_vector)[0] == np.shape(spd_matrix)[0]
assert spd_matrix.shape[0] == spd_matrix.shape[1]
assert load_vector.shape[0] == spd_matrix.shape[0]
assert is_matrix_spd(spd_matrix)

x0 = np.zeros((np.shape(load_vector)[0], 1))
r0 = np.copy(load_vector)
p0 = np.copy(r0)

error_residual = 1e9 # lgtm [py/multiple-definition]
error_x_solution = 1e9 # lgtm [py/multiple-definition]
error = 1e9

iterations = 0

while error > tol:
Expand All @@ -91,7 +86,7 @@ def conjugate_gradient(
# kalkulasi error
error_residual = np.linalg.norm(r - r0)
error_x_solution = np.linalg.norm(x - x0)
error = np.maximum(error_residual, error_x_solution)
error = max(error_residual, error_x_solution)

# update variabel
x0 = np.copy(x)
Expand Down
39 changes: 24 additions & 15 deletions struktur_data/tree/binary_search_tree/binary_search_tree.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from typing import Optional


class BinarySearchTree:
# method __init__ digunakan untuk membangun Node
def __init__(self, data: int = None):
self.data = data
self.left = None
self.right = None
def __init__(self, data: Optional[int] = None) -> None:
self.data: Optional[int] = data
self.left: Optional[int] = None
self.right: Optional[int] = None

def insert(self, new_data):
def insert(self, new_data: int) -> None:
if self.data is None:
# menginisasi root jika belum ada node sama sekali
self.data = new_data
Expand All @@ -24,40 +27,46 @@ def insert(self, new_data):
# rekursif melalui atribut right
self.right.insert(new_data)

def preorder(self, arr: list = []):
def preorder(self, arr: Optional[list[int]] = None) -> list[int]:
# Algoritma preorder adalah sebagai berikut:
# 1. Append data pada node
# 2. Traverse ke kiri
# 3. Traverse ke kanan
arr.append(self.data) # lgtm [py/modification-of-default-value]
if arr is None:
arr = []
arr.append(self.data)
if self.left is not None:
self.left.preorder(arr)
if self.right is not None:
self.right.preorder(arr)
return arr

def inorder(self, arr: list = []):
def inorder(self, arr: Optional[list[int]] = None) -> list[int]:
# Algoritma inorder adalah sebagai berikut:
# 1. Traverse ke kiri
# 2. Append data pada node
# 3. Traverse ke kanan
if self.left is not None:
if arr is None:
arr = []
if self.left:
self.left.inorder(arr)
arr.append(self.data) # lgtm [py/modification-of-default-value]
if self.right is not None:
arr.append(self.data)
if self.right:
self.right.inorder(arr)
return arr

def postorder(self, arr: list = []):
def postorder(self, arr: Optional[list[int]] = None) -> list[int]:
# Algoritma postorder adalah sebagai berikut:
# 1. Traverse ke kiri
# 2. Traverse ke kanan
# 3. Append data pada node
if self.left is not None:
if arr is None:
arr = []
if self.left:
self.left.postorder(arr)
if self.right is not None:
if self.right:
self.right.postorder(arr)
arr.append(self.data) # lgtm [py/modification-of-default-value]
arr.append(self.data)
return arr


Expand Down

0 comments on commit bf68975

Please sign in to comment.