From 3d254244e564e40fdf348600d168777fa8a8f43a Mon Sep 17 00:00:00 2001 From: kayabaakihiko13 Date: Sun, 21 May 2023 01:02:35 +0700 Subject: [PATCH 1/2] feat: penambahan algoritma ridge Regession --- .../rigde_regession.py | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 implementation/artificial_intelligence/rigde_regession.py diff --git a/implementation/artificial_intelligence/rigde_regession.py b/implementation/artificial_intelligence/rigde_regession.py new file mode 100644 index 00000000..caffb55a --- /dev/null +++ b/implementation/artificial_intelligence/rigde_regession.py @@ -0,0 +1,97 @@ +# ridge regession adalah dimana merupakan tipe +# regessi untuk modifikasi OLS regesi yang bertujuan +# agar membantu untuk menolak overfitting +# overfiting itu adalah pemodelan matematis +# bahwa koresponden yang terlalu dekat dengan nilai +# actual yang bisa menggangu akurasi pemodelan kita +import numpy as np + + +class Ridge: + def __init__(self, ridge_parameter: float = 1.0) -> None: + """ + Parameter: + --- + :param rigde_parameter: adalah merupakan alpha + yang berfungsi untuk bias + di OLS + """ + self.alpha = ridge_parameter + self.intercept = None + self.coef_ = None + self.theta = None + + def fit(self, x: np.array, y: np.array): + """ + Parameters: + --- + :param X: merupakan data untuk melatih algoritma + :param y: merupakan data target + + Return: + --- + :param self: merupakan estimator + """ + X = np.array(x) + X = np.c_[np.ones((X.shape[0], 1)), X] + Y = np.array(y) + W = X.T @ X + identity = np.identity(Y.shape[0]) + bias = self.alpha * identity + self.theta = np.linalg.inv(W + bias) @ X.T @ Y + self.coef_ = self.theta[1:] + self.intercept = self.theta[0] + return self + + def transform(self, x: np.array): + """ + Parameter: + --- + :param x:merupakan data input yang ingin mendapatkan + ouput dari hasil algoritma + Returns: + --- + :param self.predictions: merupakan hasil dari outputnya + """ + thetas = self.theta + X = np.array(x) + X_predictor = np.c_[np.ones((X.shape[0], 1)), X] + self.predictions = X_predictor.dot(thetas) + return self.predictions + + def error_value(self, x: np.array, y: np.array): + X = np.array(x) + y = np.array(y) + self.fit(X, y) + error = y - self.transform(X) + return error + + def score(self, y_actual: np.array, y_predict: np.array) -> float: + tss = np.sum((y_actual - np.mean(y_actual))**2) + rss = np.sum((y_actual - y_predict)**2) + r_squared = 1 - rss / tss + return r_squared + + +def main(): + import matplotlib.pyplot as plt + X = np.array([[1, 2], [2, 3], [4, 5]]) + y = np.array([[1], [7], [9]]) + print(X) + print(y.shape) + model = Ridge() + # modeling=Ridge() + model.fit(X, y) + y_predict = model.transform(X) + print(y_predict) + print(model.score(y, y_predict)) + plt.scatter(X[:, 0], y, color='black') + plt.plot(X[:, 0], y_predict) + plt.title('Low-Alpha Ridge on Three Points') + plt.show() + + +if __name__ == "__main__": + import doctest + doctest.testmod(verbose=True) + main() From a1e79165b6789e5d710ade8c936e98aaa574882b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 20 May 2023 18:04:09 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../artificial_intelligence/rigde_regession.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/implementation/artificial_intelligence/rigde_regession.py b/implementation/artificial_intelligence/rigde_regession.py index caffb55a..ea243637 100644 --- a/implementation/artificial_intelligence/rigde_regession.py +++ b/implementation/artificial_intelligence/rigde_regession.py @@ -67,14 +67,15 @@ def error_value(self, x: np.array, y: np.array): return error def score(self, y_actual: np.array, y_predict: np.array) -> float: - tss = np.sum((y_actual - np.mean(y_actual))**2) - rss = np.sum((y_actual - y_predict)**2) + tss = np.sum((y_actual - np.mean(y_actual)) ** 2) + rss = np.sum((y_actual - y_predict) ** 2) r_squared = 1 - rss / tss return r_squared def main(): import matplotlib.pyplot as plt + X = np.array([[1, 2], [2, 3], [4, 5]]) y = np.array([[1], [7], [9]]) print(X) @@ -85,13 +86,14 @@ def main(): y_predict = model.transform(X) print(y_predict) print(model.score(y, y_predict)) - plt.scatter(X[:, 0], y, color='black') + plt.scatter(X[:, 0], y, color="black") plt.plot(X[:, 0], y_predict) - plt.title('Low-Alpha Ridge on Three Points') + plt.title("Low-Alpha Ridge on Three Points") plt.show() if __name__ == "__main__": import doctest + doctest.testmod(verbose=True) main()