-
Notifications
You must be signed in to change notification settings - Fork 1
/
q2A.py
97 lines (90 loc) · 2.55 KB
/
q2A.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 13 13:31:03 2019
@author: Aditya's HP Omen 15
"""
# this program if for line fitting using OLS (oridnary least squares)
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
import pickle
from statistics import mean
from numpy import linalg as LA
style.use('fivethirtyeight')
# DATA1
#importing data
f = open('data1_new.pkl','rb')
data1 = pickle.load(f)
f.close()
da = np.asarray(data1)
X = da[:,0]
Y = da[:,1]
plt.axis([-150,150,-100,100])
#creatng OLS function
def OLS(X,Y):
m = (np.multiply(mean(X),mean(Y)) -mean(np.multiply(X,Y) ))/((mean(X))**2-mean((X)**2))
c = mean(Y) - m*mean(X)
return m,c
m,c = OLS(X,Y)
regression_line = [(m*x)+c for x in X]
plt.figure(1)
plt.scatter(X,Y, color = 'blue')
#plotting line using OLS parameters
plt.plot(X,regression_line, color = 'red', label = "OLS")
X2 = np.array([da[:,0],X])
Y2 = np.array([da[:,1],regression_line])
plt.plot(X2,Y2,color = 'red')
plt.title("Line fitting with vertical least squares on data 1")
plt.legend()
plt.axis([-150,150,-100,100])
plt.show()
#DATA2
f = open('data2_new.pkl','rb')
data1 = pickle.load(f)
f.close()
da = np.asarray(data1)
X = da[:,0]
Y = da[:,1]
plt.axis([-150,150,-100,100])
#creatng OLS function
def OLS(X,Y):
m = (np.multiply(mean(X),mean(Y)) -mean(np.multiply(X,Y) ))/((mean(X))**2-mean((X)**2))
c = mean(Y) - m*mean(X)
return m,c
m,c = OLS(X,Y)
regression_line = [(m*x)+c for x in X]
plt.figure(2)
plt.scatter(X,Y, color = 'blue')
#plotting line using OLS parameters
plt.plot(X,regression_line, color = 'red', label = "OLS")
X2 = np.array([da[:,0],X])
Y2 = np.array([da[:,1],regression_line])
plt.plot(X2,Y2,color = 'red')
plt.title("Line fitting with vertical least squares on data 2")
plt.legend()
plt.axis([-150,150,-100,100])
plt.show()
#DATA3
f = open('data3_new.pkl','rb')
data1 = pickle.load(f)
f.close()
da = np.asarray(data1)
X = da[:,0]
Y = da[:,1]
plt.axis([-150,150,-100,100])
def OLS(X,Y):
m = (np.multiply(mean(X),mean(Y)) -mean(np.multiply(X,Y) ))/((mean(X))**2-mean((X)**2))
c = mean(Y) - m*mean(X)
return m,c
m,c = OLS(X,Y)
regression_line = [(m*x)+c for x in X]
plt.figure(3)
plt.scatter(X,Y, color = 'blue')
plt.plot(X,regression_line, color = 'red', label = "OLS")
X2 = np.array([da[:,0],X])
Y2 = np.array([da[:,1],regression_line])
plt.plot(X2,Y2,color = 'red')
plt.title("Line fitting with vertical least squares on data 3")
plt.legend()
plt.axis([-150,150,-100,100])
plt.show()