Skip to content

Commit 6925455

Browse files
committed
🎨 : change ipynb to py.
1 parent 3e36caf commit 6925455

3 files changed

Lines changed: 133 additions & 0 deletions

File tree

File renamed without changes.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import pandas as pd
2+
import numpy as np
3+
import matplotlib.pyplot as plt
4+
from model.RidgeRegression import RidgeRegression
5+
6+
# Example usage:
7+
if __name__ == "__main__":
8+
df = pd.read_csv('../dataset/HeightWeight.csv')
9+
first_5_rows = df.head(100)
10+
11+
# Sample data
12+
# X = first_5_rows["Height"].values
13+
# y = first_5_rows["Weight"].values
14+
15+
X = np.array([[0], [2]])
16+
y = np.array([0, 2])
17+
18+
# Create and train the model
19+
model = RidgeRegression(lambda_param=0)
20+
21+
X = model.standardization(X)
22+
23+
# Add bias term to X
24+
X_b = np.c_[np.ones((X.shape[0], 1)), X]
25+
26+
model.training(X_b, y, "gradientDes")
27+
28+
# Make predictions
29+
predictions = model.predict(X_b)
30+
31+
# Calculate and print the loss
32+
error = model.costFunction(X_b, y)
33+
print("Loss:", error)
34+
35+
# Plotting
36+
plt.figure(figsize=(10, 6))
37+
38+
# Plot original data points
39+
plt.scatter(X, y, color='blue', label='Data points')
40+
41+
plt.plot(X, predictions, color='red', label='Prediction line')
42+
43+
# Labels and title
44+
plt.xlabel('Height')
45+
plt.ylabel('Weight')
46+
plt.title('Ridge Regression')
47+
plt.legend()
48+
49+
lambdas = [0, 10, 20 , 40, 400]
50+
slope_vals = np.arange(-30.1, 30.1, 0.1)
51+
slopes = np.array([[0, v] for v in slope_vals])
52+
plt.figure(figsize=(10, 6))
53+
54+
for i, lambda_param in enumerate(lambdas):
55+
w_history = []
56+
c_history = []
57+
for j, slope_param in enumerate(slopes):
58+
model = RidgeRegression(lambda_param=lambda_param, weights=slope_param)
59+
loss = model.costFunction(X_b, y)
60+
w_history.append(slope_param[1])
61+
c_history.append(loss)
62+
plt.plot(w_history, c_history, label=f"λ {lambda_param}")
63+
64+
plt.ylim(0, 20)
65+
plt.xlim(-10, 12)
66+
plt.xlabel('Slope Value')
67+
plt.ylabel('Sum of Squared Residuals + λ x |Slope|')
68+
plt.title('Ridge Regression Visualization')
69+
plt.legend()
70+
71+
plt.show()
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import numpy as np
2+
3+
def gradientDescent(X, y, y_pred, n_samples, lambda_param, weights):
4+
gradient = (1 / n_samples) * (X.T @ (y_pred - y)) + (lambda_param / n_samples) * weights
5+
gradient[0] -= (lambda_param / n_samples) * weights[0] # Don't regularize the bias term
6+
return gradient
7+
8+
def normalEquation(X, y, lambda_param):
9+
# Identity matrix
10+
identity_metrix = lambda_param * np.eye(X.shape[1])
11+
identity_metrix[0, 0] = 0 # Bias term should not be regularized
12+
13+
# Normal equation for ridge regression
14+
weights = np.dot(np.linalg.inv(np.dot(X.T, X) + identity_metrix), np.dot(X.T, y))
15+
return weights
16+
17+
class RidgeRegression:
18+
19+
def __init__(self, lambda_param, weights = None):
20+
self.lambda_param = lambda_param
21+
self.weights = weights
22+
self.weights_history = []
23+
self.costs_history = []
24+
25+
def training(self, X, y, mode, lr = 0.1, n_iters = 100):
26+
if (mode == "normalEq"):
27+
self.weights = normalEquation(X, y, self.lambda_param)
28+
elif (mode == "gradientDes"):
29+
n_samples, n_features = X.shape
30+
self.weights = np.zeros(n_features)
31+
for _ in range(n_iters):
32+
self.weights_history.append(self.weights[1])
33+
self.costs_history.append(self.costFunction(X, y))
34+
y_pred = self.predict(X)
35+
weights_gradient = gradientDescent(X, y, y_pred, n_samples, self.lambda_param, self.weights)
36+
self.weights -= lr * weights_gradient
37+
else:
38+
raise Exception("Sorry, we don't have that type of optimization.")
39+
40+
def standardization(self, X): # X_std = X - mean of X / standard deviation of X
41+
mean_x = np.array([np.mean(X)])
42+
std_x = np.array([np.std(X)])
43+
X_std = (X - mean_x) / std_x
44+
return X_std
45+
46+
def test(self):
47+
print(self.weights.shape)
48+
49+
def get_Weights_History(self):
50+
return self.weights_history
51+
52+
def get_Costs_History(self):
53+
return self.costs_history
54+
55+
def predict(self, X):
56+
return np.dot(X, self.weights)
57+
58+
def costFunction(self, X, y):
59+
y_pred = self.predict(X)
60+
mse = np.sum((y - y_pred) ** 2)
61+
regularization = self.lambda_param * np.sum(self.weights[1:] ** 2)
62+
return mse + regularization

0 commit comments

Comments
 (0)