forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgradient descent python
More file actions
41 lines (28 loc) · 926 Bytes
/
gradient descent python
File metadata and controls
41 lines (28 loc) · 926 Bytes
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
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_wine
from sklearn.preprocessing import StandardScaler
data = load_wine()
X = data.data[:, :1] # Use the first feature for simplicity
y = data.target.astype(float)
scaler = StandardScaler()
X = scaler.fit_transform(X)
X_b = np.c_[np.ones((X.shape[0], 1)), X] # shape (n_samples, 2)
theta = np.random.randn(2)
learning_rate = 0.1
iterations = 100
m = len(y)
loss_history = []
for i in range(iterations):
predictions = X_b.dot(theta)
errors = predictions - y
gradient = (2/m) * X_b.T.dot(errors)
theta = theta - learning_rate * gradient
loss = (1/m) * np.sum(errors**2) # MSE
loss_history.append(loss)
plt.plot(range(iterations), loss_history, color='blue')
plt.xlabel("Iterations")
plt.ylabel("Mean Squared Error")
plt.title("Gradient Descent on Wine Data")
plt.show()
print("Final parameters:", theta)