|
|
@ -0,0 +1,21 @@ |
|
|
|
import numpy as np |
|
|
|
import matplotlib.pyplot as plt |
|
|
|
############################### |
|
|
|
#Datos originales |
|
|
|
############################### |
|
|
|
X = 2 * np.random.rand(100, 1) |
|
|
|
y = 4 + 3 * X + np.random.randn(100,1) |
|
|
|
|
|
|
|
plt.plot(X,y,".") |
|
|
|
############################### |
|
|
|
X_b = np.c_[np.ones((100,1)), X] #Se agrega x0=1 para cada instancia |
|
|
|
eta = 0.1 #Pasos |
|
|
|
n_itera = 1000 |
|
|
|
m=100 |
|
|
|
|
|
|
|
theta = np.random.randn(2,1) #Inicialización aleatoria |
|
|
|
for iteracion in range (n_itera): |
|
|
|
gradiente = 2/m * X_b.T.dot(X_b.dot(theta)-y) |
|
|
|
theta = theta - eta * gradiente |
|
|
|
|
|
|
|
print(theta) |