-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhouse.py
69 lines (56 loc) · 1.71 KB
/
house.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
# -*- coding: utf-8 -*-
from mul_2 import Regression
def load_data(data_path):
X=[]
Y=[]
with open(data_path,'r') as f:
content=f.readlines()
for line in content:
line_list=[]
s=line.strip(' ').replace('\n','')
for num in s.split(' '):
if not num=='':
line_list.append(eval(num))
X.append(line_list[:-1])
Y.append(line_list[-1])
return X,Y
def evaluation(W_test,X_test,Y_true):
W=[]
Y=[]
w_l,x_l=len(W_test),len(X_test[0])
if not w_l==x_l and len(X_test)==len(Y_true):
print('dim error ')
return
for w in W_test:
for num in w:
W.append(num)
if type(Y_true[0])==list:
for w in Y_true:
for num in w:
Y.append(num)
else:
Y=Y_true
#
# fenzi=sum([sum([abs(x*w) for x,w in zip(x_row,W)])-abs(y) for x_row,y in zip(X_test,Y)])
# fenmu=sum([abs(y) for y in Y])
fenzi=sum([abs(sum([x*w for x,w in zip(x_row,W)])-y) for x_row,y in zip(X_test,Y)])
fenmu=sum(Y)
#
# dayin=[sum([x*w for x,w in zip(x_row,W)]) for x_row in X_test]
#
#
# return dayin,Y
print('acc is :',1-fenzi/fenmu)
if __name__=='__main__':
data_path='housing.data'
X,Y=load_data(data_path)
print('all data is : {} rows'.format(len(X)))
X_train,Y_train=X[:400],Y[:400]
d=len(X[0])
n=len(X_train)
X_test,Y_test=X[400:],Y[400:]
reg=Regression(d,n,X_train,Y_train)
W=reg.run()
for x_row in X_test:
x_row.append(1)
evaluation(W,X_test,Y_test)