Skip to content

Instantly share code, notes, and snippets.

@rubcuadra
Created August 15, 2017 16:43
Show Gist options
  • Save rubcuadra/8b75e1ea382e5ea5fb81a6411e0ac7f6 to your computer and use it in GitHub Desktop.
Save rubcuadra/8b75e1ea382e5ea5fb81a6411e0ac7f6 to your computer and use it in GitHub Desktop.
Script 15min en clase para obtener la regresión con una linea, usa la lógica de multiplicar matrices A-1*y = thetas
#Regresion lineal, solo jala con linea, implementar el solicitar un grado de polinomio
import matplotlib.pyplot as plt
import numpy as np
x = [ 500,1300,2000,3800,4000,5500,6800,7400,10000]
y = [ 1000,2000,3000,4500,5000,5100,5300,5400,6000 ]
m = len(x)
Ex = sum(i for i in x)
ExCuad = sum(i*i for i in x)
Ey = sum(i for i in y)
Exy = 0
for i in range(0,len(x)):
Exy += x[i]*y[i]
A = np.array([[m,Ex],
[Ex,ExCuad]])
Y = np.array([Ey,Exy])
Ainv = np.linalg.inv(A)
thetas = Ainv.dot(Y)
fx = lambda _x: thetas[0] + thetas[1]*_x
# Plot the data
plt.scatter(x,y, label='Valores Tamano/Casa')
plt.plot(x, [ fx(i) for i in x], label='Polinomio')
# Add a legend
plt.legend()
# Show the plot
plt.show()
#Otra solucion:
#Transpuesta del vector de datos X multiplicada por datos X
#Sacamos inversa a ese resultado
#Multiplicamos por X transpuesta
#Multiplicamos Matriz Y
#Metodo del gradiente descendente
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment