"""
Hilario Iglesias Martínez.
*****************************************
Ejercicio:
CuadernoEstocas-Aula-48-15-SEP-RV-2.py
-----------------------------------------
Ejecución:python3 CuadernoEstocas-Aula-48-15-SEP-RV-2.py
******************************************
Se prueba con los valores con pareamétros mínimos,
con el fin de apreciar su funcionamiento:
learning_rate = 0.01
n_iterations = 4
np.random.seed(0)
X = 2 * np.random.rand(4, 1)
y = 4 + 3 * X + np.random.randn(4, 1)
-------------------------------------------
Creado bajo plataforma Linux.
Ubuntu 20.04.6 LTS
Editado con Sublime Text.
Ejecutado bajo consola linux:
python3 CuadernoEstocas-Aula-48-15-SEP-RV-2.py
"""
import numpy as np
import matplotlib.pyplot as plt
# Genera datos de ejemplo
np.random.seed(0)
X = 2 * np.random.rand(4, 1)
y = 4 + 3 * X + np.random.randn(4, 1)
# Inicialización de parámetros
theta = np.random.randn(2, 1) # Inicialización aleatoria de los parámetros (intercepto y pendiente)
learning_rate = 0.01
n_iterations = 4
m = len(X)
# Función de costo (error cuadrático medio)
def mse_cost(y_true, y_pred):
return ((y_true - y_pred) ** 2).mean()
# Historia de costos para trazar
cost_history = []
# Descenso de gradiente estocástico
for iteration in range(n_iterations):
for i in range(m):
random_index = np.random.randint(m) # Elije una muestra aleatoria
xi = np.vstack([1, X[random_index]]) # Agrega un 1 para el término de intercepto
yi = y[random_index:random_index+1]
y_pred = xi.T.dot(theta)#y_pred=theta0*1+theta1*xi[1]
gradients = -2 * xi.dot(yi - y_pred)#Derivada,gradiente
theta = theta - learning_rate * gradients#Adecuación de pendiente y ordenada cuando x es cero. Peso, sesgo.
print("*****************************")
print("random_index\n:",random_index)
print("*****************************")
print("xi\n:",xi)
print("*****************************")
print("yi\n:",yi)
print("*****************************")
print("y_pred:\n",y_pred)
print("*****************************")
print("gradients:\n",gradients)
print("*****************************")
print("theta:\n",theta)
print("*****************************")
# Calcular el costo después de cada iteración completa
y_pred = X.dot(theta[1:]) + theta[0] # Actualizar y_pred
cost = mse_cost(y, y_pred)#Llamamos a la función mse_cost(y_true, y_pred)
cost_history.append(cost)#Almacenamos en cost_history
# Trazar la convergencia
plt.figure(figsize=(10, 5))
plt.plot(cost_history, marker='o')
plt.xlabel('Iteraciones')
plt.ylabel('Error Cuadrático Medio')
plt.title('Convergencia de SGD')
plt.show()
plt.figure(figsize=(10, 5))
plt.scatter(X,y, label='Datos')
x_line = np.array([0, 2])
y_line = theta[0] + theta[1] * x_line
plt.plot(x_line, y_line, c='red', label='Línea ajustada')
plt.xlabel('Eje X')
plt.ylabel('Eje y')
plt.legend()
plt.show()
# Mostrar los parámetros finales
print("Parámetros finales (theta):", theta)
"""
SEGUIMIENTO.
--------------------------
*****************************
random_index
: 2
*****************************
xi
: [[1. ]
[1.20552675]]
*****************************
yi
: [[8.56666867]]
*****************************
y_pred:
[[0.39176863]]
*****************************
gradients:
[[-16.34980009]
[-19.71012141]]
*****************************
theta:
[[0.06027915]
[0.60769972]]
*****************************
*****************************
random_index
: 3
*****************************
xi
: [[1. ]
[1.08976637]]
*****************************
yi
: [[7.11794189]]
*****************************
y_pred:
[[0.72252986]]
*****************************
gradients:
[[-12.79082406]
[-13.93900985]]
*****************************
theta:
[[0.18818739]
[0.74708981]]
*****************************
*****************************
random_index
: 3
*****************************
xi
: [[1. ]
[1.08976637]]
*****************************
yi
: [[7.11794189]]
*****************************
y_pred:
[[1.00234074]]
*****************************
gradients:
[[-12.2312023 ]
[-13.32915288]]
*****************************
theta:
[[0.31049941]
[0.88038134]]
*****************************
*****************************
random_index
: 2
*****************************
xi
: [[1. ]
[1.20552675]]
*****************************
yi
: [[8.56666867]]
*****************************
y_pred:
[[1.37182267]]
*****************************
gradients:
[[-14.389692 ]
[-17.34715866]]
*****************************
theta:
[[0.45439633]
[1.05385293]]
*****************************
*****************************
random_index
: 0
*****************************
xi
: [[1. ]
[1.09762701]]
*****************************
yi
: [[9.16043901]]
*****************************
y_pred:
[[1.61113377]]
*****************************
gradients:
[[-15.09861049]
[-16.57264265]]
*****************************
theta:
[[0.60538244]
[1.21957936]]
*****************************
*****************************
random_index
: 1
*****************************
xi
: [[1. ]
[1.43037873]]
*****************************
yi
: [[7.31385832]]
*****************************
y_pred:
[[2.34984281]]
*****************************
gradients:
[[ -9.92803101]
[-14.20084442]]
*****************************
theta:
[[0.70466275]
[1.3615878 ]]
*****************************
*****************************
random_index
: 1
*****************************
xi
: [[1. ]
[1.43037873]]
*****************************
yi
: [[7.31385832]]
*****************************
y_pred:
[[2.65224898]]
*****************************
gradients:
[[ -9.32321868]
[-13.33573371]]
*****************************
theta:
[[0.79789493]
[1.49494514]]
*****************************
*****************************
random_index
: 1
*****************************
xi
: [[1. ]
[1.43037873]]
*****************************
yi
: [[7.31385832]]
*****************************
y_pred:
[[2.93623267]]
*****************************
gradients:
[[ -8.7552513 ]
[-12.52332527]]
*****************************
theta:
[[0.88544745]
[1.62017839]]
*****************************
*****************************
random_index
: 1
*****************************
xi
: [[1. ]
[1.43037873]]
*****************************
yi
: [[7.31385832]]
*****************************
y_pred:
[[3.20291616]]
*****************************
gradients:
[[ -8.22188432]
[-11.76040847]]
*****************************
theta:
[[0.96766629]
[1.73778248]]
*****************************
*****************************
random_index
: 0
*****************************
xi
: [[1. ]
[1.09762701]]
*****************************
yi
: [[9.16043901]]
*****************************
y_pred:
[[2.87510327]]
*****************************
gradients:
[[-12.57067149]
[-13.79790853]]
*****************************
theta:
[[1.09337301]
[1.87576156]]
*****************************
*****************************
random_index
: 1
*****************************
xi
: [[1. ]
[1.43037873]]
*****************************
yi
: [[7.31385832]]
*****************************
y_pred:
[[3.77642245]]
*****************************
gradients:
[[ -7.07487174]
[-10.11974607]]
*****************************
theta:
[[1.16412172]
[1.97695902]]
*****************************
*****************************
random_index
: 0
*****************************
xi
: [[1. ]
[1.09762701]]
*****************************
yi
: [[9.16043901]]
*****************************
y_pred:
[[3.33408534]]
*****************************
gradients:
[[-11.65270735]
[-12.7903263 ]]
*****************************
theta:
[[1.2806488 ]
[2.10486228]]
*****************************
*****************************
random_index
: 3
*****************************
xi
: [[1. ]
[1.08976637]]
*****************************
yi
: [[7.11794189]]
*****************************
y_pred:
[[3.57445692]]
*****************************
gradients:
[[-7.08696994]
[-7.72314148]]
*****************************
theta:
[[1.3515185]
[2.1820937]]
*****************************
*****************************
random_index
: 0
*****************************
xi
: [[1. ]
[1.09762701]]
*****************************
yi
: [[9.16043901]]
*****************************
y_pred:
[[3.74664347]]
*****************************
gradients:
[[-10.82759108]
[-11.8846564 ]]
*****************************
theta:
[[1.45979441]
[2.30094026]]
*****************************
*****************************
random_index
: 3
*****************************
xi
: [[1. ]
[1.08976637]]
*****************************
yi
: [[7.11794189]]
*****************************
y_pred:
[[3.96728172]]
*****************************
gradients:
[[-6.30132035]
[-6.86696698]]
*****************************
theta:
[[1.52280761]
[2.36960993]]
*****************************
*****************************
random_index
: 1
*****************************
xi
: [[1. ]
[1.43037873]]
*****************************
yi
: [[7.31385832]]
*****************************
y_pred:
[[4.91224726]]
*****************************
gradients:
[[-4.80322211]
[-6.87042676]]
*****************************
theta:
[[1.57083983]
[2.4383142 ]]
*****************************
"""