Demo entry 6320820

test1

   

Submitted by test1 on Nov 10, 2016 at 03:15
Language: Python 3. Code size: 908 Bytes.

# -*- coding: utf-8 -*-
"""
Created on Sun Nov  6 20:05:35 2016

@author: Roberto
"""
import numpy as np
import matplotlib.pyplot as plt
dat=open('L:\Aplicaciones\Tarea 2016\Tarea 2016\P1_code\P1_dataset.txt','r')
lines = dat.readlines()
dat.close()

x=[]
y=[]
for line in lines:
    p=line.split()
    x.append(float(p[0]))
    y.append(float(p[1]))

xv=np.array(x)
yv=np.array(y)

vect=np.repeat(1,len(x))
X1=np.column_stack((vect,x))
matriz_diseno=np.dot(X1.T, X1)
inverso=np.linalg.inv(matriz_diseno)
matrix_2=np.dot(X1.T, y)
beta_est=inverso.dot(matrix_2)
print(beta_est)

a=[min(xv),max(xv)]
b=[beta_est[0] + min(xv) * beta_est[1], beta_est[0] +  max(xv) * beta_est[1]]

plt.plot(xv,yv,'sb')
plt.grid(True)
plt.xlabel('x')
plt.ylabel('y')
plt.title('Datos Regresion Lineal')
plt.text(4,2.6,'$y=1.01732595 + 0.06565208 x$')

plt.plot(a,b, c = "red")
plt.show()

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).