-
Notifications
You must be signed in to change notification settings - Fork 0
/
03-slr.py
56 lines (43 loc) · 1.6 KB
/
03-slr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
class SimpleLinearRegression:
def __init__(self, learning_rate=0.01, num_iterations=1000):
self.learning_rate = learning_rate
self.num_iterations = num_iterations
self.slope = 0
self.intercept = 0
def fit(self, X, y):
num_samples, num_features = X.shape
self.slope = np.zeros(num_features)
self.intercept = 0
for _ in range(self.num_iterations):
y_pred = np.dot(X, self.slope) + self.intercept
grad_slope = - (2 / num_samples) * np.dot(X.T, y - y_pred)
grad_intercept = - (2 / num_samples) * np.sum(y - y_pred)
self.slope -= self.learning_rate * grad_slope
self.intercept -= self.learning_rate * grad_intercept
def predict(self, X):
return np.dot(X, self.slope) + self.intercept
def calculate_rmse(self, y, y_pred):
mse = np.mean((y - y_pred) ** 2)
rmse = np.sqrt(mse)
return rmse
data = pd.read_csv("Salary_dataset.csv")
X = np.array([[1], [2], [3], [4], [5]])
y = np.array([2, 4, 5, 4, 5])
# X = data["YearsExperience"].values
# y = data["Salary"].values
# X = X.reshape(-1, 1)
model = SimpleLinearRegression(learning_rate=0.01, num_iterations=1000)
model.fit(X, y)
y_pred = model.predict(X)
rmse = model.calculate_rmse(y, y_pred)
print(f"RMSE: {rmse}")
plt.scatter(X, y, label="Actual data")
plt.plot(X, y_pred, color='pink', label="Fitted line")
plt.xlabel("Years_Experience")
plt.ylabel("Salary")
plt.title("Simple Linear Regression - Salary dataset")
plt.legend()
plt.show()