Ejemplos de Machine Learning para el uso y aplicación de regresiones
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
799 B

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. ###############################
  4. #Datos originales
  5. ###############################
  6. X = 2 * np.random.rand(100, 1)
  7. y = 4 + 3 * X + np.random.randn(100,1)
  8. plt.plot(X,y,".")
  9. ###############################
  10. X_b = np.c_[np.ones((100,1)), X] #Se agrega x0=1 para cada instancia
  11. m=100
  12. n_epochs = 50
  13. t0, t1 = 5, 50 # learning schedule hyperparameters
  14. def learning_schedule(t):
  15. return t0 / (t + t1)
  16. theta = np.random.randn(2,1) # random initialization
  17. for epoch in range(n_epochs):
  18. for i in range(m):
  19. random_index = np.random.randint(m)
  20. xi = X_b[random_index:random_index+1]
  21. yi = y[random_index:random_index+1]
  22. gradients = 2 * xi.T.dot(xi.dot(theta) - yi)
  23. eta = learning_schedule(epoch * m + i)
  24. theta = theta - eta * gradients
  25. print(theta)