commit b6ef092a183d5ce1b8eece55eb12c525af83e722 Author: jason_cv Date: Tue Apr 14 17:40:15 2020 -0500 Ejemplo de regresion lineal diff --git a/Regresionlinealmatricial.py b/Regresionlinealmatricial.py new file mode 100644 index 0000000..df56e77 --- /dev/null +++ b/Regresionlinealmatricial.py @@ -0,0 +1,23 @@ +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 +theta_best=np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(y) + +X_new = np.array([[0], [2]]) +X_new_b = np.c_[np.ones((2, 1)), X_new] #Se agrega x0=1 para cada instancia +y_predict = X_new_b.dot(theta_best) + +plt.plot(X_new, y_predict, "r-") +plt.plot(X, y, "b.") +plt.axis([0, 2, 0, 15]) +plt.show() \ No newline at end of file