-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_lr.py
40 lines (31 loc) · 911 Bytes
/
test_lr.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
import numpy as np
import random
from matplotlib import pyplot as plt
from sklearn import linear_model
def run():
inp = np.loadtxt('data.txt',dtype='float', delimiter=' ')
n, d = inp.shape
#number of points to generate
points = 2
mat = np.zeros((n*points,d))
for i in range(n):
for j in range(points):
x = random.uniform(-1.0,1.0)
mat[points*i+j,0] = x + inp[i,0]
y = random.uniform(-1.0,1.0)
mat[points*i+j,1] = y + inp[i,1]
plt.scatter(mat[:,0],mat[:,1],color='red')
reg = linear_model.LinearRegression()
x_data = np.zeros((n*points,1))
y_data = np.zeros((n*points,1))
x_data[:,0]=mat[:,0]
y_data[:,0]=mat[:,1]
reg.fit(x_data, y_data)
print('coefficient \n',reg.coef_)
x_test = np.zeros((n,1))
x_test[:,0] = inp[:,0]
y_pred = reg.predict(x_test)
plt.plot(x_test,y_pred,color='blue')
plt.show()
if __name__ == '__main__':
run()